IDisposable的对象的依赖注入和续航时间 [英] Dependency injection and life time of IDisposable objects

查看:198
本文介绍了IDisposable的对象的依赖注入和续航时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立一个库使用依赖注入的方式(与Ninject),我有某种可能的混乱,因为我的不正确的设计。总之,我的设计方法是

I am trying to develop a library using dependency injection approach (with Ninject) and I am having some kind of confusion likely because of my incorrect design. In summary, my design approach is


  1. A 对象有一个通用对象。

  2. A 对象使用了一些可变数量的孩子的对象。

  3. 所有孩子对象应使用同样的常见父对象实例对象

  1. A parent object has a common object.
  2. A parent object uses some variable number of child objects.
  3. All child objects should use the very same common object instance with their parent object

下面。是我的问题领域的一个简单模型

Here is a simple model of my problem domain.

interface IParent : IDisposable {
    void Operation();
}
interface ICommon : IDisposable {
    void DoCommonThing();
}
interface IChild1 {
    void DoSomething();
}
interface IChild2 {
    void DoAnotherThing();
}
class Parent : IParent {
    private readonly ICommon _common;
    public Parent(ICommon common) {
        _common = common;
    }
    public void Dispose() {
        _common.Dispose();
    }
    public void Operation() {
        var c1 = ObjectFactory.GetInstance<IChild1>();
        c1.DoSomething();
        var c2 = ObjectFactory.GetInstance<IChild2>();
        c2.DoAnotherThing();
        // number of childs vary, do things until cn
        _common.DoCommonThing();
    }
}
class Common : ICommon {
    private bool _isDisposed;
    public void Dispose() {
        _isDisposed = true;
    }
    public void DoCommonThing() {
        if (_isDisposed) 
            throw new Exception("Common Object is Disposed");
    }
}
class Child1 : IChild1
{
    private readonly ICommon _common;
    public Child1(ICommon common) {
        _common = common;
    }
    public void DoSomething() {
        // Do Something...
        _common.DoCommonThing();
    }
}
class Child2 : IChild2 {
    private readonly ICommon _common;
    public Child2(ICommon common) {
        _common = common;
    }
    public void DoAnotherThing() {
        // Do Another Thing...
        _common.DoCommonThing();
    }
}



问题1


$ 的对象有所不同b $ b

需要子的数量。例如,根据返回 c1.DoSomething 的值,我可能会或可能不会需要其他的子对象。所以,我不想通过构造函数注入过,只需创建它们在需要的时候。但是,这种方法会导致违反好莱坞原则的。

Problem 1

Number of needed child objects vary. For example, according to return value of c1.DoSomething I may or may not need other child objects. So I do not want to inject them through constructor and just create them when they are needed. But this approach causes violation of Hollywood Principle.

如何预防这种违规行为,而不进孩子通过构造函数对象?

How this violation can be prevented, without injecting child objects through constructor?

我想孩子对象使用相同的通用对象实例与对象。如此续航时间通用对象应该是相同的是其父母。

I want child objects to use same common object instance with their parent object. So life time of common object should be same as its parent.


  1. 如果没有续航时间为ICommon定义,那么所有的孩子对象都会有自己的通用对象实例。

  1. If there is no life time is defined for ICommon then all child objects will have their own common object instance.

如果ICommon的续航时间在线程的定义或要求的范围那么我不能使用母公司的不同实例对象相同的线程或请求范围。因为每个对象应使用自己的品牌新的通用对象和处理它。

If life time of ICommon is defined in Thread or Request scope then I cannot use different instances of parent object in same Thread or Request scope. Because each parent object should use their own brand new common object and dispose it.

所以我不能用我所知道的续航时间范围选项解决它。我公司生产的是第二个问题的另一个解决方案,但它使代码更糟糕。

So I could not solve it using life time scope options that I know. I produced another solution for this second problem but it makes code worse.

第一,而不是注入 ICommon 对象,反对它的自我通过的ObjectFactory <创建它/ p>

First, instead of injecting ICommon into parent object, parent object it self creates it through ObjectFactory

class Parent : IParent {
    private readonly ICommon _common;
    public Parent() {
        _common = ObjectFactory.GetInstance<ICommon>();
    }
.....



然后,而不是注射 ICommon 孩子对象,对象集通用子对象的对象

Then, instead of injecting ICommon into child object, parent object sets common object of child objects.

interface IChild {
    ICommon Common { get; set; }
}
interface IChildN : IChild {
     void DoNthThing();
}
abstract class ChildBase : IChild {
    ICommon IChild.Common { get; set; }
}
class ChildN : IChildN {
     public void DoNthThing() { }
}
class Parent : IParent {
    private readonly ICommon _common;
    public void Operation() {
        var c1 = ObjectFactory.GetInstance<IChild1>();
        c1.Common = _common;
        c1.DoSomething();
        var c2 = ObjectFactory.GetInstance<IChild2>();
        c2.Common = _common;
        c2.DoAnotherThing();
        _common.DoCommonThing();
    }
}



但这个解决方案又违反了好莱坞原则,我要设置每个孩子对象的共同财产。

哪有对象分发其通用对象孩子使用依赖注入的对象? (最好用Ninject)

How can parent object distribute its common object to child objects using Dependency Injection? (preferably with Ninject)

这是多一点一般在我的问题:如何能正确应用这个模型依赖注入

This is a little bit more general about my problem: How can Dependency Injection applied correctly to this model?

请注意:<?code> ObjectFactory.GetInstance 通话内核。获得 Ninject的

NOTE: ObjectFactory.GetInstance calls Kernel.Get of Ninject

推荐答案

您需要使用任 CallScope NamedScope 。这些都是 Ninject.Extensions.NamedScope 包的一部分。这可以让你的范围通用对象父,因此所有子请求接收相同的常见的。

You need to use either the CallScope or the NamedScope. These are part of the Ninject.Extensions.NamedScope package. This allows you to scope the common object to the parent so all child requests receive the same common.

关于子对象的创建。如果你有要求根据某种算法子对象,你需要用一个工厂来实例化。使用 Ninject.Extensions.Factory 包来实现这一目标。这确实上下文保持获取和传递给孩子要求家长背景,因此允许也重用你的常见对象,其中由工厂创建的孩子。

Regarding the child object creation. If you have to request child objects depending on some algorithm you need to instantiate it with a factory. Use the Ninject.Extensions.Factory package to achieve this. This does a context preserving get and passes the parent context to the child request and therefore allows to reuse your common object also in the children which are created by the factory.

所以,到底会有没有必要用自己的对象的工厂。

So in the end there will be no need to use your own object factory.

这篇关于IDisposable的对象的依赖注入和续航时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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