This is a simple tip, I’ve used the Castle Windsor NHibernate Integration Facility to build up the data access services in one of the projects I’m working on, I also wanted to use the NHibernate SchemaExport utilities to build up the database from scratch, this can be very useful when using SQLite in a testing environment.

Since the facility is configured with XML files, I had the problem on how to access the NHibernate.Cfg.Configuration object; having a look at the source code of the Integration Facility it turns out that the Configuration object are registered in the container too along with the factories (one configuration and one factory for each element declared in the configuration file), an alias name is assigned to each configuration object in the form of: {factory_id}.cfg.

So if you have the following settings:

<facility id="nhibernatefacility" isweb="true"
			 type="Castle.Facilities.NHibernateIntegration.NHibernateFacility,Castle.Facilities.NHibernateIntegration">
	<factory id="MainDatabase" alias="nh.facility.default">
		<settings>
			<item key="connection.provider">NHibernate.Connection.DriverConnectionProvider</item>
			...
		</settings>
		<assemblies>
			<assembly>YourAssembly</assembly>
		</assemblies>
	</factory>
</facility>
You can access and use the configuration objects like this:

[Test, Explicit]
public void CreateDatabaseSchema()
{
	NHibernate.Cfg.Configuration cfg = Container.Resolve<NHibernate.Cfg.Configuration>("MainDatabase.cfg");
	SchemaExport export = new SchemaExport(cfg);
	export.Execute(false, true, false);
}

Related Content