This project is read-only.

ASP.NET Guidelines

This guideline shows how to use the Opf3 Framework in an ASP.NET application. It demonstrates what is important when storing persistent objects in the ASP.NET ViewState or session and what should be considered when creating instances of the ObjectContext.

ObjectContextFactory

An ASP.NET application is a multi threaded web application. The factory class should therefore return for each request a new instance of the ObjectContext. We can use the ObjectContextFactory from guideline "ObjectContextFactory" and modify it to return always new instances of the ObjectContext class:

C#
// Returns fully initialized instances of the ObjectContext.
public static class ObjectContextFactory
{
    // Returns a new fully initialized instance of the ObjectContext.
    public static ObjectContext GetObjectContext()
    {
        // Create a connection to the database.
        ObjectContext context = new ObjectContext(new AccessStorage(@"C:\", "db.mdb"));
        // Set a concurrency manager.
        context.ConcurrencyManager = new Md5ConcurrencyManager();

        return context;
    }
}

VB.NET
' Returns fully initialized instances of the ObjectContext.
Public Class ObjectContextFactory

    ' Returns a new fully initialized instance of the ObjectContext.
    Public Shared Function GetObjectContext() As ObjectContext
        ' Create a connection to the database.
        Dim context = New ObjectContext(New AccessStorage("C:\", "db.mdb"))
        ' Set a concurrency manager.
        context.ConcurrencyManager = New Md5ConcurrencyManager()
        Return context
    End Function

End Class

The factory is ready to use in a web application.

Implementing ISelfContainingObject

All persistent objects of the web application should now implement the ISelfContainingObject. This interface is required if an application is multi threaded and/or the persistent objects are loaded and saved with different instances of the ObjectContext class. It is also always implemented when persistents are serialized or leave the current AppDomain. For further information see the documentation of the interface or the guideline on webservices and .NET remoting.

This interface makes sure that the persistent object holds its state and concurrency information (as part of the type definition). Each time the persistent is serialized or leaves the AppDomain the concurrency and state information is also serialized, as it is part of the object. The following sample shows how to implement the interface in a persistent:

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

After implementing the interface the persistent objects can be stored savely in the ASP.NET ViewState or session.

C#
// Get the user with the ID 1.
User user = context.GetObject<User>("ID = {0}", 1);
// Store the object in the ASP.NET ViewState of the current page.
this.ViewState["user"] = user;

VB.NET
' Get the user with the ID 1.
Dim user = context.GetObject(Of User)("ID = {0}", 1)
' Store the object in the ASP.NET ViewState of the current page.
Me.ViewState.Add("user", user)

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

Comments

No comments yet.