将这个Windsor代码段转换为NHibernate的StructureMap

本文关键字:转换 NHibernate StructureMap Windsor 代码 段转换 | 更新日期: 2023-09-27 18:08:35

有人能帮我把下面的转换为StructureMap吗?

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连接模块。配置,这还不够吗?

  3. 我不明白其余的,就像如何ISessionFactoryProvider被连接为"。asfactory()",你不需要提供实现吗?

  4. resolveAll正在做什么?

我是StructureMap(和DI一般)的新手,所以请不要以为我知道很多。

将这个Windsor代码段转换为NHibernate的StructureMap

下面是我用来连接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,但语法非常相似。


更新(回答你在NHibernateInstaller类中的评论/问题):

简短的回答是删除类——一旦提供了名为SessionFactoryProvider的ISessionFactoryProvider的实现,就不需要它了。将HttpContext.Current.Application[SessionFactoryProvider.Key] = container.GetInstance<ISessionFactoryProvider>();行添加到您的global.asax.cs.

这是为什么…在StructureMap中,通过程序集扫描,不需要注册NHibernateSessionModule。当前的SM二进制文件不包括Windsor拥有的类型工厂实现,但是在GitHub源代码中有一个实现(查找AutoFactory)。要绕过这个限制,您必须使用GetSessionFactories()方法创建ISessionFactoryProvider的一个名为SessionFactoryProvider的派生。