Tuesday 17 February 2009

FluentConfiguration -- New API to configure NHibernate

Fluent NHibernate from the very beginning provides really clean API to configure NHibernate. I didn't expect to see any changes in this area ... but yet new "fluent" way to configure NHibernate has been introduced.

This is the way I was using so far (it still works well):

   1:  private static Configuration GetNHibernateConfig()
   2:  {
   3:      return MsSqlConfiguration.MsSql2005
   4:          .ConnectionString(c => c.Is(@"Data Source=db_server;Database=db_name;...."))
   5:          .UseReflectionOptimizer()
   6:          .ShowSql()
   7:          .ConfigureProperties(new Configuration());
   8:  }
   9:  
  10:  public static ISessionFactory GetSessionFactory()
  11:  {
  12:      // configure nhibernate
  13:      Configuration config = GetNHibernateConfig();
  14:  
  15:      var models = new PersistenceModel();
  16:      
  17:      // alter default conventions if necessary
  18:      SetUpConvention(models.Conventions);
  19:  
  20:      models.addMappingsFromAssembly(typeof (Product).Assembly);
  21:      models.Configure(config);
  22:  
  23:      // save xml files with mappings to some random location
  24:      models.WriteMappingsTo(@"c:\dev\mappings");
  25:  
  26:      // build factory
  27:      return config.BuildSessionFactory();
  28:  }
  29:  

Honestly I didn't expect that readability can be improved much ... but check this:

   1:  public static ISessionFactory GetFluentlyConfiguredSessionFactory()
   2:  {
   3:      return Fluently.Configure()
   4:          .Database(MsSqlConfiguration
   5:                        .MsSql2005
   6:                        .ConnectionString(c => c.Is(@"Data Source=db_server;Database=db_name;....")))
   7:  
   8:          .Mappings(m =>
   9:                    m.FluentMappings.AddFromAssemblyOf<Product>()
  10:                        .ConventionDiscovery.Add(new AdventureWorksConvention())
  11:                        .ExportTo(@"c:\dev\mappings"))
  12:  
  13:          .BuildSessionFactory();
  14:  }

What I really like about Fluent NHibernate is that it doesn't force user to take all or nothing. If you want you can easily use combine different types of mappings. For instance you can add fluent-nh to your existing project, reuse old mappings (XML files) and add new mappings configured with fluent API. Here is an example:

   1:  public static ISessionFactory GetFluentlyConfiguredSessionFactoryWithHbmFiles()
   2:  {
   3:      return Fluently.Configure()
   4:          .Database(MsSqlConfiguration
   5:                        .MsSql2005
   6:                        .ConnectionString(c =>
   7:                                          c.Is(@"Data Source=db_server;Database=db_name;....")))
   8:  
   9:          .Mappings(m =>
  10:                        {
  11:                            m.FluentMappings.AddFromAssemblyOf<Product>()
  12:                                .ConventionDiscovery.Add(new AdventureWorksConvention())
  13:                                .ExportTo(@"c:\dev\mappings");
  14:                            m.HbmMappings.AddFromAssemblyOf<Product>();
  15:                        })
  16:  
  17:          .BuildSessionFactory();
  18:  }
In a similar way it is possible to combine standard fluent-nh mappings with XML files and auto mapping.

I didn't include in this post anything about conventions to keep things short and concise but you can find details in this post.

Related posts:

2 comments:

AlanBarlow said...

Will be sharing this with someone who can understand this. Starting a business start with a custom logo.
custom logo design company

Haris said...

Thank you for sharing it with us.
I am really happy to see this blog.
It is very helpful for me.

Nursing assignment help