4 September 2008

How To: Use notifications of property changed

There are two ways to get notified of changes made withing a command:

  1. CommandDone event (in UndoRedoManager)
  2. Changed event (in particular UndoRedo property, list or dictionary)

CommandDone event is fired after any command has been commited/undone/redone. It is fired once per command despite how many changes were in the command.

Changed event provides much higher level of granularity. It is fired when particular property/list/dictionary was changed during a command.

In this post I will tell more about the Changed event. This event provides very precise control on what data notifications you receive. Subscribtion is fairly simple:



class DataClass
{
internal readonly UndoRedo<int> prop1 = new UndoRedo<int>(0);
public int Prop1
{
get { return prop1.Value; }
set { prop1.Value = value; }
}
public Subscribe()
{
prop1.Changed +=
new EventHandler<MemberChangedEventArgs>(prop1_Changed);
}
}


Besides of precision, Changed event guaranties data consistency for subscriber. In other words, subscriber may always assume that all data is in consistent state despite on order of how data was changed. For example, we subscribe on Prop1 changes. The point is that our handler may rely that Prop2 is consistent too even if it was changed later in the command.

This magic happens because Changed event is not fired immediately but waits untill the command is commited. And this is the most important thing you have to understand: firing of Changed event is postponed to the point where all data is in consistent state and commited.

This advantage allows you to selectively update UI layer with confidence that changed property and any other data properties can be correctly displayed. So, you can write very intelligent and reliable MVC/MVVM pattern even for complex cases.

Another notes are:

  • If command was cancelled, there will not be any Changed event;
  • If a property was changed ten times during the command, Changed event will be fired once for this property when command commited;
  • Changed event will also be fired when the same command is Undone/Redone;
  • You can assign Owner and Name to the property. It usefull when one handler subscribed on several properties at once:



prop1.Owner = this;
prop1.Name = "Prop1";
prop2.Owner = this;
prop2.Name = "Prop2";
...
void prop_Changed(object sender, MemberChangedEventArgs e)
{
if (e.Member.Name == "Prop1")
{...}
}

8 comments:

Clover said...

This is really a great read for me. Thank you for publishing articles having a great insight stimulates me to check more often for new write ups. Keep posting!

Clover
www.n8fan.net

Pebbles said...

it is fabulous to have some great and awesome blog! I was like geez! Can’t wait to share this blog site to my friends the contents are very informative and useful.

pebbles
www.joeydavila.net

sarah lee said...

I really enjoyed reading your article. I found this as an informative and interesting post, so i think it is very useful and knowledgeable. I would like to thank you for the effort you have made in writing this article.


edupdf.org

Leslie Lim said...

Good article. I was really enjoyed reading your post. This is truly worthy to read. Good content and good information. God bless!

Jasmin
www.imarksweb.org

sandra said...


Every time I visit your blog it really completes my day, and hey its not a joke. I am telling the truth. Thank you for always inspiring us and for writing a very touching article.

zandra

www.n8fan.net

lee woo said...

Love it! Very interesting topics, I hope the incoming comments and suggestion are equally positive. Thank you for sharing this information that is actually helpful.


ufgop.org
ufgop.org

Barbie Chiu said...

Thank you for sharing your thoughts and for inspiring us. Keep it up and continue on what your doing. Visit my site too.

triciajoy.com

www.triciajoy.com

Cindy Dy said...

I enjoy reading your articles. You really have a wonderful blogs. Keep up the good work. Thank you also for the information!

www.gofastek.com