统一2.0和处理IDisposable的类型(特别是PerThreadLifetimeManager) [英] Unity 2.0 and handling IDisposable types (especially with PerThreadLifetimeManager)

查看:182
本文介绍了统一2.0和处理IDisposable的类型(特别是PerThreadLifetimeManager)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,类似的问题被问了几次(例如:这里 这里,的这里这里),但它是为统一previous版本在哪里得到的答复是依赖于使用 LifetimeManager 类。

I know that similar question was asked several times (for example: here, here,here and here) but it was for previous versions of Unity where the answer was dependent on used LifetimeManager class.

文件说:

统一使用了继承的特定类型   从LifetimeManager基类   (统称为寿命   管理者)来控制其存储   引用对象实例,以及如何   这些容器处置   实例。

Unity uses specific types that inherit from the LifetimeManager base class (collectively referred to as lifetime managers) to control how it stores references to object instances and how the container disposes of these instances.

好吧,听起来不错,所以我决定检查落实构建的生命周期管理。我的结论:

Ok, sounds good so I decided to check implementation of build in lifetime managers. My conclusion:

  • TransientLifetimeManager - 没有处理处置。集装箱仅解决实例,它并没有跟踪它。调用code是负责处理实例。
  • ContainerControlledLifetimeManager - 当一辈子经理配置部署实例(当容器被布置=)。提供hiearchy所有容器之间共享单一实例。
  • HierarchicalLifetimeManager - 源自 ContainerControlledLifetimeManager 行为。它提供了单身的hiearchy(子容器)每个集装箱的实例。
  • ExternallyControlledLifetimeManager - 没有处理处置。正确的行为,因为容器没有实例的所有者。
  • PerResolveLifetimeManager - 没有处理处置。它一般是一样的 TransientLifetimeManager 但它可以让整个解析对象图的时候重复使用实例的依赖注入。
  • PerThreadLifetimeManager - 处置没有处理的MSDN中也有介绍。 谁负责处置?
  • TransientLifetimeManager - no handling of disposing. Container only resolves instance and it does not track it. Calling code is responsible for disposing instance.
  • ContainerControlledLifetimeManager - disposes instance when lifetime manager is disposed (= when container is disposed). Provides singleton instance shared among all containers in hiearchy.
  • HierarchicalLifetimeManager - derives behavior from ContainerControlledLifetimeManager. It provides "singleton" instance per container in hiearchy (subcontainers).
  • ExternallyControlledLifetimeManager - no handling of disposing. Correct behavior because container is not owner of the instance.
  • PerResolveLifetimeManager - no handling of disposing. It is generally same as TransientLifetimeManager but it allows reusing instance for dependency injection when resolving whole object graph.
  • PerThreadLifetimeManager - no handling of disposing as also described in MSDN. Who is responsible for disposing?

实现内置的 PerThreadLifetimeManager 是:

public class PerThreadLifetimeManager : LifetimeManager
{
    private readonly Guid key = Guid.NewGuid();
    [ThreadStatic]
    private static Dictionary<Guid, object> values;

    private static void EnsureValues()
    {
        if (values == null)
        {
            values = new Dictionary<Guid, object>();
        }
    }

    public override object GetValue()
    {
        object result;
        EnsureValues();
        values.TryGetValue(this.key, out result);
        return result;
    }

    public override void RemoveValue()
    { }

    public override void SetValue(object newValue)
    {
        EnsureValues();
        values[this.key] = newValue;
    }
}

所以处理容器不处理与此生管理器创建一次性的实例。线程完成也将不会出售这些实例。那么,谁是负责释放实例?

So disposing container does not dispose disposable instances created with this lifetime manager. Thread completion will also not dispose those instances. So who is responsible for releasing instances?

我试图手动配置在code解决实例,我发现了另一个问题。我不能拆卸的instnace。一生经理RemoveValue是空的 - 一旦实例被创建就无法从线程静态词典中删除它(我也怀疑 TearDown中方法不执行任何操作)。所以,如果你调用处理的情况下,你会得到处置实例后解决。我想这可能是非常大的问题,采用从线程池中的线程这一生管理器时。

I tried to manually dispose resolved instance in code and I found another problem. I can't teardown the instnace. RemoveValue of lifetime manager is empty - once the instance is created it is not possible to remove it from thread static dictionary (I'm also suspicious that TearDown method does nothing). So if you call Resolve after disposing the instance you will get disposed instance. I think this can be quite big problem when using this lifetime manager with threads from thread pool.

如何正确使用这一辈子经理?

How to correctly use this lifetime manager?

此外,这种实现是经常重复使用的定制一生的经理像PerCallContext,PerHtt prequest,PerAspNetSession,PerWcfCall,等等。只有线程静态辞典被替换其他结构。

Moreover this implementation is often reused in custom lifetime managers like PerCallContext, PerHttpRequest, PerAspNetSession, PerWcfCall, etc. Only thread static dictionary is replaced with some other construct.

另外我理解正确的处理处置的对象是依赖于生命周期管理器?因此,应用程序code是取决于所使用的寿命经理。

Also do I understand it correctly that handling disposable objects is dependent on lifetime manager? So the application code is dependent on used lifetime manager.

我看了,在其他IoC容器中,处理临时处置的对象是由子容器处理,但我没有找到例子团结 - 这可能与当地范围内的子容器和 HiearchicalLifetimeManager ,但我不知道该怎么做。

I read that in other IoC containers dealing with temporary disposable objects is handled by subcontainers but I didn't find example for Unity - it could be probably handled with local scoped subcontainer and HiearchicalLifetimeManager but I'm not sure how to do it.

推荐答案

有只有少数情况下统一将配置一个实例。这实在是不支持的。我的解决方案是一个自定义扩展来实现这一点 - 的http://www.neovolve.com/2010/06/18/unity-extension-for-disposing-build-trees-on-teardown/

There are only a few circumstances where Unity will dispose an instance. It is really unsupported. My solution was a custom extension to achieve this - http://www.neovolve.com/2010/06/18/unity-extension-for-disposing-build-trees-on-teardown/

这篇关于统一2.0和处理IDisposable的类型(特别是PerThreadLifetimeManager)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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