Spring.NET включает полезные классы-обертки над API NHibernate (HibernateTemplate, HibernateDaoSupport и тд). Можно ли использовать Fluent NHibernate для настройки меппинга и при этом не отказываться от поддержки Spring.NET?
Можно ли использовать Spring.NET и Fluent NHibernate вместе?
2
Спросил
Лучший ответ:
2
В Spring.NET за инстанцирование сессии NHibernate отвечает класс LocalSessionFactoryObject
. Чтобы загрузить меппинг Fluent из сборок нужно унаследоваться от LocalSessionFactoryObject
и перекрыть методы. создающие сессию.
Так:
public class FluentNhibernateLocalSessionFactoryObject
: LocalSessionFactoryObject
{
protected override void PostProcessConfiguration(Configuration config)
{
base.PostProcessConfiguration(config);
Fluently.Configure(config)
.Database(MySQLConfiguration.Standard.ConnectionString(
c => c.FromConnectionStringWithKey("connectionString")))
.Mappings( m => m
.FluentMappings
.AddFromAssemblyOf<FluentNHibernateLocalSessionFactoryObject>())
.BuildSessionFactory();
}
}
Или так:
public class FluentNhibernateLocalSessionFactoryObject
: LocalSessionFactoryObject
{
protected override ISessionFactory NewSessionFactory(Configuration config)
{
config = Fluently.Configure(config)
.Mappings(m => m
.FluentMappings
.AddFromAssemblyOf<FluentSessionFactoryObject>())
.BuildConfiguration();
return base.NewSessionFactory(config);
}
}