【问题标题】:Injecting ISession Into My Repositories Using Structuremap in a Asp.Net MVC Application在 Asp.Net MVC 应用程序中使用 Structuremap 将 ISession 注入我的存储库
【发布时间】:2009-08-18 22:24:54
【问题描述】:

我的存储库都在构造函数中使用 ISession:

protected Repository(ISession session)
{
     this.session = session;
}
private readonly ISession session;

在使用 StructureMap 的 Asp.Net MVC 应用程序中,我将如何在我的 StructureMap 注册表中设置 ISession?我还需要将 SessionFactory 添加到容器中吗? FluentNHibernate 会改变一切吗?

【问题讨论】:

    标签: asp.net-mvc nhibernate fluent-nhibernate structuremap


    【解决方案1】:

    您应该使用工厂方法注册 ISession。

    另一种选择(并不总是最好的,但易于使用)是:

    实现 ISession 和 ISessionFactory 接口(SessionProxy 和 SessionFactoryProxy)。

    public class SessionAggregator : ISession {
        protected ISession session;
    
        public SessionAggregator(ISessionFactory theFactory) {
            if (theFactory == null)
                throw new ArgumentNullException("theFactory", "theFactory is null.");
            Initialise(theFactory);
        }
    
        protected virtual void Initialise(ISessionFactory factory) {
            session = factory.OpenSession();
        }
        // the ISession implementation - proxy calls to the underlying session  
     }
    
    public class SessionFactoryAggregator : ISessionFactory {
        protected static ISessionFactory factory;
        private static locker = new object();
        public SessionFactoryAggregator() {
                if (factory == null) {
                  lock(locker) {
                    if (factory == null)
                      factory = BuildFactory();
                  }
                }
        }
    
        // Implement the ISessionFactory and proxy calls to the factory                
    }
    

    这样你只需注册 ISession(由 SessionAggregator 实现)和 ISessionFactory(SessionFactoryAggreagator),任何 DI 框架都可以轻松解析 ISession。

    如果您的 DI 不支持工厂方法(我不知道 Structure Map 是否支持),这很好。

    我已将这些实现添加到我的 Commons 程序集中,因此我不应该每次都重新实现它。

    编辑:现在,要在 Web 应用程序中使用 ISession:

    1. 在结构映射中注册 SessionFactoryAggregator(生命周期可以是单例的)。
    2. 在 Snstrucure 映射中注册 SessionAggregator 并将其生命周期设置为 InstanceScope.Hybrid。
    3. 在每个请求结束时,您需要通过调用 HttpContextBuildPolicy.DisposeAndClearAll() 来释放会话

    代码可能如下所示:

    // The Registry in StructureMap
    ForRequestedType<ISessionFactory>()
            .CacheBy(InstanceScope.Singleton)
            .TheDefaultIsConcreteType<SessionFactoryAggregator>();
    
    ForRequestedType<ISession>()
            .CacheBy(InstanceScope.Hybryd)
            .TheDefaultIsConcreteType<SessionAggregator>();
    
    // Then in EndRequest call
    HttpContextBuildPolicy.DisposeAndClearAll()
    

    【讨论】:

    • 我真的在寻找一个 StructureMap 示例,或者使用您的示例,我将如何将 SessionAggregator 和 SessionFactoryAggregator 注入容器中。考虑到网络应用环境、上下文、会话等。
    • 这正是我想要的,thanx。
    【解决方案2】:

    This问答可能对您有所帮助。

    一种方法 - 从"S#arp Architecture" 窃取休眠会话管理。效果很好。

    【讨论】:

      猜你喜欢
      • 2011-02-14
      • 1970-01-01
      • 1970-01-01
      • 2019-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多