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

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

问题描述

我的库中的所有采取的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和ISessionFactory(SessionFactoryAggreagator)(由SessionAggregator实现)和DI任何框架将轻松解决的Isession。

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

这是很好的,如果你的DI不支持工厂方法(我不知道,如果结构图一样)。

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

我已经装配添加了这些实现到我的共享,所以我不应该重新实现每一次它。

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()处理会话

在code可以关注一下:

The code can look like:

// The Registry in StructureMap
ForRequestedType<ISessionFactory>()
        .CacheBy(InstanceScope.Singleton)
        .TheDefaultIsConcreteType<SessionFactoryAggregator>();

ForRequestedType<ISession>()
        .CacheBy(InstanceScope.Hybryd)
        .TheDefaultIsConcreteType<SessionAggregator>();

// Then in EndRequest call
HttpContextBuildPolicy.DisposeAndClearAll()

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

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