Why is Initialize not called from OnDeserializedHandler?

Why is Initialize not called from OnDeserializedHandler?

Old forum URL: forums.lhotka.net/forums/t/6777.aspx


JoshL posted on Sunday, April 12, 2009

According to the code comment, Csla.Core.Initialize "allows subclass to initialize events before any other activity occurs". It is called from the constructor of BusinessBase, before any initialization of business rules or authorization rules. However, it is not called on deserialize of the object. Why is this?

RockfordLhotka replied on Sunday, April 12, 2009

You should override OnDeserialized() to do any event rehooking and so forth.

JoshL replied on Monday, April 13, 2009

I understand that, but don't understand what the point of Initialize is in that case.

Implementing OnDeserliazed for the same functionality as Initialize is inconsistent: Initialize runs _before_ CSLA's initialization on constructor, but OnDeserialized runs _after_ CSLA's initialization. There is no way to run initialization logic _before_ OnDeserialized.

If I need to override OnDeserialized anyways, why not simply implement the custom event hooking in the constructor of the descendant class, in order to keep the timing consistent?

RockfordLhotka replied on Monday, April 13, 2009

Semantically they aren't necessarily the same.

Initialize() exists so you can initialize elements of your object when it is
first created.

OnDeserialized() exists so you can reset elements of your object after it
has been deserialized.

Some elements of your object may need initialization only. Others may need
to be reset on deserialization only. Others may need both.

So I don't necessarily disagree with the concept of an "OnDeserializing()"
method - though it would still technically run after deserialization is
complete (because we don't control that - deserialization is purely a .NET
thing). But this is still not the same semantic operation as initializing a
new object instance.

What is your scenario where you need to reset elements of your object after
deserialization, but before CSLA restores values like the Parent property,
etc?

Rocky

Copyright (c) Marimer LLC