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:
="1.0"="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.