In a previous post on using ActiveRecord, I chose to use the app.config file to store the configuration and load it. The following three examples cover the existing method and two alternatives.
1. The first method of configuration was achieved by adding a configuration section and the necessary information, and initialising the framework with an instance of the ActiveRecordSectionHandler.
[App.Config]
<configSections>
<section name="activerecord"
type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<activerecord>
<config>
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2005Dialect"/>
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
<add key="hibernate.connection.connection_string" value="Server=SERVER;initial catalog=TelephoneBook;Integrated Security=SSPI"/>
<add key="hibernate.show_sql" value="true"/>
</config>
</activerecord>
UPDATE: 6 JUN 2008: The configuration section has changed slightly for ActiveRecord - I've noticed it from about build 1.0.3.4719 onwards (may have been earlier, but I haven't downloaded the trunk for a while). Here is an updated config section.
UPDATE: 9 JUN 2008: Had a good look this weekend - it's part of the NHibernate 2.0 release
<activerecord>
<config>
<add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect"/>
<add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
<add key="connection.connection_string" value="Server=SERVER;initial catalog=TelephoneBook;Integrated Security=SSPI"/>
<add key="show_sql" value="true"/>
</config>
</activerecord>
[Code]
ActiveRecordStarter.Initialize(ActiveRecordSectionHandler.Instance, typeof(Contact), typeof(TelephoneNumber));
2. This could also have been done entirely in code, making use of the InPlaceConfigurationSource class, as demonstrated below in the second exapmle. This may be useful if you're allowing a user to choose a connection from a list.
Hashtable properties = new Hashtable();
properties.Add("hibernate.connection.driver_class", "NHibernate.Driver.SqlClientDriver");
properties.Add("hibernate.dialect", "NHibernate.Dialect.MsSql2000Dialect");
properties.Add("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider");
properties.Add("hibernate.connection.connection_string", "Data Source=SERVER;Initial Catalog=TelephoneBook;Integrated Security=SSPI");
InPlaceConfigurationSource source = new InPlaceConfigurationSource();
source.Add(typeof(ActiveRecordBase), properties);
ActiveRecordStarter.Initialize(source, typeof(Contact), typeof(TelephoneNumber));
3. A third method of configuration is to store the information in a seperate XML file (or CONFIG file, your choice) and load it up from there. An example of the XML file could look like this, which as you may notice is the same as the section from the app.config file.
<?xml version="1.0" encoding="utf-8"?>
<activerecord>
<config>
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2005Dialect" />
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="hibernate.connection.connection_string" value="Data Source=SERVER;Initial Catalog=TelephoneBook;Integrated Security=SSPI" />
</config>
</activerecord>
In code, you would make use of the XmlConfigurationSource class to load the XML file, and the call the Initialize method.
XmlConfigurationSource source = new XmlConfigurationSource("activerecord.xml");
ActiveRecordStarter.Initialize(source, typeof(Contact), typeof(TelephoneNumber));
UPDATE 25 JUNE 2008: I've added another post on more advanced configuration of ActiveRecord
here.