Object Notifiers using INotifyPropertyChanged, INotifyCollectionChanged

Object Notifiers are very useful for any language. We declare classes which might be used by other processes. Now whenever any changes of the object needs to notify the external world, Microsoft introduced a new Technique in doing so. In this article I have discussed how you can implement your own class which would notify the external world whenever certain modification of the object is made.

Observer Patter deals with the technique where the objects modified into the collection should notify the external entity.In this article I have also provided the basics on how you can implement your own Observer rather than using the already existing ObservableCollection, an MS implementation of Observer.

Introduction


Classes are the building blocks for any programming language. Even though we use class to define our custom business logic and apply them, but the most important utility of a class is to store data blocks within the object itself. C# classes are capable of storing data using fields, but they produced one step more abstraction level by introducing the Property System.  Properties are elements that are defined to wrap data members.  Thus anything that we need to expose through the object should be wrapped around using these properties. As a rule we don’t expose any fields as public for a class, rather we create a property and expose that to the outside world, so that the developers can easily impose one level of abstraction by not exposing the actual data.

You can read the entire article from
http://www.dotnetfunda.com/articles/article886-change-notification-for-objects-and-collections-.aspx



Introduction of Property System


Properties has two methods. Get and Set. Get is called whenever data is fetched using the property, whereas Set is called whenever the property value is set. There is an implicit variable value which allows you to grab the data sent from the external world to the property setter. Thus typically an example of property is :

private int _data;

public int Data

{

    get { if (this._data == 0) this._data = 50; return this._data; }

    set

    {

        if (value >= 0)

        this._data = value;

    }

}
Here in the above example you can see, I have wrapped around a property element from the external world using a property. Thus I have also implemented our own custom logic around the data when it is get or set from the property. It is always recommended to expose a data element using property even though you don't have any more to write than get or set the value to private variables. For those which doesn't need to write any custom logic you can use the implicit Property descriptor feature introduced with C# lately which doesn't need you to declare private variable for your property.

public int Data { get; set; }

The above line will work just like private fields, so CLR will automatically generate a private anonymous field for you during run time to store the value of the property.

READ MORE FROM :
http://www.dotnetfunda.com/articles/article886-change-notification-for-objects-and-collections-.aspx

I hope you will like the article.
Shout it Submit this story to DotNetKicks Bookmark and Share
Read Disclaimer Notice

2 comments:

Rajesh Singh said...

Good work Abhishek. Do like the implementation.

Anonymous said...

Hi Abhishek,

I am working on a MVVM SL App and i came across this Problem binding Data to RichTextBox. I am getting data SQL and need to format like para etc.

Any Help is Appretiated

Thanks
Raj

Post a Comment

Please make sure that the question you ask is somehow related to the post you choose. Otherwise you post your general question in Forum section.

Author's new book

Abhishek authored one of the best selling book of .NET. It covers ASP.NET, WPF, Windows 8, Threading, Memory Management, Internals, Visual Studio, HTML5, JQuery and many more...
Grab it now !!!