Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EntityChanged EventArgs #194

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,22 @@ public void EntityChanged_Event_Should_Fire_When_Graph_Items_Added_Modified_Dele
Assert.Equal(7, changesCount);
}

[Fact]
public void EntityChanged_Event_Should_Provide_Better_EventArgs()
{
var database = new MockNorthwind();
var order = database.Orders[0];
var changeTracker = new ChangeTrackingCollection<Order> { order };
changeTracker.EntityChanged += (s, e) =>
{
var ecea = (EntityChangedEventArgs)e;
Assert.Same(order, ecea.Entity);
Assert.Equal(nameof(Order.OrderId), ecea.PropertyName);
};

order.OrderId++;
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
<Compile Include="..\TrackableEntities.Client\EntityBasePortable.cs">
<Link>EntityBasePortable.cs</Link>
</Compile>
<Compile Include="..\TrackableEntities.Client\EntityChangedEventArgs.cs">
<Link>EntityChangedEventArgs.cs</Link>
</Compile>
<Compile Include="..\TrackableEntities.Client\IIdentifiable.cs">
<Link>IIdentifiable.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
<Compile Include="..\TrackableEntities.Client\EntityBase.cs">
<Link>EntityBase.cs</Link>
</Compile>
<Compile Include="..\TrackableEntities.Client\EntityChangedEventArgs.cs">
<Link>EntityChangedEventArgs.cs</Link>
</Compile>
<Compile Include="..\TrackableEntities.Client\IIdentifiable.cs">
<Link>IIdentifiable.cs</Link>
</Compile>
Expand Down
8 changes: 4 additions & 4 deletions Source/TrackableEntities.Client/ChangeTrackingCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
if (entity.TrackingState == TrackingState.Unchanged)
{
entity.TrackingState = TrackingState.Modified;
if (EntityChanged != null) EntityChanged(this, EventArgs.Empty);
if (EntityChanged != null) EntityChanged(this, new EntityChangedEventArgs(entity, null, e.PropertyName));
}

// Add prop to modified props, and fire EntityChanged event
Expand All @@ -171,7 +171,7 @@ private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
}
}
}
}
}

/// <summary>
/// Insert item at specified index.
Expand Down Expand Up @@ -201,7 +201,7 @@ protected override void InsertItem(int index, TEntity item)
item.SetState(TrackingState.Added, visitationHelper.Clone());

// Fire EntityChanged event
if (EntityChanged != null) EntityChanged(this, EventArgs.Empty);
if (EntityChanged != null) EntityChanged(this, new EntityChangedEventArgs(item, Parent, null));
}
base.InsertItem(index, item);
}
Expand Down Expand Up @@ -239,7 +239,7 @@ protected override void RemoveItem(int index)
item.SetState(TrackingState.Deleted, visitationHelper.Clone());

// Fire EntityChanged event
if (EntityChanged != null) EntityChanged(this, EventArgs.Empty);
if (EntityChanged != null) EntityChanged(this, new EntityChangedEventArgs(item, Parent, null));

// Cache deleted item if not added or already cached
if (item.TrackingState != TrackingState.Added
Expand Down
39 changes: 39 additions & 0 deletions Source/TrackableEntities.Client/EntityChangedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TrackableEntities.Client
{
/// <summary>
/// Supplies data about the object that raised the event.
/// </summary>
public class EntityChangedEventArgs : EventArgs
{
/// <summary>
/// Initializes a new instance of <see cref="EntityChangedEventArgs"/>. /// </summary>
/// <param name="entity"></param>
/// <param name="parent"></param>
/// <param name="propertyName"></param>
public EntityChangedEventArgs(ITrackable entity, ITrackable parent, string propertyName)
{
Entity = entity;
Parent = parent;
PropertyName = propertyName;
}

/// <summary>
/// To be added.
/// </summary>
public ITrackable Entity { get; }
/// <summary>
/// To be added.
/// </summary>
public ITrackable Parent { get; }
/// <summary>
/// To be added.
/// </summary>
public string PropertyName { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<Compile Include="Constants.cs" />
<Compile Include="EntityBase.cs" />
<Compile Include="EntityBasePortable.cs" />
<Compile Include="EntityChangedEventArgs.cs" />
<Compile Include="IIdentifiable.cs" />
<Compile Include="IRefPropertyChangeTrackerResolver.cs" />
<Compile Include="ITrackingCollection.cs" />
Expand Down