joelweiss/ChangeTracking

Nested TrackableCollection : Change One getting Multiple Notifications

toumir opened this issue · 3 comments

Hi,
Take a look at the following LinqPad script.
Can you explain why I get notified multiple times (for Orderdetails) despite I made one change

void Main()
{
	Order order = new Order
	{
		Id = 1,
		CustomerNumber = "Test",
		OrderDetails = new List<OrderDetail>
		{
			new OrderDetail
			{
				OrderDetailId = 1,
				ItemNo = "Item123"
			},
			new OrderDetail
			{
				OrderDetailId = 2,
				ItemNo = "Item369"
			}
		}
	};

	var trackedOrder = order.AsTrackable();
	trackedOrder.CastToIChangeTrackable().PropertyChanged += (s, e) =>
	 {
		e.PropertyName.Dump();
	 };

	var first = trackedOrder.OrderDetails.FirstOrDefault();

	first.ItemNo = "test"; //<==== only one change
}

public class Order
{
	public virtual int Id { get; set; }
	public virtual string CustomerNumber { get; set; }
	public virtual decimal Total { get; set; }
	public virtual IList<OrderDetail> OrderDetails { get; set; }
}

public class OrderDetail
{
	public virtual int OrderDetailId { get; set; }
	public virtual string ItemNo { get; set; }
}

here the output when changing ItemNo

ChangeTrackingStatus
ChangedProperties
OrderDetails //1
OrderDetails //2
OrderDetails //3
OrderDetails //4

Thanks Sir for your time.

Hi, can anyone confirm this behavior ?
thanks

On every change of a first.Property the parent raises PropertyChanged

change your code like so

...
var trackedOrder = order.AsTrackable();
trackedOrder.CastToIChangeTrackable().PropertyChanged += (s, e) =>
 {
	 e.PropertyName.Dump("Parent");
 };

var first = trackedOrder.OrderDetails.FirstOrDefault();
first.CastToIChangeTrackable().PropertyChanged += (s, e) =>
 {
	 e.PropertyName.Dump("Child");
 };
...

this will output

Parent
ChangeTrackingStatus

Parent
ChangedProperties

Parent
OrderDetails

Child
ChangeTrackingStatus

Parent
OrderDetails

Child
ChangedProperties

Parent
OrderDetails

Child
ItemNo

Parent
OrderDetails

Child
ChangedProperties

HTH

Got it thank you Sir :)