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" >
    <session-factory>
        <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>
    </session-factory>
</hibernate-configuration>

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);
config.AddProperties(props);

 

What if you could do this, instead?

MsSqlConfiguration
   .MsSql2005
   .ConnectionString.Is("Server=(local);Database=dbname;uid=user;pwd=password");
   .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).

-Chad

Let’s try it again… testability succinctly