确实消除< T>()每个会话的回报对象? [英] Does Resolve<T>() return objects per-session?

查看:105
本文介绍了确实消除< T>()每个会话的回报对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在微软统一的IoC,如果我叫解析< SOMETYPE>?(),我可以保证返回的对象是已在当前会话期间创建的

In Microsoft Unity IoC, if I call Resolve<SomeType>(), can I guarantee that the object returned is the one that was created during the current session?

例如,三个用户登录时,让我们说, SOMETYPE 中,获取在容器中创建的对象为每个用户不同的值。将向通话解析返回已为当前用户创建的对象?还是会做一些愚蠢像回创建的最后一个?

For example, three users sign on, and let's say that the object of SomeType that gets created in the container has different values for each user. Will a call to Resolve return the object that was created for the current user? Or would it do something stupid like return the last one that was created?

我有,由于一些环境问题的烦恼测试这个自己,我需要检查在不久的东西,因此,如果有人能回答这个问题,将是非常有益的!

I'm having troubles testing this myself due to some environment problems and I need to check something in soon, so if someone could answer this it would be very helpful!

修改

原谅我,因为我很新的团结,但基于我读这里,好像我应该能够用唯一的名称来注册容器中的对象,并通过该名称进行检索。所以,我岂不是能够使用一个会话内持续会话ID或其他一些价值取回我的对象​​?

Forgive me for I am very new to Unity, but based on what I read here, it seems like I should be able to register objects in the container with a unique name and retrieve them by that name. So, wouldn't I be able to use a session ID or some other value that persists within a session to retrieve my object?

推荐答案

哇哦,在上午MVC应用程序中使用统一生命周期管理。我在哪里开始?

Oh wow, lifetime management using Unity in am MVC app. Where do I start?

首先,因为没有ASP.NET系统,将保证相同的实例将在同一会话请求之间使用的会话单身是不是真的有可能。会话可以模仿同一个对象被序列化和请求之间反序列化是在会话中依然存在。

First of all, session singletons are not really possible as there is no ASP.NET system that will guarantee that the same instance will be used between requests in the same session. The session can mimic the same object persisted within the session by serializing and deserializing it between requests.

瞬态情况 - 即简单的注册,而不生命周期管理规范是足够的99%的时间。这意味着,为注册类型的实例将被创建每次需要时

Transient instances - i.e. simple registrations without lifetime management specification are sufficient 99% of the time. This implies that an instance of registered type will be created every time it is needed.

有很少,你需要实例住整个请求的寿命。然而,当你需要这些,您真的需要的人。到数据库的连接是一个最好的人选。要求单身,而另一方面更容易创建和管理。

It is very rarely that you need instances to live throughout the lifetime of the request. However when you need those, you really need those. A connection to a DB is a perfect candidate for this. Request singletons, on the other hand are much easier to create and manage.

最优雅的解决方案是使用Unity的子容器功能。子容器可以在请求开始创建,设置在请求结束(作为一个额外的好处,将处理所有的 ContainerControlledLifetimeManager 实例)。

The most elegant solution is to use Unity's child container feature. A child container can be created at the beginning of the request, disposed at the end of the request (as an added bonus it will dispose all ContainerControlledLifetimeManager instances).

在创建子容器,所有注册仍然可以从父容器,所以你需要用的子容器注册的要求具体的东西。

When creating a child container, all registrations are still available from the parent container, so you need to register request specific stuff with the child container.

下面是伪code得到这个工作:

Here is pseudo-code to get this working:

private void Application_Start() {
  _parentContainer = new UnityContainer();
   //creates a transient registration, available at any point in the app.
  _parentContainer.RegisterType<IParentIntf, ParentIntfImpl>();
  ControllerBuilder.Current.SetControllerFactory(new ServiceLocatorControllerFactory());
}

private void Application_BeginRequest() {
  var childContainer = _parentContainer.CreateChildContainer();
  //registers a request "singleton"
  //This registration is a type registration, an instance of RequestInterfaceImpl
  //will be created when needed and then kept in the container for later use.
  childContainer.RegisterType<IRequestInterface,RequestInterfaceImpl>(new ContainerControlledLifetimeManager());
  //save the child container in the context, so we can use it later
  HttpContext.Items["childContainer"] = childContainer;
}

private void Application_EndRequest() {
  //dispose the child container
  ((IUnityContainer)HttpContext.Items["childContainer"]).Dispose();
}

这需要做的另一件事是重写器厂使用的子容器创建控制器。控制器是进入应用程序的第一点,他们可以简单地采取在其构造其他组件的依赖关系。

One other thing that needs to be done is to override the Controller Factory to use the child container to create controllers. Controller are the first point of entry into the application and they could simply take a dependency on other components in their constructor.

public class UnityControllerFactory : DefaultControllerFactory {

    #region IControllerFactory Members

    public override IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName) {
        IController controller;
        controllerName = controllerName.ToLower();
        var container = ((IUnityContainer)HttpContext.Items["childContainer"])
        if(container.IsRegistered<IController>(controllerName))
            controller = container.Resolve<IController>(controllerName);
        else 
            controller = base.CreateController(requestContext, controllerName) ;
        return controller;
    }
} 

这篇关于确实消除&LT; T&GT;()每个会话的回报对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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