Go Back

Creating a Persistent Object with OpenAccess

In my last blog post I described how to install Telerik's OpenAccess ORM Express and create a new OpenAccess-enabled Visual Studio project.  Below we'll use the environment we previously setup to create a persistent object that we'll later use in Sitefinity.

Before proceeding to the steps below you MUST work through all the steps found in my previous blog post.

Create your first Persistent Class Item

To create a new persistent object right-click on your project (MyClass) in Visual Studio's Solution Explorer and click Add -> Add New Item.

add-new-item

For the item type choose Class and type Customer.cs for the class file name.

add-new-item-details

Define your Object

We now have an empty Customer class and must define the properties that will exist.  Let's start with something really simple:

using System;

namespace MyClass
{
    public class Customer
    {
        private string _firstname;
        private string _lastname;

        public string FirstName
        {
            get
            {
                return _firstname;
            }
            set
            {
                _firstname = value;
            }
        }

        public string LastName
        {
            get
            {
                return _lastname;
            }
            set
            {
                _lastname = value;
            }
        }
    }
}

Our Customer class now has two public properties that can be used to get and set the customer's first & last name: 

Customer cust = new Customer();
cust.FirstName = "Gabe";
cust.LastName = "Sumner";

This will work just fine.  However, the moment the application terminates the object is unloaded from memory and lost.  We are going to use OpenAccess to overcome this by persisting our Customer object in the Sitefinity database.

Persist your Object

To persist our Customer objects we'll use OpenAccess' Forward Mapping feature.  Forward Mapping means that OpenAccess will automatically create database tables for your existing objects.

Forward Mapping: Classes -> Database

OpenAccess also supports Reverse Mapping.  In this scenario, OpenAccess will automatically create classes based on your existing database tables:

Reverse Mapping: Database -> Classes

To persist our newly created Customer class click OpenAccess -> Forward Mapping (Classes to Tables).

forward-mapping

Click Customer from the left-hand tree and check Make the class persistent.

persist-customer

You can specify the database table name and many other details if you wish.  For now, just click Done.  If we look at the Customer.cs file we see OpenAccess inserted a new line:

[Telerik.OpenAccess.Persistent()]

Here is the final result:

using System;

namespace MyClass
{
    [Telerik.OpenAccess.Persistent()]
    public class Customer
    {
        private string _firstname;
        private string _lastname;

        public string FirstName
        {
            get
            {
                return _firstname;
            }
            set
            {
                _firstname = value;
            }
        }

        public string LastName
        {
            get
            {
                return _lastname;
            }
            set
            {
                _lastname = value;
            }
        }
    }
}

OpenAccess also added some mappings to the the App.config file:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="openaccess" type="Telerik.OpenAccess.Config.ConfigSectionHandler, Telerik.OpenAccess.Config, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7ce17eeaf1d59342" />
  </configSections>
  <openaccess xmlns="http://www.telerik.com/OpenAccess">
    <references />
    <connections>
      <connection id="DatabaseConnection1">
        <databasename>Sitefinity</databasename>
        <servername>(LOCAL)\SQLEXPRESS</servername>
        <integratedSecurity>True</integratedSecurity>
        <backendconfigurationname>mssqlConfiguration</backendconfigurationname>
        <connectionParams>User Instance=True;AttachDBFilename=C:\Users\Gabe\website\App_Data\Sitefinity.mdf</connectionParams>
      </connection>
    </connections>
    <backendconfigurations>
      <backendconfiguration id="mssqlConfiguration" backend="mssql">
      <mappingname>mssqlMapping</mappingname>
      </backendconfiguration>
    </backendconfigurations>
    <mappings current="mssqlMapping">
      <mapping id="mssqlMapping"></mapping>
    </mappings>
  </openaccess>
</configuration>

I'm including the XML above as an example.  You should not need to type any of this by hand.  Use the OpenAccess Visual Studio tools to generate your App.config file.

Build the Project

We're now finished.  To build our project click Build -> Build MyClass.

build-myclass

Using our new Persistent Class

Our persistent Customer class is now created and ready to be used.

The next step is to add a reference to our newly created assembly from our Sitefinity web site.  We can then use our persistent Customer class and OpenAccess in our Sitefinity project.  These instructions will be contained in my next post.

Comments  1

  • Ben 16 Dec

    Pretty great stuff I can't wait to convert my Sitefinity modules ;-) But the cool part about Nolics was that you didn't have write standard classes to for persistent classes. You could write just one line and it would generate the private member and the public property.
Post a comment!
  1. Formatting options
       
     
     
     
     
       
  2. I'm sorry for the CAPTCHA. You have spammers to thank for this: