Simple Opf3 is very simple to use. It's no work to set up the framework and load or save an object from/to a database. It is also very simple to mark an object as a persistent. A persistent is an object (type) that can be loaded and stored in the database. To explain the simplicity better we should make an example. The following is our table (named USER) in a Microsoft Office Access database. | ID [long] | NAME [varchar(50)] | | 1 | Alex James | | 2 | John Carpender | The table contains two entries. One is Alex James and the other is John Carpender. Now we need to map the schema of the table to our persistent class. First we create a simple class that contains two properties: One ID and the other Name. | C# VB.NET | // Our class containing the ID and Name property. public class User { private long _id; private string _name; // The ID property. This one should match the ID field. public long ID { get { return _id; } set { _id = value; } } // The Name property. This one should match the NAME field. public string Name { get { return _name; } set { _name = value; } } } ' Our class containing the ID and Name property. Public Class Order Private m_id As Long Private m_name As String ' The ID property. This one should match the ID field. Public Property ID() As String Get Return m_id End Get Set (ByVal value As String) m_id = value End Set End Property ' The Name property. This one should match the NAME field. Public Property Name() As String Get Return m_name End Get Set (ByVal value As String) m_name = value End Set End Property End Class | Until now we had nothing to do with Opf3. It is now time to bind the object with the fields in the table. Opf3 uses the straight forward way of using .NET attributes for the binding. You need only two attributes: The PersistentAttribute to mark the object as persistent (bind it to the table) and the FieldAttribute to bind a property to a field. That's it! Let's do it now. | C# VB.NET | // Our class containing the ID and Name property. [Persistent("USER")] public class User { private long _id; private string _name; // The ID property. This one should match the ID field. [Field("ID", Identifier = true, AllowDBNull = false)] public long ID { get { return _id; } set { _id = value; } } // The Name property. This one should match the NAME field. [Field("NAME")] public string Name { get { return _name; } set { _name = value; } } } ' Our class containing the ID and Name property. <Persistent("USER")> Public Class Order Private m_id As Long Private m_name As String ' The ID property. This one should match the ID field. <Field("ID", Identifier := True, AllowDBNull := False)> Public Property ID() As Long Get Return m_id End Get Set (ByVal value As Long) m_id = value End Set End Property ' The Name property. This one should match the NAME field. <Field("NAME")> Public Property Name() As String Get Return m_name End Get Set (ByVal value As String) m_name = value End Set End Property End Class | In one of the FieldAttributes we set also the Identifier and AllowDBNull properties. Those attribute's properties are used to mark the bound property as primary key. The persistent is now ready! Next we need to set up Opf3. That is done in two lines of code. First we need a storage (is the Opf3 representation of a database) and an instance of the ObjectContext. That's all. Let's do it. | C# VB.NET | // Get ourselfs a storage. This storage is initialized with the path and the file name // of the database file. AccessStorage storage = new AccessStorage("C:\\temp", "database.mdb"); ObjectContext context = new ObjectContext(storage); ' Get ourselfs a storage. This storage is initialized with the path and the file name ' of the database file. Dim storage As New AccessStorage("C:\temp", "database.mdb") Dim context As New ObjectContext(storage) | Opf3 is set up! We are ready to load our first object. | C# VB.NET | // Load the object with the ID 1. User user = context.GetObject<User>("ID = 1"); ' Load the object with the ID 1. Dim user As User = context.GetObject(Of User)("ID = 1") | The framework allows you to use a simple query language to load objects from the database. This language is a subset of OPath. For complexer queries you can write an ObjectSearcher or use direct SQL. The dialect allows using the properties of the persistent object: ID is here the property of the persistent object not the name of the database's field! That's it. We loaded the first object from the database. You can now work with the object as you work with all other objects in .NET. For more information about how to use the Opf3 Framework click here. |