Click here to Skip to main content
15,886,026 members
Articles / Programming Languages / SQL
Article

Simple Object-Relational Mapping

Rate me:
Please Sign up or sign in to vote.
1.68/5 (5 votes)
19 Mar 2008CPOL2 min read 24.3K   233   22   4
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:

C#
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:

SQL
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
<?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:

C#
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)



Comments and Discussions

 
GeneralParent-Child relationships Pin
trevorde stickman19-Mar-08 10:54
trevorde stickman19-Mar-08 10:54 
how would you handle these?

Kind regards
Trevor D'Arcy-Evans
GeneralRe: Parent-Child relationships Pin
Erase Me Please3-Apr-08 23:46
Erase Me Please3-Apr-08 23:46 
GeneralEntitySpaces.net Pin
Marc Arbesman19-Mar-08 8:32
Marc Arbesman19-Mar-08 8:32 
GeneralRe: EntitySpaces.net Pin
Erase Me Please3-Apr-08 23:49
Erase Me Please3-Apr-08 23: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.