To begin elaborating on frameworks and development decisions for the 59 Days of Code contest, the first thing I wanted to invest in was a good and stable OR/M framework.  Initially I was going to go with the new version of Entity Framework 4.0 with .NET but reading a little more into my options, I am going to go with Fluent NHibernate. If you are unaware, Fluent NHibernate is an excellent OR/M tool that allows you to declare your mappings to your database in a more fluent way through code, instead of tedious XML configuration files.  This makes your life a lot easier.  Plus, you get the benefit of very abstracted code which allows me to easier plugin a test framework (more on that later). To setup and configure Fluent NHibernate, first I created a NHibernate Session object helper:

private static ISessionFactory SessionFactory
{
	get
	{
		if (_sessionFactory == null)
		{
			_sessionFactory = Fluently.Configure()
				.Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey("MyDbConnFromWebConfig")))
				.Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<User>().Where(t => t.Namespace == "Namespace.Data.Entities")
				.Setup(s => s.FindIdentity = property => property.Name == property.DeclaringType.Name + "Id")))
				.BuildSessionFactory();
		}
		return _sessionFactory;
	}
}

public static ISession OpenSession()
{
	return SessionFactory.OpenSession();
}

What this does is basically use a singleton pattern to use NHibernate to map my data class library to my database. For example here is what my User class looks like:

public class User
{
	public virtual long UserId { get; private set; }
	public virtual DateTime CreatedDateTime { get; set; }
	public virtual long CreatedUserId { get; set; }
	public virtual DateTime ModifiedDateTime { get; set; }
	public virtual long ModifiedUserId { get; set; }
	public virtual DateTime LastActivityDateTime { get; set; }
	public virtual int UserRoleId { get; set; }
	public virtual string Username { get; set; }
	public virtual string Email { get; set; }
	public virtual string Password { get; set; }
	public virtual string Firstname { get; set; }
	public virtual string Lastname { get; set; }
	public virtual bool Enabled { get; set; }
}

In addition to that, you can also see I am using AutoMapping and setting up my entity identities by mapping the column to EntityName + “Id” so for users it would be UserId.

Without going too much into my architecture I want to quickly explain how I am using this session factory from my repositories to actually get the data. This part is where NHibernate gets fun. Now say I want to get a user by Username and Password. Now I just do something like the following:

using (ISession session = NHibernateHelper.OpenSession())
{
	User user = session.CreateCriteria<User>().Add(Restrictions.Eq("Username", username)).Add(Restrictions.Eq("Password", password)).UniqueResult<User>();
}

Bam! That’s it. NHibernate will automatically create your mappings and map the results back to your class. A lot of the build in methods they provide are very cool and allow me to fluently wite my queries without doing any plain text SQL or stored procedures.

Next I am looking into caching with NHibernate, as they have hooks into the ASP.NET cache provider and so forth.


COMMENTS / 4 COMMENTS

Fluent NHibernate OR/M…

Thank you for submitting this cool story – Trackback from DotNetShoutout…

DotNetShoutout added these pithy words on Mar 04 10 at 3:41 pm

Fluent NHibernate OR/M…

Thank you for submitting this cool story – Trackback from iAwaaz-News-for-the-People-by-the-People…

iAwaaz-News-for-the-People-by-the-People added these pithy words on Mar 05 10 at 5:25 pm

Fluent NHibernate is not an OR/M Tool.
Fluent NHibernate is one of several ways to configure NHibernate, which is a OR/M tool.

Gustavo Ringel added these pithy words on Mar 05 10 at 7:21 am

You are correct. I just wanted to emphasize the ease of use when using Fluent NHibernate vs. NHibernate by itself and how you can do automatic mapping.

robertschultz added these pithy words on Mar 05 10 at 7:41 am

SPEAK / ADD YOUR COMMENT
Comments are moderated.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Return to Top

59 Days of Code: Fluent NHibernate OR/M

FRESH / LATEST POSTS

TAG / CLOUD

.NET 59 Days of Code Adaptive Payments Annoying API Apple ASP.NET ASP.NET MVC AT&T Bars Blog C# Central Valley Comcast CVBI Dancing Facebook Fireworks Fresno FriendFeed GoDaddy Google Google I/O Hipsters House Music HTML5 Independence Day iPhone Lake Don Pedro Microsoft MVC New Year Resolutions Nightlife Payments PayPal PlasticJungle Programming Ruby Social Networking SoundCloud Tattoos Twitter Venture Capital Videos Wordpress