I'm trying to use NHibernate.Search on a SharpArchitecture app, with FluentNHibernate.Search mapping to maintain pure POCO domain objects.

But i dont know how to setup the NHibernateSession:

On my Global.asax.cs i have this initialization and works fine:

NHibernateSession.Init(
    this.webSessionStorage,
    new[] { Server.MapPath( "~/bin/MyBlog.Infrastructure.dll" ) },
    new AutoPersistenceModelGenerator().Generate(),
    Server.MapPath( "~/NHibernate.config" ) );

Then, https://github.com/trullock/Fluent-NHibernate-Search/wiki says that i need to create a FluentSearch config like this:

Configuration nhcfg = FluentSearch.Configure()
    .DefaultAnalyzer().Standard()
    .DirectoryProvider().FSDirectory()
    .IndexBase("~/Index")
    .IndexingStrategy().Event()
    .MappingClass<LibrarySearchMapping>()
    .BuildConfiguration();

And finally configure NHibernate.Search atop FluentNHibernate.

But, what can i do to connect "nhcfg" config with NHibernateSession.Init? NHibernateSession.Init and FluentHibernate.Search appear to have incompatible interfaces.

Is there a way to integrate NHibernate.Search on a SharpArchitecture app with FluentHibernate.Search mapping?

有帮助吗?

解决方案

Solved!

I have looked inside NHibernateSesssion implementation from SharpArchitecture and extracted the session factory configuration outside NHibernateSession.Init method. Finally i have added the new configuration calling NHibernateSession.AddConfiguration method.

Pay attention that NHibernateSession.Init internally register some listeners:

 c.EventListeners.PreInsertEventListeners = new IPreInsertEventListener[]
                        {
                            new DataAnnotationsEventListener()
                        };
 c.EventListeners.PreUpdateEventListeners = new IPreUpdateEventListener[]
                        {
                            new DataAnnotationsEventListener()
                        };

The problems is that DataAnnotationsEventListener class, is internal to SharpArch.NHibernate.dll; so i need to duplicate this class inside my project. Ugly but works.

Finally NHibernate session initialization look like this:

        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);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top