在 Asp.Net MVC 应用程序中使用 Structuremap 将 ISession 注入我的存储库 [英] Injecting ISession Into My Repositories Using Structuremap in a Asp.Net MVC Application

查看:24
本文介绍了在 Asp.Net MVC 应用程序中使用 Structuremap 将 ISession 注入我的存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My repositories all take ISession in the constructor:

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

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

In an Asp.Net MVC application, using StructureMap, how would I go about setting up ISession in my the StructureMap Registry? Would I need to to add SessionFactory to the container also? Does FluentNHibernate change things?

推荐答案

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

You should register ISession using a factory method.

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

Another options (not always the best, but easy to use) is to:

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

Implement ISession and ISessionFactory interfaces (SessionProxy and 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(SessionFactoryAggregator),任何 DI 框架都可以轻松解析 ISession.

This way you can just register ISession (implemented by SessionAggregator) and ISessionFactory (SessionFactoryAggreagator) and any DI framework will resolve ISession easily.

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

This is good if your DI does not support factory method (I don't know if Structure Map does).

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

I have added these implementation to my Commons assembly so I should not reimplement it every time.

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

Now, to make use of ISession in web application:

  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()

这篇关于在 Asp.Net MVC 应用程序中使用 Structuremap 将 ISession 注入我的存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆