Los Techies : Blogs about software and anything tech!

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

Kick It on DotNetKicks.com
Posted Aug 18 2008, 08:02 PM by chadmyers
Filed under:

Comments

bogardj wrote re: Fluent NHibernate - Configuration
on 08-18-2008 10:08 PM

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

chadmyers wrote re: Fluent NHibernate - Configuration
on 08-18-2008 10:16 PM

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

Stefan Lieser wrote re: Fluent NHibernate - Configuration
on 08-19-2008 2:00 AM

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 );

Reflective Perspective - Chris Alcock » The Morning Brew #161 wrote Reflective Perspective - Chris Alcock &raquo; The Morning Brew #161
on 08-19-2008 3:09 AM

Pingback from  Reflective Perspective - Chris Alcock  &raquo; The Morning Brew #161

Dew Drop - August 19, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Drop - August 19, 2008 | Alvin Ashcraft's Morning Dew
on 08-19-2008 8:08 AM

Pingback from  Dew Drop - August 19, 2008 | Alvin Ashcraft's Morning Dew

Joe Ocampo wrote re: Fluent NHibernate - Configuration
on 08-19-2008 8:57 AM

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

links for 2009-06-14 « Praveen’s Blog wrote links for 2009-06-14 &laquo; Praveen&#8217;s Blog
on 06-14-2009 8:10 PM

Pingback from  links for 2009-06-14 « Praveen’s Blog

Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems