I was going to use the NHibernate facility provided by Castle Windsor until I found out it didn't support using class mapping attributes. I ended up rolling my own implementation but added a couple new features to my implementation:
- Automatic context aware session container (web context or thread local).
- Ability to use NHibernate mapping attributes instead of hbm.xml files.
- Can use the built-in .NET 2.0 connectionStrings element to specify an NHibernate connection string.
I think the first two items are pretty obvious, but the last feature I added because most standard .NET applications make use of these elements - for better or worse. I think its a good idea to be consistent if possible. Additionally this would allow any standard .NET administration tool to examine and edit the connection string for the application.
I also wanted the ability to use a custom configuration section so that I could add additional elements and attributes as needed. The configuration section handler produces a SessionFactoryConfiguration instance, which itself it a subclass of NHibernate.Cfg.Configuration. The configuration section looks like this:
<
sessionManager>
<
settings>
<
setting name="
hibernate.dialect">NHibernate.Dialect.MsSql2000Dialect</
setting>
<
setting name="
hibernate.connection.driver_class">NHibernate.Driver.SqlClientDriver</
setting>
<setting name="dotnet.connection_string">Laptop</setting> <
setting name="
hibernate.connection.provider">
NHibernate.Connection.DriverConnectionProvider
</
setting>
</
settings>
<
resources>
<
resource assembly="
Sneal.Northwind.Core"
useAttributes="
true"/>
</
resources>
</
sessionManager>
Notice the setting
dotnet.connection_string. This is the custom hook I added to my custom configuration handler that resolves
Laptop to the .NET connection string named
Laptop:
<
connectionStrings>
<
add name="
Laptop"
connectionString="
Database=Northwind; Data Source=.\sqldev2000;" />
</
connectionStrings>
Then I just wire up my SessionManager instance to my Repository instance using an IoC container; currently Windsor. I have an HttpModule which handles my Unit of Work currently, but unfortunately its tied to Windsor and my current application, so now I'm off to decouple it for reuse.