CopyState() and Serialization Problem

CopyState() and Serialization Problem

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


cciu posted on Wednesday, June 27, 2007

Hi, all

Recently I encountered a serialization problem when coding my BO using CSLA. There was an event in my BO and I subscribed it to a WinForm. The event was coded in “Serialization Safe” way that it had 2 delegate fields, one for non-serializable subscribers and one for serializable subscribers.

However, when I called the BO’s BeginEdit(), it thrown out a serialization error indicated that the WinForm should be mark [Serializable]. With a little tracing, I found that the error was fired inside UndoableBase.CopyState(), in there, a BinaryFormatter tried to serialize a HybridDictionary that contains my non-serializable field value. The cause was obvious then, after putting the non-serializable field value to a List, its original attribute information was lost and no longer safe.

I was wondering for a moment, why the PropertyChanged() event in BindableBase worked just fine? And soon I found out the state copying only traced back to UndoableBase, which is inherited from BinableBase, so fields from BinableBase are not processed by the copy machine.

 

There is a simple workaround for this, if I mark the field [NotUndoable()], but is it a “Correct” thing to do? And is that mean all classes derived from UndoableBase should mark their [NotSerialized()] fields to [NotUndoable()] as well?

Another workaround is to wrap the [NotSerialized()] fields into a class, then it is a wrapper object to be put into the HybridDictionary instead of naked fields, so the information can be preserved, but this involves extra coding, which is certainly undesirable.

 

Could anyone share some insight on this? TIA.

RockfordLhotka replied on Friday, June 29, 2007

Yes, you must mark the fields (both of them) as NotUndoable.

Some of the CSLA framework classes don't need to do this because they sit in the inheritance hierarchy at or above UndoableBase. But anything derived below UndoableBase must mark these fields as NotUndoable to avoid issues.

cciu replied on Friday, June 29, 2007

Got it, now it is clear to me, thanks a lot.

Copyright (c) Marimer LLC