# Sunday, September 27, 2009

I like to store my application's configuration in app.config or web.config (depending on the project type) which is pretty standard for .NET applications.  Therefore, I'm not a big fan of ActiveRecord's XmlConfigurationSource.

That said, the InPlaceConfigurationSource is somewhat difficult to puzzle out.  After reading the source code, I was able to bend it to my will.

In particular, I like this syntax well enough:

InPlaceConfigurationSource configuration =
    InPlaceConfigurationSource.Build(DatabaseType.MSSQLServer2005, connectionString);

It lets me setup my connection string and database type and default most everything else.

The "problem" is that I can't then do something clean and simple like this:

configuration.Add("show_sql", true);

Instead I have to do this:

Castle.Core.Configuration.MutableConfiguration oneMutableConfiguration =
  (Castle.Core.Configuration.MutableConfiguration)configuration.GetConfiguration(typeof(ActiveRecordBase));
oneMutableConfiguration.Children.Add(new
  Castle.Core.Configuration.MutableConfiguration("show_sql", true.ToString()));
configuration.Add(typeof(ActiveRecordBase), oneMutableConfiguration);

Which works, but it's not the friendliest API to program against.

Be careful, the "Add" method on InPlaceConfigurationSource is not really "Add".  It is really "Replace".

The other main alternative for using InPlaceConfigurationSource is something like this, which is useful for when I can't just use the defaults:

IDictionary<string, string> properties =
  new System.Collections.Generic.Dictionary<string, string>();
properties.Add("connection.driver_class", "NHibernate.Driver.SqlClientDriver");
properties.Add("connection.provider",
  "NHibernate.Connection.DriverConnectionProvider");
properties.Add("proxyfactory.factory_class",
  "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");

properties.Add("dialect", "NHibernate.Dialect.MsSql2005Dialect");
properties.Add("connection.connection_string", connectionString);

properties.Add("show_sql", true.ToString());

InPlaceConfigurationSource configuration = new InPlaceConfigurationSource();
configuration.Add(typeof(ActiveRecordBase), (IDictionary<string, string>)properties);


This was written and tested against "ActiveRecord 2.0 - August 1st, 2009".