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.
Good work Abhishek. Do like the implementation.
ReplyDeleteHi Abhishek,
ReplyDeleteI 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