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

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

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.
  • http://jimmybogard.lostechies.com Jimmy Bogard

    What does the deployment strategy look like here? I have at least 3 different connection strings, which get swapped out by tasks.

  • http://chadmyers.lostechies.com Chad Myers

    @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).

  • http://lieser-online.de Stefan Lieser

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

    MsSqlConfiguration
    .MsSql2005
    .Connection
    .Server(“(local)”)
    .Database(“dbname”)
    .Username(“user”)
    .Password(“password”)
    .ToConnectionString()
    .Configure( nhibConfig );

  • http://www.lostechies.com/blogs/joe_ocampo/ Joe Ocampo

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