Click here to Skip to main content
Licence CPOL
First Posted 19 Mar 2008
Views 12,545
Downloads 69
Bookmarked 18 times

Simple Object-Relational Mapping

By | 19 Mar 2008 | Article
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.
 
Part of The SQL Zone sponsored by
See Also

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)

About the Author

Costas Bakopanos

Architect
PHONAK AG
Switzerland Switzerland

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralParent-Child relationships Pinmembertrevorde stickman10:54 19 Mar '08  
GeneralRe: Parent-Child relationships PinmemberCostas Bakopanos23:46 3 Apr '08  
GeneralEntitySpaces.net PinmemberMarc Arbesman8:32 19 Mar '08  
GeneralRe: EntitySpaces.net PinmemberCostas Bakopanos23:49 3 Apr '08  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 19 Mar 2008
Article Copyright 2008 by Costas Bakopanos
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid