Fluent NHibernate – Configuration

Used NHibernate?  Had to use the XML configuration (hibernate.cfg.xml)?  Remember this?

<?xml version="1.0" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
        <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
        <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
        <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
        <property name="connection.connection_string">Server=(local);Initial Catalog=dbname;User Id=user;Password=********</property>

Or maybe you write code like this:

IDictionary<string, string> props = new Dictionary<string, string>();
String connectionString = "Server=server;Database=dbName;User ID=user;Password=passwd";
props.Add("hibernate.dialect", "NHibernate.Dialect.MySQLDialect");
props.Add("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider");
props.Add("hibernate.connection.driver_class", "NHibernate.Driver.MySqlDataDriver");
props.Add("hibernate.connection.connection_string", connectionString);


What if you could do this, instead?

   .Configure( nhibConfig );

Hint: It’s already in the FluentNHibernate trunk!

(At the time of this writing, we have MsSql, SQLite and PostgreSQL. It’s easy to add new configs, so we’ll be adding support for all the ones that NHibernate supports out of the box in the upcoming weeks).


About Chad Myers

Chad Myers is the Director of Development for Dovetail Software, in Austin, TX, where he leads a premiere software team building complex enterprise software products. Chad is a .NET software developer specializing in enterprise software designs and architectures. He has over 12 years of software development experience and a proven track record of Agile, test-driven project leadership using both Microsoft and open source tools. He is a community leader who speaks at the Austin .NET User's Group, the ADNUG Code Camp, and participates in various development communities and open source projects.
This entry was posted in Fluent NHibernate. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • What does the deployment strategy look like here? I have at least 3 different connection strings, which get swapped out by tasks.

  • @Jimmy:

    You can pull the connection string from appsettings (ConnectionString.FromAppSetting()), from the connectionStrings element in the web.config (ConnectionString.FromConnectionStringWithKey), or setting it directly via whatever mean you have (ConnectionString.Is).

  • I would like to see the connection string splitted like so:

    .Configure( nhibConfig );

  • This is really awesome. I need to pick up a .Net project again to try this out!