Serialization Guidelines

This guideline shows how to use the Opf3 Framework with web services, .NET Remoting and other methods that need serialization (for example: storing a persistent in the ASP.NET ViewState). When using those methods a few guidelines need to be followed to leverage all benefits of the Opf3 Framework.

Usually persistent objects or lists of persistent objects are remoted when working with web services or .NET Remoting. Since the persistent objects are not derived from any special base class it's only important that all properties have a getter and setter method or the IXmlSerializable interface is implemented by the persistent object. This interface (part of the .NET Framework) allows you to do a custom xml serialization.

Further every persistent object (that is remoted) should implement the ISelfContainingObject interface. The following example shows how to implement this interface in a persistent object:

C#
// Persistent class mapping to the "NEWS" table.
[Persistent("NEWS")]
public class News : ISelfContainingObject
{
    private ObjectInfo _objectInfo;

    private int _id;
    private string _title;
    private string _text;
    private DateTime _publishDate;

    // ID property. Mapping to the "ID" field. Identifier.
    [Field("ID", AutoNumber=true, Identifier=true, AllowDBNull=false)]
    public int ID
    {
        get { return _id; }
        set { _id = value; }
    }
    
    // Other bound persistent properties... 
    // They have been removed since not required in the example.

    #region ISelfContainingObject Members

    // Returns an instance of the ObjectInfo object. This instance
    // contains the state of the persistent object and it's concurrency
    // information.
    public ObjectInfo ObjectInfo
    {
        get { return _objectInfo; }
        set { _objectInfo = value; }
    }

    #endregion
}

VB.NET
' Persistent class mapping to the "NEWS" table.
<Persistent("NEWS")> Public Class News
    Implements ISelfContainingObject

    Private m_objectInfo As ObjectInfo

    Private m_id As Integer
    Private m_title As String
    Private m_text As String
    Private m_publishDate As DateTime

    ' ID property. Mapping to the "ID" field. Identifier.
    <Field("ID", AutoNumber:=True, Identifier:=True, AllowDBNull:=False)> _
    Public Property ID() As String
        Get
            Return m_id
        End Get
        Set(ByVal value As String)
            m_id = value
        End Set
    End Property

    ' Other bound persistent properties... 
    ' They have been removed since not required in the example.

    ' Returns an instance of the ObjectInfo object. This instance
    ' contains the state of the persistent object and it´s concurrency
    ' information.
    Public Property ObjectInfo() As Chili.Opf3.ObjectInfo _
     Implements Chili.Opf3.ISelfContainingObject.ObjectInfo
        Get
            Return m_objectInfo
        End Get
        Set(ByVal value As Chili.Opf3.ObjectInfo)
            m_objectInfo = value
        End Set
    End Property
    
End Class

What are the benefits of implementing ISelfContainingObject?

The framework usually manages the persistent objects' state in the instance of the ObjectContext used to load or save the persistent. The persistent object "lives" in that instance. Usually this works very fine when working in a single threaded Windows application. The application has one instance of the ObjectContext that manages the whole load/save/delete process. The state management and concurrency system is very transparent to the user.

When serialization (working with web services, .NET Remoting and other) is required you need to implement the ISelfContainingObject interface. It allows the Opf3 Framework to store the state and concurrency information of the persistent object with the object itself. It's like giving the object a bag that contains all data required for the framework (to work with that persistent object). Everytime the framework needs the information it's directly requested from the persistent object's instance rather then searching it in the ObjectContext. The persistent object is independent of any ObjectContext instance.

During seralization the persistent object instance leaves the current AppDomain. It's for the .NET and Opf3 Framework as if the object instance would have been removed. It no longer exists in the current process. When not implementing the ISelfContainingObject interface all associated data (state and concurrency data) is removed, because it's as if the object instance does not exist anymore. By implementing the interface the problem is avoided.

That's everything to make your persistent objects ready for web services, .NET Remoting or other methods that need serialization. After implementing the interface, they are ready to go!

Last edited Nov 24, 2009 at 5:17 PM by davidbrown, version 1

Comments

No comments yet.