Click here to Skip to main content
Click here to Skip to main content

Simple Object-Relational Mapping

, 19 Mar 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
A very simple ORM class. It doesn't have all the functionality that others have, but it might be an easy solution for smaller things.

Introduction

What I have here is a very simple ORM class. It doesn't have all the functionality that others have, but it might be an easy solution for smaller things.

If you need to look into more serious ORM frameworks, then take a look at Spring.Net and NHibernate, LINQ to SQL, and LINQ to Entities, or many others.

Don't look at the Data Access Application block because it does no object-relational mapping, it's simply a factory to abstract ADO.NET functionality.

Using the code

First of all, you need the DataMapper class (attachment in this article). I use ideas in that taken from the CSLA .NET framework.

Next, create a business object:

using System; 
namespace ASPNET.StarterKit.Portal.BusinessLayer.BusinessObjects 
{ 
  [Serializable] 
  public class PortalHtmlText 
  { 
  private int _ModuleID; 
  private string _DesktopHtml; 
  private string _MobileSummary;
  private string _MobileDetails; 
  public int ModuleID 
  { 
    get { return _ModuleID; } 
    set { _ModuleID = value; } 
  } 
  public string DesktopHtml 
  { 
    get { return _DesktopHtml; } 
    set { _DesktopHtml = value; } 
  } 
  public string MobileSummary 
  { 
    get { return _MobileSummary; } 
    set { _MobileSummary = value; } 
  } 
  public string MobileDetails 
  { 
    get { return _MobileDetails; } 
    set { _MobileDetails = value; } 
  } 
  } 
}

Create a table in the database:

CREATE TABLE [dbo].[Portal_HtmlText]( 
  [ModuleID] [int] NOT NULL, 
  [DesktopHtml] [ntext] NOT NULL, 
  [MobileSummary] [ntext] NOT NULL,
  [MobileDetails] [ntext] NOT NULL 
)

We will need a connection string similar to this one:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="ConnectionString"
         connectionString="server=.\SQLEXPRESS;
                  Trusted_Connection=true;database=Portal"
         providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

Finally, let's get all the rows of the table and do the mapping:

string dataProvider = 
  ConfigurationManager.ConnectionStrings["ConnectionString"].ProviderName; 
string connectionString = 
  ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 
DbProviderFactory factory = DbProviderFactories.GetFactory(dataProvider); 
DbConnection connection = factory.CreateConnection(); 
connection.ConnectionString = connectionString; 
connection.Open(); 
DbCommand command = factory.CreateCommand(); 
command.Connection = connection; 
command.CommandType = CommandType.Text; 
command.CommandText = "SELECT * FROM Portal_HtmlText"; 
IDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection); 
IList<PortalHtmlText> list = DataMapper.MapList<PortalHtmlText>(reader); 

If you analyze the DataMapper class, you will easily understand that the data reader should return the same column names as the business object has. I know very well that this is a hard assumption, but if you can make it, then you can easily map database objects into business objects. After profiling with AQTime and DotTrace, I found out that the mapping takes around 3% of the total operation, the rest are database related. In addition, where you can really gain performance is by using datareaders compared to datasets. Using ORM, you need to transfer a business object to the other end of the wire, not a disconnected dataset. So, I believe that, in general, you will gain performance.

Another worth to mention point is what happens in case a column that exists in the data reader doesn't exist in the business object as a public property. In such a case, an exception is thrown, and with good unit testing, you should detect the deficiency early enough.

For more examples, take a look at this link where I use this DataMapper extensively.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


Comments and Discussions

 
GeneralParent-Child relationships Pinmembertrevorde stickman19-Mar-08 11:54 
GeneralRe: Parent-Child relationships PinmemberCostas Bakopanos4-Apr-08 0:46 
GeneralEntitySpaces.net PinmemberMarc Arbesman19-Mar-08 9:32 
GeneralRe: EntitySpaces.net PinmemberCostas Bakopanos4-Apr-08 0:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 19 Mar 2008
Article Copyright 2008 by Erase Me Please
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid