The NHibernate facility provides two levels of integration with NHibernate. You should pick the one you feel more comfortable with.
- You can just receive the
ISessionFactory and Configuration on your components and use it as you wish.
- You can use
ISessionManager component to manage sessions.
Integration level 1
This example illustrates how you can have fine grained control over
ISessionFactory and thus,
ISession.
using NHibernate;
using NHibernate.Cfg;
public class MyDataAccessClass
{
private ISessionFactory _sessFactory;
private Configuration _cfg;
public MyDataAccessClass(ISessionFactory sessFactory, Configuration cfg)
{
_sessFactory = sessFactory;
_cfg = cfg;
}
public void SaveSomething(object item)
{
ISession session = _sessFactory.OpenSession();
// .. do whatever you want
session.Close();
}
}With this scenario you have to control transactions and share sessions instances manually.
Integration level 2
The second level of integration introduce the ISessionManager interface so you can use session and leave to the ISessionManager implementation the responsability of sharing compatible session within an invocation chain and be aware of transactions.
Configuration
The configuration serves two purposes: configure NHibernate and configure the facility for the environment it is running in.
Configuration Schema
<facilities>
<facility id="nhibernate" isWeb="true|false" customStore="typename for a class that implements ISessionStore" type="implementation type of facility">
<factory id="nhibernate.factory">
<settings>
<item key="nhibernate config key 1">value</item>
<item key="nhibernate config key 2">value</item>
</settings>
<resources>
<resource name="hbm.xml file location" />
<resource assembly="assembly name" name="hbm.xml file name" />
</resources>
<assemblies>
<assembly>assembly name</assembly>
</assemblies>
</factory>
</facility>
</facilities>
You can register more than one factory if you are accessing more than one database. In this case, you must provide an alias for the factory:
<facilities>
<facility id="nhibernate" ...>
<factory id="nhibernate.factory">
...
</factory>
<factory id="nhibernate.factory" alias="oracle2">
...
</factory>
</facility>
</facilities>
The alias is used to obtain an ISession instance through ISessionManager. More on that below.
The attribute isWeb allows the facility to switch the implementation for ISessionStore. You can provide your own implementation using the attribute customStore.
Configuration Sample
<configuration>
<configSections>
<section
name="castle"
type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
</configSections>
<castle>
<facilities>
<facility id="nhibernate" type="Castle.Facilities.NHibernateIntegration.NHibernateFacility, Castle.Facilities.NHibernateIntegration">
<factory id="nhibernate.factory">
<settings>
<item key="connection.provider">
NHibernate.Connection.DriverConnectionProvider
</item>
<item key="connection.driver_class">
NHibernate.Driver.MySqlDataDriver
</item>
<item key="connection.connection_string">
Database=minddump;Data Source=localhost
</item>
<item key="dialect">
NHibernate.Dialect.MySQLDialect
</item>
</settings>
<resources>
<resource name="..\bin\Author.hbm.xml" />
<resource name="..\bin\Blog.hbm.xml" />
<resource name="..\bin\Post.hbm.xml" />
</resources>
</factory>
</facility>
</facilities>
</castle>
</configuration>
If you wish to use a connection string from a section of your configuration file then you need to use the NHibernate hibernate.connection.connection_string_name property rather than the hibernate.connection.connection_string property. This is documented in the NHibernate documentation here: http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/session-configuration.html#d0e771.