Complex Object Graph Serialization/NotUndoable Workaround Solution

Complex Object Graph Serialization/NotUndoable Workaround Solution

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


bwebber posted on Tuesday, October 04, 2011

Hi

I have a relatively complex CSLA 4 object graph and would like to know if my solution around the BeginEdit / Serialization issue is correct.  This is a Silverlight project.

Object Graph:

The problem here is that the child objects re-reference objects in the parent list.  The OrganogramList class fetches all required data and builds the entire object graph on the server side.  It also links the required ChildOrganogram object to the appropriate Organogram object.

Before (using older CSLA and WinForms), I would have placed a NotUndoable tag onto the Organogram reference in my ChildOrganogram class, but with the static PropertyInfo declarations this tag is no longer valid, and the serialization no longer works on member variables.

My workaround is messy, so I was hoping there is a cleaner solution:

My property declaration is as follows:

    Public Shared ChildOrganogramProperty As PropertyInfo(Of Organogram) = RegisterProperty(Of Organogram)(Function(c) c.ChildOrganogram, "Child Organogram")

     Public Sub SetChildOrganogram(ByVal value As Organogram)

       LoadProperty(ChildOrganogramProperty, value)

     End Sub

     <NotUndoable()> Private mChildOrganogram As Organogram

     Public ReadOnly Property ChildOrganogram() As Organogram

      Get

        Return mChildOrganogram

      End Get

    End Property

 

My OrganogramList calls SetChildOrganogram during its Fetch to link the ChildOrganogram objects to their correct Organogram.  It does the same after an Update.  The DataPortal correctly serializes this property.  Then on the Silverlight side, to avoid Edit Level mismatches, I have overriden the OnDeserialize as follows:

    Protected Overrides Sub OnDeserialized(ByVal context As System.Runtime.Serialization.StreamingContext)

      MyBase.OnDeserialized(context)

      ' Reinitialise the child organogram

      mChildOrganogram = GetProperty(ChildOrganogramProperty)

      LoadProperty(ChildOrganogramProperty, Nothing)

    End Sub

While this is working, I am not convinced it is the best solution.  I am also aware that I am sending multiple copies of the exact same object across the line (though this is not a major concern).

Any advice would be appreciated.  Feel free to respond in C# ;)

Thanks in advance!

RockfordLhotka replied on Tuesday, October 04, 2011

The recommended solution is to use private backing fields for properties that must be marked NonSerialized or NotUndoable.

If you do this in Silverlight/WP7, you must also override OnGetState and OnSetState to serialize/deserialize the value - assuming you want it serialized. In your case you do not :)

But then you probably need to override OnDeserialized to restore the reference? And in that case, in your OnGetState/OnSetState you might pass some other identifying value (like the object's primary key?) through the serialization stream so you can use it in OnDeserialized to restore the reference.

bwebber replied on Thursday, October 06, 2011

Hi Rocky

Thank you for your prompt response.

If found that I did not need the OnGetState and OnSetState as I already had the primary key value as one of my properties.  My code is now the following which is much neater:

 

  <NotUndoable()> Private mChildOrganogram As Organogram

 

    Public Property ChildOrganogram() As Organogram

      Get

        Return mChildOrganogram

      End Get

    End Property

 

    Protected Overrides Sub OnDeserialized(ByVal context As System.Runtime.Serialization.StreamingContext)

      MyBase.OnDeserialized(context)

      ' Reinitialise the child organogram

      mChildOrganogram = CType(CType(CType(Me.Parent, OrganogramChildOrganogramList).Parent, Organogram).Parent, OrganogramList).GetItem(Me.OrganogramIDChild)

    End Sub

This is working fine.

Thanks for the help.

 

Copyright (c) Marimer LLC