Posted by: rcosic | 23/10/2008

ORM (NHibernate)

“We hire people who build doghouses, give them cranes and ask them to build skyscrapers. Then we’re surprised when they fail.
– Eileen Steets Quann

Some may ask why I’m doing this lammentations about such an obscure topic. Well, it’s all about sharping tools for building skyscrapers…

NHibernate is a object/relational persistance and query engine adapted for .NET (Hibernate as its predecessor). It lets you develop classes through association, inheritance, polymorphism, composition, and collections. Hibernate allows you to write queries in its proprietary SQL extension (HQL), as well as in native SQL, or with an object-oriented Criteria and Example API. It is open source solution.

The creators also commonly advocate using of TDD (Test Driven Development) with NAnt and DDD (Domain Driven Design). Although NHibernate can also be used to generate the domain model by starting from an existing database schema you can also start from the opposite direction and start with a domain model first and let NHibernate generate the database schema from the model (and the mapping meta data).

Since NHibernate is an open source project anybody has free access to the binaries and/or source code. The source code is stored in a SVN repository (Subversion Source Control System). The most current source code is called with a funny buzzword – the “trunk”.

An instance of an entity in the domain corresponds to a row in a table in the database. We have to define a mapping between the entity and the corresponding table in the database. This mapping can be done either by defining a mapping file (an xml-document) or by decorating the entity with attributes (as declarative security in .NET).

Mapping file has to be defined as .hbm.xml. Please note the “hbm” part of the file name. This is a convention used by NHibernate to automatically recognize the file as a mapping file (which also builds as an “Embedded Resource”).

NHibernate doesn’t get in your way such as that it defines many reasonable defaults. So if you don’t provide a column name for a property explicitly it will name the column according to the property. Or NHibernate can automatically infer the name of the table or the type of the column from the class definition. As a consequence my xml mapping file is not “cluttered” with redundant information.

We also have to tell NHibernate which database product we want to use and provide it the connection details in form of a connection string. This is done by adding xml-based configuration file (hibernate.cfg.xml).

To summarize, “NHibernate’s primary feature is mapping from .NET classes to database tables (and from CLR data types to SQL data types). NHibernate also provides data query and retrieval facilities. NHibernate generates the SQL commands and relieves the developer from manual data set handling and object conversion, keeping the application portable to most SQL databases, with database portability delivered at very little performance overhead.” – from the documentation.

NHibernate is primarily tested on Microsoft SQL Server 2000. It is also known to work on these databases:

– Microsoft SQL Server 2005/2000
– Oracle
– Microsoft Access
– Firebird
– PostgreSQL
– DB2 UDB
– MySQL
– SQLite

NHibernate 2.1 is currently under development and will include a provider for Linq. (see my next post)

Continue with the Linq in the next post…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: