Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does INotifyPropertyChanging interface helps limit memory consumption

I am starting to learn LINQ-to-SQL for Windows Phone 8, and came across this article on MSDN.

They show a base class for DataContext which implements both INotifyPropertyChanging and INotifyPropertyChanged. The reasoning for the INotifyPropertyChanging is:

◦The INotifyPropertyChanged interface is used for change tracking.

◦The INotifyPropertyChanging interface helps limit memory consumption related to change tracking.

The article fails to give any specific references to justify the claim of memory consumption to the INotifyPropertyChanging interface. The article on INotifyPropertyChanging itself just says:

Notifies clients that a property value is changing.

Can someone please explain to me how this interface which limits the memory footprint of an application, just by notifying that a property value is about to change (and not even restricting that change to from happening)?

like image 947
Adarsha Avatar asked Sep 02 '14 15:09

Adarsha


People also ask

What is the purpose of INotifyPropertyChanged?

The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed. For example, consider a Person object with a property called FirstName .

When to use INotifyPropertyChanged in WPF?

INotifyPropertyChanged interface is used to notify the view or ViewModel that it does not matter which property is binding; it is updated. Let's take an example for understanding this interface. Take one WPF Window in which there are a total of three fields: First Name, Last Name and Full Name.

How do you implement INotifyPropertyChanged interface?

To implement INotifyPropertyChanged you need to declare the PropertyChanged event and create the OnPropertyChanged method. Then for each property you want change notifications for, you call OnPropertyChanged whenever the property is updated.


2 Answers

I can only extrapolate, but I think that's what the author had in mind:

In a world without INotifyPropertyChanging, if the consumer needs the old value of a property, it has to preemptively cache it (because, once the PropertyChanged event is raised, it's too late and the value is already changed). Alternatively, the producer can keep a copy of the old value in distinct properties. Either way, the data remains duplicated all the time.

With INotifyPropertyChanging, the consumer doesn't need to cache anything beforehand. When the PropertyChanging event is raised, it can grab the old value, knowing it's about to change. Then the NotifyPropertyChanged event is raised, the consumer can grab the new value, do whatever with both, then drop them. The data is still duplicated, but only at a certain point of time and for a limited duration.

like image 91
Kevin Gosse Avatar answered Oct 07 '22 23:10

Kevin Gosse


Okay, I finally found another MSDN article which actually explains how INotifyPropertyChanging will limit memory footprint. Quoting the article (emphasis mine):

Notifications are provided through the PropertyChanging event in property setters. When LINQ to SQL is notified of the first change to an object, it creates a copy of the object and considers the object a candidate for generating an Update statement.

For objects that do not implement INotifyPropertyChanging, LINQ to SQL maintains a copy of the values that objects had when they were first materialized.

So if you don't implement INotifyPropertyChanging and never update any objects fetched using Linq-SQL, it will still create a copy of the object for every object it creates. By implementing the interface, you can avoid that additional memory usage, and have it create copies only when you actually are making a change to the object state.

like image 33
Adarsha Avatar answered Oct 08 '22 00:10

Adarsha