Using OpenAccess Forward Mapping really allows you to focus on building a strong model, rather than getting caught up in the persistence details, and this can expedite development time dramatically. Every developer I know could use a little more time to focus on more important things :) Before we can use the OpenAccess Forward Mapping Wizard, we need to create a model for our application.  After we have a solid model, we can run the wizard, and it will create the database for us! Awesome!

Getting Started

The first thing that I do is create an abstract base class that all business objects in my library will inherit. 

I like setting it up this way for a few reasons:

  • It keeps my persistence information out of my models. If down the road I need to change my ID to a Guid, i can easily do that without making a change in every class.
  • I like to have a base object that allows me to easily come back and add functionality into all my objects if it is needed down the road. 



   2: public abstract class PersistedEntity : IPerstistable
   3:     {
   4: protected int id;
   5: protected DateTime createdOn;
   6: protected DateTime lastEditedOn;
   8: public int ID
   9:         {
  10:             get { return id; }
  11:         }
  13: public DateTime CreatedOn
  14:         {
  15:             get { return createdOn; }
  16:             set { createdOn = value; }
  17:         }
  19: public DateTime LastEditedOn
  20:         {
  21:             get { return lastEditedOn; }
  22:             set { lastEditedOn = value; }
  23:         }
  24:     }

As you can see here, I have made the fields “protected” this is so that it can easily work with any inheritance structure i choose to use in OpenAccess. 


Adding Functionality

Now I will create a few interfaces to add behavior to my models. I only have two behaviors to worry about in these models:
  1. Handling alerts for an object. The goal is for an object to be assigned multiple alerts.  Ex: We want to set an alert for 4 years down the road on an asset to look into decommissioning it, but also we need to remember to renew the warranty on it sooner than that.
  2. Handling Notes for an object. This will hold misc information for an object. 


This interface will set up the behavior regarding notes in the classes that implement it.

   1: public interface INotable
   2:    {
   3:        IList<Note> Notes
   4:        {
   5:            get;
   6:        }
   7:    }

Next, I need to create a way for my models to have alerts added to them.

   1: public interface IAlertable : IPerstistable
   2:    {
   3:        IList<Alert> Alerts
   4:        {
   5:            get;
   6:        }
   7:    }

Finalizing the Model

After I have these interfaces together, I can go ahead and create models for Asset, AssetGroup, Alert, and Note. 


Here is what it looks like all put together:



Brining In OpenAccess

Now that we have our model together, we can go ahead and open up OpenAccess, and get the persistent storage ready to go.  To do this, open up the Telerik Menu in Visual Studio go to OpenAccess, and launch the wizard.  In here you need to enable the project to use ORM, and then start the forward mapping wizard.  Select the classes you would like to persist, and then click done.   Now build your project and OpenAccess will create the DB for you!  Hurray!


I know those instructions were a little fast, so I urge you to go check out the great walkthrough on Telerik TV.  You can find it here: Forward Mapping with Telerik OpenAccess ORM.


Once we have completed these steps OpenAccess will create a schema for us like the one shown below.



There are a few interesting things to note here:

  • For the Alerts table it has created columns named AlertedItemClass, and AlertedItemID. This is because AlertedItem in our model was of type IAlertable; so when we pull this data out of the database, OpenAccess will automatically create the correct object based on these fields.
  • OpenAccess added a field for version tracking called voa_version.


I hope you enjoyed this blog, and I look forward to hearing any questions or comments.

Next time we will create a simple DAL which will allow our UI to access our data, so check back soon!


Comments are disabled in preview mode.