Partially, the OnCreated method is not called when expected

I have expanded my silverlight client-side domain class in a partial class. In particular, I added the RelayCommand property to which I will bind the button. The RelayCommand property must be initialized, and therefore it seems that the best place for this would be in the OnCreated partial method.

However, I understand that when an object from the server materializes on the client side, its constructor is not called (which seems completely wrong for me!) Since the constructor is not being called, it does not call the OnCreated method.

Is there a configuration or convention for getting this OnCreated partial method that will be called as materialized objects?

+4
source share
2 answers

The OnCreated () partial method is only called when an Entity instance is created using its default constructor.

If you want to initialize loaded objects, you must override the OnLoaded method. A logical value is passed to it, indicating whether the object was loaded for the first time or not.

+5
source

DataContractSerialization does not call the constructor of the objects that it deserializes. This decision was made because with previous serialization methods in .NET, which should always have a default constructor for any object to be serialized, is a problem. This does not apply to RIA Services, it was a design decision taken when creating the WCF itself, and there is no configuration to change it.

Further information can be found at http://blogs.msdn.com/b/carlosfigueira/archive/2011/09/06/wcf-extensibility-serialization-callbacks.aspx , as well as examples of how you can use [OnDeserialized] for constructor effect replications called.

However, there is a second problem that can cause problems. Objects are constantly being created. For example, at any time when you call TEntity.GetOriginal, a newly created object is created and returned from the method. This makes an attempt to do something like tuning RelayCommand to a potential performance and stability issue. You should probably configure RelayCommands at the DataService or ViewModel level, rather than inside the object itself.

+7
source

Source: https://habr.com/ru/post/1384810/


All Articles