如何建立国际奥委会时,一个关键的类需要会话(或其他特定的上下文变量) [英] How to set up IoC when a key class needs Session (or other context-specific variable)

查看:75
本文介绍了如何建立国际奥委会时,一个关键的类需要会话(或其他特定的上下文变量)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出其中的相关类可以改变基于应用程序中的一些变量(在这种情况下,会话状态)如何在情况下使用的IoC。例如,我们的每一个客户都有不同的数据库,因此需要对存储在其会话值建立与数据库的连接(尤其是因为一些用户可能有多个数据库,如果他们拥有多个企业,将数据库之间进行切换) 。

I am trying to figure out how to use IoC in situations where the dependent classes can change based on some variable in the application (in this case, Session state). For example, each of our clients have a different database, so the connection to the database needs to be built on a value stored in their Session (particularly since some users could have multiple databases if they own multiple businesses, and would switch between databases).

下面是我们如何会目前建立这一结构的通用示例:

Here is a generic example of how we'd currently set up this structure:

public class MyTestController : ControllerBase
{
    Repository _rep;

    public MyTest(Repository rep)
    {
        _rep = rep;
    }

    public MyTest()
    {
        string connString = String.Format("Server={0}; Database={1};"
            , SessionContainer.ServerName, SessionContainer.DatabaseName;
        var dc = new DataContext(connString);
        _rep = new Repository(dc);
    }

    public int SampleFn()
    {
        return _rep.GetCountOfEmployees();
    }
}

public class Repository
{
    DataContext _context;

    public Repository(DataContext context)
    {
        _context = context;
    }
}

我们将能够进行此设置使用的IoC和消除默认的C-器?如果是这样,怎么样?我没有只用直喷问题这样,但我想探索一个StructureMap或统一的可能性。(注:我们通常传递DB /服务器到工厂类,构建的datacontext ...上面的例子仅仅是为了简洁)

Would we be able to set this up using IoC and eliminate the default c-tors? If so, how? I don't have a problem just using D.I. like this, but I'd like to explore the possibility of a StructureMap or Unity (note: we normally pass in db/server to a factory class that builds the datacontext ... above example is just for brevity).

推荐答案

如何创建的存储库实例,以及它的一生中,没有控制器的关注。

How the Repository instance is created, as well as its lifetime, is of no concern of the Controller.

当您在容器中注册组件,您应该指定组件的使用寿命。根据您的实现,你可以简单地选择设置库的lifefime跟随会话。

When you register components in the container, you should specify the lifetime of the component. Depending on your implementation, you may simply choose to set the lifefime of the Repository to follow the session.

在任何情况下,你可以使用一个工厂来创建从会话存储库中,而是从外面控制做到这一点。

In any case you could use a factory to create the repository from the session, but do this from outside the Controller.

您一定要摆脱默认构造函数的。

You definitely need to get rid of the default constructor.


关闭我的头顶,我不记得如何统一或StructureMap做到这一点,所以这里是一个温莎城堡的例子。

Off the top of my head I can't remember how to do this in Unity or StructureMap, so here's a Castle Windsor example.

定义一个抽象工厂:

public interface IRepositoryFactory
{
    Repository Create();
}

和实施

public class MyRepositoryFactory : IRepositoryFactory
{
    private readonly HttpContextBase httpContext;

    public MyRepositoryFactory(HttpContextBase httpContext)
    {
        if (httpContext == null)
        {
            throw new ArgumentNullException("httpContext");
        }

        this.httpContext = httpContext;
    }

    #region IRepositoryFactory Members

    public Repository Create()
    {
        // return Repository created from this.httpContext
    }

    #endregion
}

现在注册所有的东西

container.AddFacility<FactorySupportFacility>();
container.Register(Component.For<IRepositoryFactory>()
    .ImplementedBy<MyRepositoryFactory>()
    .LifeStyle.PerWebRequest);
container.Register(Component.For<Repository>()
    .UsingFactory((IRepositoryFactory f) => f.Create())
    .LifeStyle.PerWebRequest);

在这里,我使用了PerWebRequest的生活方式,但如果你想优化你可能希望创建一个自定义PerWebSession生活方式。这是不是太难的城堡做的,但我不记得这是在其他DI容器有多难。

Here I've used the PerWebRequest lifestyle, but if you want to optimize you might want to create a custom PerWebSession lifestyle. This is not too hard to do in Castle, but I can't remember how hard it is in other DI Containers.

您还需要注册Htt​​pContextBase,因为MyRepositoryFactory依赖于它。

You will also need to register HttpContextBase, since MyRepositoryFactory depends on it.

这篇关于如何建立国际奥委会时,一个关键的类需要会话(或其他特定的上下文变量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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