Click here to Skip to main content
12,700,558 members (33,693 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked

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.


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 
  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" ?>
    <add name="ConnectionString"
         providerName="System.Data.SqlClient" />

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

string dataProvider = 
string connectionString = 
DbProviderFactory factory = DbProviderFactories.GetFactory(dataProvider); 
DbConnection connection = factory.CreateConnection(); 
connection.ConnectionString = connectionString; 
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.


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


About the Author

You may also be interested in...

Comments and Discussions

GeneralParent-Child relationships Pin
trevorde stickman19-Mar-08 11:54
membertrevorde stickman19-Mar-08 11:54 
GeneralRe: Parent-Child relationships Pin
Costas Bakopanos4-Apr-08 0:46
memberCostas Bakopanos4-Apr-08 0:46 Pin
Marc Arbesman19-Mar-08 9:32
memberMarc Arbesman19-Mar-08 9:32 
GeneralRe: Pin
Costas Bakopanos4-Apr-08 0:49
memberCostas Bakopanos4-Apr-08 0:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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
Web02 | 2.8.170118.1 | Last Updated 19 Mar 2008
Article Copyright 2008 by Erase Me Please
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid