我正在尝试在SharpArchitecture应用程序上使用NHibernate.Search,通过FluentNHibernate.Search映射来维护纯POCO域对象。
但我不知道如何设置NHibernateSession:
在我的Global.asax.cs上,我有这个初始化,并且工作正常:
NHibernateSession.Init(
this.webSessionStorage,
new[] { Server.MapPath( "~/bin/MyBlog.Infrastructure.dll" ) },
new AutoPersistenceModelGenerator().Generate(),
Server.MapPath( "~/NHibernate.config" ) );然后,https://github.com/trullock/Fluent-NHibernate-Search/wiki说我需要创建一个FluentSearch配置,如下所示:
Configuration nhcfg = FluentSearch.Configure()
.DefaultAnalyzer().Standard()
.DirectoryProvider().FSDirectory()
.IndexBase("~/Index")
.IndexingStrategy().Event()
.MappingClass<LibrarySearchMapping>()
.BuildConfiguration();最后在FluentNHibernate之上配置NHibernate.Search。
但是,如何将"nhcfg“配置与NHibernateSession.Init连接起来呢?NHibernateSession.Init和FluentHibernate.Search似乎具有不兼容的接口。
有没有办法将SharpArchitecture应用上的NHibernate.Search与FluentHibernate.Search地图集成?
发布于 2012-04-16 12:03:37
解决了!
我研究了SharpArchitecture中的NHibernateSesssion实现,并在NHibernateSession.Init方法之外提取了会话工厂配置。最后,我添加了调用NHibernateSession.AddConfiguration方法的新配置。
请注意,NHibernateSession.Init在内部注册了一些侦听器:
c.EventListeners.PreInsertEventListeners = new IPreInsertEventListener[]
{
new DataAnnotationsEventListener()
};
c.EventListeners.PreUpdateEventListeners = new IPreUpdateEventListener[]
{
new DataAnnotationsEventListener()
};问题是SharpArch.NHibernate.dll内部的DataAnnotationsEventListener类;所以我需要在我的项目中复制这个类。虽然很难看,但很管用。
最后,NHibernate会话初始化如下所示:
var nhConfig = new Configuration();
nhConfig.Configure( Server.MapPath( "~/NHibernate.config" ) );
var cnf = Fluently
.Configure( nhConfig )
.Mappings(
m =>
{
var mappingAssembly = Server.MapPath( "~/bin/MyBlog.Infrastructure.dll" );
var assembly = Assembly.LoadFrom( MakeLoadReadyAssemblyName( mappingAssembly ) );
m.HbmMappings.AddFromAssembly( assembly );
m.FluentMappings.AddFromAssembly( assembly ).Conventions.AddAssembly( assembly );
m.AutoMappings.Add( new AutoPersistenceModelGenerator().Generate() );
})
.ExposeConfiguration( c =>
{
FluentSearch.Configure( c )
.DefaultAnalyzer().Standard()
.DirectoryProvider().FSDirectory()
.IndexBase( "~/Index" )
.IndexingStrategy().Event()
.Listeners( FluentNHibernate.Search.Cfg.ListenerConfiguration.Default )
.MappingClass<SearchMap>()
.BuildConfiguration();
c.SetListeners( ListenerType.PostInsert, new[] { new FullTextIndexEventListener() } );
c.SetListeners( ListenerType.PostUpdate, new[] { new FullTextIndexEventListener() } );
c.SetListeners( ListenerType.PostDelete, new[] { new FullTextIndexEventListener() } );
c.SetListener( ListenerType.PostCollectionRecreate, new FullTextIndexCollectionEventListener() );
c.SetListener( ListenerType.PostCollectionRemove, new FullTextIndexCollectionEventListener() );
c.SetListener( ListenerType.PostCollectionUpdate, new FullTextIndexCollectionEventListener() );
/*
c.EventListeners.PreInsertEventListeners = new IPreInsertEventListener[]
{
new DataAnnotationsEventListener()
};
c.EventListeners.PreUpdateEventListeners = new IPreUpdateEventListener[]
{
new DataAnnotationsEventListener()
};
*/
})
.BuildConfiguration();
NHibernateSession.Storage = this.webSessionStorage;
NHibernateSession.AddConfiguration(
NHibernateSession.DefaultFactoryKey,
cnf.BuildSessionFactory(),
cnf,
null);https://stackoverflow.com/questions/10158342
复制相似问题