4

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?

iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
Ralph Shillington
  • 20,718
  • 23
  • 91
  • 154
  • That's why I don't bind directly to RIA Services' generated classes, but rather create an independent ViewModel over them. Anyway, that's a good question, I'd like to know the answer too. – Pavel Gatilov Dec 06 '11 at 06:20
  • @PavelGatilov I hear you loud and clear. It was my (mistaken) understanding that the whole idea behind how the classes were generated (made partial, including OnCreated etc) was for the sole purpose of making them ultimately suitable for the client, particularly binding - but nonsense like this makes me wonder. – Ralph Shillington Dec 06 '11 at 11:52

2 Answers2

7

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.

Colin Blair
  • 270
  • 1
  • 3
  • Thanks Colin. The only reason that I needed (rather wanted) a RelayCommand on my entity was to make binding buttons found in a datagrid row easier set up. Not a big deal, now my buttons bind to a relaycommand of the ViewModel, and pass the row as a command parameter - which is likely what should have been done in the first place! Now how do I mark both your answer and @jehof as correct? – Ralph Shillington Dec 06 '11 at 15:42
  • Nah, leave it on @jehof. I forgot about the OnLoaded method and that was a much better answer to the original question. – Colin Blair Dec 06 '11 at 15:53
5

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

If you want to initialize your loaded entities you have to override the OnLoaded method. A boolean is passed to it that specifies if the entity was loaded first time or not.

Jehof
  • 34,674
  • 10
  • 123
  • 155
  • I like this answer, although I've heard from another channel that what I'm trying to do is ill-advised regardless. I remain to be convinced. (Colin?) +1 for the answer and stay tuned for the checked. – Ralph Shillington Dec 06 '11 at 15:28
  • you get the vote becuase you answer is closest to the question, but @colin bigger picture is the path I'm now on. – Ralph Shillington Dec 06 '11 at 15:45