I’m using NHibernate + Fluent to handle the database in my application. So far I’ve been using a SessionSource to create my ISession objects. I’m a bit confused now about what comes from NHibernate or Fluent, and what I really should use for creating my sessions.
ISession comes from NHibernate, and the SessionSource from Fluent. I create the SessionSource from a FluentConfiguration, and currently use the SessionSource to create sessions. This is my function to create sessions. The FluentConfiguration and SessionSource is reused:
if (_sessionSource == null)
{
_cfg = Fluently.Configure().Database(SQLiteConfiguration.Standard.ShowSql().UsingFile("test.db"));
_sessionSource = new SessionSource(_cfg.BuildConfiguration().Properties, new MappingsPersistenceModel());
var session = _sessionSource.CreateSession();
_sessionSource.BuildSchema(session);
return session;
}
return _sessionSource.CreateSession();
Does this look reasonable? It sounds more appealing to use a ISessionFactory to create sessions though, so I tried using one. This comes from NHibernate, so I don’t know if that’s why it is a problem, but it fails when my sessions are created from an ISessionFactory.
// Done once:
_sessionFactory = _cfg.BuildSessionFactory();
// Done each time a session is requested:
_sessionFactory.OpenSession()
Using this I get a MappingException when using the session, saying “No persister for: MyProject.Model.SomeModelClass“.
Should I keep using the SessionSource? Or am I missing something regarding the ISessionFactory?
The problem seems to be that the SessionFactory doesn’t know about the mappings since they are only given to the SessionSource. Adding the mappings during the fluent configuration and getting the factory from this seems to help. This gave me what looks like a better solution. Does this look reasonable to those of you with more experience on this?