2011-09-14 47 views
0

有人可以幫助我轉換下面來StructureMap?轉換此溫莎片段來StructureMap NHibernate的

public class NHibernateInstaller : IWindsorInstaller 
{ 
    #region IWindsorInstaller Members 

    public void Install(IWindsorContainer container, IConfigurationStore store) 
    { 
     container.Register(Component.For<ISessionFactory>() 
           .UsingFactoryMethod(k => BuildSessionFactory())); 

     container.Register(Component.For<NHibernateSessionModule>()); 

     container.Register(Component.For<ISessionFactoryProvider>().AsFactory()); 

     container.Register(Component.For<IEnumerable<ISessionFactory>>() 
            .UsingFactoryMethod(k => k.ResolveAll<ISessionFactory>())); 

     HttpContext.Current.Application[SessionFactoryProvider.Key] 
         = container.Resolve<ISessionFactoryProvider>(); 
    } 

    #endregion 
    public ISessionFactory BuildSessionFactory() { ... } 
} 

我有這個迄今爲止在我的Global.asax:

protected void Application_Start() 
{ 
    AreaRegistration.RegisterAllAreas(); 

    RegisterGlobalFilters(GlobalFilters.Filters); 
    RegisterRoutes(RouteTable.Routes); 

    IContainer container = new Container(
     x => 
     { 
      x.For<ISessionFactory>().Use // ??? NHibernateHelper.BuildSessionFactory(); 

      x.For<IControllerActivator>().Use<StructureMapControllerActivator>(); 
      x.For<IBar>().Use<Bar>(); 
     } 
    ); 


    DependencyResolver.SetResolver(new StructuredMapDependencyResolver(container)); 
} 
  1. NHibernateHelper.BuildSessionFactory()是我的方法,不知道如何設置了爲ISessionFactory。

  2. 不知道爲什麼我需要把NHibernateSessionModule在我的容器,我只是有線了web.config中的模塊,這還不夠?

  3. 我不明白其餘的問題,比如ISessionFactoryProvider如何被連接成「.AsFactory()」,你不需要在那裏提供實現嗎?

  4. resolveAll是做什麼的?

我是新來的StructureMap(和DI一般)所以請不要假設我知道非常感謝。

回答

1

以下是我已經可以用於連接起來StructureMap和NHibernate的典型設置:

IContainer container = new Container(c => 
{ 
    c.Scan(s=> 
    { 
     // This autoregisters the IFoo to Foo 
     s.AssembliesFromApplicationBaseDirectory(); 
     s.WithDefaultConventions(); 
    }; 

    // SessionFactory needs to be a singleton object 
    c.ForSingletonOf<ISessionFactory>() 
     .Use(NHibernateHelper.BuildSessionFactory()); 

    // Add your own interceptor implementation here 
    c.For<IInterceptor>().Use<EmptyInterceptor>(); 

    // I assume that NHibernateSession uses the NHibernateSessionModule 
    // I use a similar implementation named TransactionBoundaryModule 
    c.For<INHibernateSession>() 
     .HybridHttpOrThreadLocalScoped() 
     .Use<NHibernateSession>(); 

    // Gets the current session from the NHibernateSession 
    // Ensures one session per request 
    c.For<ISession>().Use(x => 
    { 
     var instance = x.GetInstance<INHibernateSession>(); 
     return instance.CurrentSession; 
    }); 

    // Same for StatelessSession 
    c.For<IStatelessSession>() 
     .Use(x => x.GetInstance<ISessionFactory>() 
     .OpenStatelessSession()); 
}; 

僅供參考......我在https://github.com/codeprogression/Fluently-Persistent一個參考實現。從CP.FluentlyPersistent.Web/Global.asax.cs開始並遵循它。我使用了StructureMap Registry類來連接NHibernate,但語法非常相似。


UPDATE(回答關於在NHibernateInstaller類線的評論/問題):

簡短的回答是刪除類 - 它不需要一旦你提供一個名爲SessionFactoryProvider的ISessionFactoryProvider的實現。將下面這行添加到您的global.asax.cs中:HttpContext.Current.Application[SessionFactoryProvider.Key] = container.GetInstance<ISessionFactoryProvider>();

這是爲什麼......在StructureMap,彙編掃描,無需註冊NHibernateSessionModule。當前的SM二進制文件不包含Windsor的類型化工廠實現,但實現在GitHub源代碼中(查找AutoFactory)。爲了解決這個限制,你必須用GetSessionFactories()方法創建一個名爲SessionFactoryProvider的ISessionFactoryProvider派生。

+0

很不錯,順便說一句,你怎麼使用無狀態會話?是出於性能原因的只讀對象? – Blankman

+0

順便說一句,我用這個:https://gist.github.com/852307 – Blankman

+0

這篇文章是在這裏:http://nhforge.org/blogs/nhibernate/archive/2011/03/03/effective-nhibernate- session-management-for-web-apps.aspx – Blankman