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.
- BROWSE / IN TIMELINE
- « 59 Days of Code
- » Google Fiber for Fresno
- BROWSE / IN Development Technology
- « 59 Days of Code
- » Google Fiber for Fresno
COMMENTS / 4 COMMENTS
DotNetShoutout added these pithy words on Mar 04 10 at 3:41 pmFluent NHibernate OR/M…
Thank you for submitting this cool story – Trackback from DotNetShoutout…
iAwaaz-News-for-the-People-by-the-People added these pithy words on Mar 05 10 at 5:25 pmFluent NHibernate OR/M…
Thank you for submitting this cool story – Trackback from iAwaaz-News-for-the-People-by-the-People…
Gustavo Ringel added these pithy words on Mar 05 10 at 7:21 amFluent NHibernate is not an OR/M Tool.
Fluent NHibernate is one of several ways to configure NHibernate, which is a OR/M tool.
robertschultz added these pithy words on Mar 05 10 at 7:41 amYou 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.
SPEAK / ADD YOUR COMMENT
Comments are moderated.

