Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

partial method OnCreated not called when expected

I've extended my domain class on the silverlight client side in a partial class. Specifically, I've added a RelayCommand property that I will be binding a button to. The RelayCommand property needs to be initialized, and so it would seem that the best place to do that would be in the OnCreated partial method.

However I gather that when the object from the server is materialized on the client side its constructor is not called (which seems totally wrong headed to me!) Since it's constructor is not called, it's not calling the OnCreated Method.

Is there a config or a convention for getting this OnCreated partial method to be called as the objects are materialized?

like image 541
Ralph Shillington Avatar asked Dec 17 '22 06:12

Ralph Shillington


1 Answers

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

You can find more information 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] to replicate the effect of the constructor being called.

However, there is a second issue that may cause you problems. Entities get constructed all the time. For example, any time you call TEntity.GetOriginal a new detached entity is being created and returned from the method. That makes trying to do anything like configuring a RelayCommand a potential performance and stability problem. You are probably better off configuring RelayCommands at the DataService or ViewModel level instead of inside the entity itself.

like image 176
Colin Blair Avatar answered Jan 22 '23 00:01

Colin Blair