Monday, May 21, 2007

Object Relational Mapping

Object Centric VS Data Centric

A data centric application is one that relies primarily on stored procedures and manually created SQL statements to display and manipulate data.

An object centric application uses objects to obtain and manipulate data. The goal of a object centric application is to allow software developers to create true Object Orientated code. How can one achieve this goal with a relational database management system (RDBMS)? This goal can obtained through the use of Object Relational Mapping (ORM).

What is Object Relational Mapping?

Object relational mapping is used to map database tables to objects. In its simplest form one database table can be mapped to one object. An object relational mapping framework would provide the mechanism to create, retrieve, update and delete data from the database using objects. Therefore software developers would have to write very little SQL. Combined with a strong framework one can achieve remarkable results with very little code.

How is data mapped to an object?

Data can be mapped to an object using either an XML file or attributes. Using an XML file one would map object properties to database fields. The XML file acts as a layer between the database and the Object. The XML file contains information about the object and the database table that it is associated to. The Object and the database table are not aware of this XML file. The ORM uses this XML file to map data from the database to the object.



Attributes on the other hand are embedded in the object. These attributes are placed above each property that is mapped to a database field.

How to get started?

The best way to learn any new technology is to read a tutorial. My ORM of choice is NHibernate. NHibernate is a .Net Framework port of Java's Hibernate. NHibernate in my opinion is by far one of the best ORM's available for .Net. Unfortunately in my experience there are not too many in depth tutorials when it comes to NHibernate. These tutorials often miss covering the basic type of relationships that exist in a database:

  • All fields on a table typically map to value types.
  • Many to one (An instance of an object)
  • One to many (A collection)
  • Many to many(A Collection).
As a result I have decided to write my own NHibernate tutorial. I will be posting this tutorial shortly.

NHibernate provides the following features:
  • Allows for more Object Oriented code.
  • Caching
    • NHibernate provides a first and second level cache. Please check out my post on the first and second level cache for a more in depth explanation.
  • Generics
    • .Net Generics are supported.
  • Query data using objects.
    • Several classes are provided for creating SQL queries via objects.
  • Lazy instantiation
    • lazy instantiation allows data on a object to only be retireved when it is requested. For a example a collection on a object would only be retrieved from the database when it is requested.
  • XML files or Attributes can be used for mappings.
The Future of ORM

.Net framework 3.0 contains an array of new programming techniques. A most pleasant surprise is the introduction of ADO.Net Entity Framework, Entity Data Model and Language Integrated Query(LINQ). The addition of these new technologies may ultimately replace third party ORM's. Using these new technologies provides programmers with even more benefits. To list a few these benefits include intellisense, IDE and compile time support.

For example, check out the following query against a database using LINQ.

// establish a query context over ADO.NET sql connection
DataContext context = new DataContext(
"Initial Catalog=petdb;Integrated Security=sspi");

// grab variables that represent the remote tables that
// correspond to the Person and Order CLR types
Table custs = context.GetTable();
Table orders = context.GetTable();

// build the query
var query = from c in custs, o in orders
where o.Customer == c.Name
select new {
c.Name,
o.OrderID,
o.Amount,
c.Age
};

// execute the query
foreach (var item in query)
Console.WriteLine("{0} {1} {2} {3}",
item.Name, item.OrderID,
item.Amount, item.Age);

The following example uses LINQ against an array
 string[] names = { "Burke", "Connor", "Frank",
"Everett", "Albert", "George",
"Harris", "David" };

IEnumerable expr = from s in names
where s.Length == 5
orderby s
select s.ToUpper();

foreach (string item in expr)
Console.WriteLine(item);

If your looking for information check out the following articles:

The LINQ Project Overview
ADO.NET Entity Framework Overview
Entity Data Model Overview


A Final Note


In this day and age it is important to keep up with each and every new technology. The landscape is ever changing and will never remain the same for long. New programming techniques, design patterns, languages and technologies are emerging at faster and faster rates. In order to stay ahead it is important to take a step back every once a while and absorb these new best practices and technologies.

No comments: