Create an Observable Object
When it comes to making an observable class, it is important to take into account some considerations that can facilitate obtaining the desired behavior. This guide presents the principal aspects to create observable classes.
How to indicate a class is observable Class
To create an observable class, first of all, you need to add the Observable
attribute.
Classes Observations
An observable class can have
private
,internal
orpublic
modifier.An observable class can be
sealed
.An observable class can be
abstract
.When you add
Observable
attribute to a class. All classes that inherit from that class will be observables. Even if the attribute is not present in one of those classes.
How to add a Property that can notify changes in an observable class
To add a property that you want it to notify changes, you need to declare it as an auto implemented property with the attribute Intercepted
When declaring an intercepted property, consider the following recommendations:
Properties can be virtual or non virtual.
Properties cannot be abstract.
Properties can be
public
,private
,internal
orprotected
.Properties must be auto implemented
Property type can be any value type or string, or any type marked as an observable.
Properties can be static
Properties Observations
Remember: Fields and events can not be intercepted. So, state in classes that needs to notify changes must be declared as properties.
You can use non auto implemented properties, but you will not be allowed to add Intercepted
attribute.
How to properly declare an Event in an observable class
To create an Event in your observable class. You must declare a Property which will have a delegate as the return type. Been a property, you need to declare it as an auto implemented property with the attribute Intercepted
.
With the aim that the event can notify their changes, it is necessary to declare it in the aforementioned way. If instead of property it is declared as an event, the operation will not be as expected, since the changes will not be notified and the stored value will be lost. What may affect the reception of invocations to the event as part of a new request from the client.
How to indicate a static observable class
Just as one can declare an instance class as observable, it can also be done with static classes.
How to use static members
Regardless of whether the class is static or not, we can use static members
Constructors
A static constructor can be used in an observable class. When the static constructor is inside an observable class it will be executed once per session*.
When a static constructor is inside a class without a Observable
attribute, it will be executed only once and it will affect the application widely.
Properties
If you use static properties without intercepted
attribute, it will have a different behavior, so you must be careful. In this case, the static properties state will behave as wide application state.
Methods
If you have a class with only static methods, that is, without properties or constructors. It is not necessary to declare the static class as observable.
How to declare Structs
Structs cannot be observable, so, a struct should be converted to an intercepted class. See the following example:
For the following input Struct.
You must convert it to a class as the following code represents:
Example:
Last updated