与构造器参数依赖注入不在接口 [英] Dependency Injection with constructor parameters that aren't interfaces

查看:97
本文介绍了与构造器参数依赖注入不在接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还在DI一个新手,我想了解,如果我想的东西走错了路。我在玩具问题时我想表示对一个IRandomProvider依赖一个模具对象。该接口是简单的:

I'm still a newbie at DI, and I am trying to understand if I am thinking of things the wrong way. I am working on a toy problem when I want to represent a Die object that has a dependency on an IRandomProvider. That interface is simple:

public interface IRandomProvider 
{
   int GetRandom(int lower, int upper);
}



我想有一个模具的构造,看起来像这样:

I want to have a Die constructor that looks like this:

Die(int numSides, IRandomProvider provider)

我试图使用具有这样的方法静态DIFactory:

I'm trying to use a static DIFactory that has a method like this:

    public static T Resolve<T>()
    {
        if (kernel == null)
        {
            CreateKernel();
        }
        return kernel.Get<T>();
    }



在哪里CreateKernel简单地绑定到特定的实现IRandomProvider的。

Where CreateKernel simply binds to a specific implementation of IRandomProvider.

我希望能够与调用这样的:

I want to be able to call this with:

DIFactory.Resolve<Die>(20);



我无法使这项工作未做的解决,它可以让我的特殊版本处理ConstructorArgs。这似乎使事情过于复杂,并需要我修改DIFactory为每隔一个实例,以及扎为构造参数的特定名称

I can't make this work without making a special version of "Resolve" which can allow me to deal with ConstructorArgs. That seems to make things overly complex, and would require me to modify DIFactory for every other instance of that, as well as tie to a specific name for the constructor parameter.

如果我重构模具类不使用int构造函数,一切工作正常。但是,现在有人给remeber初始化即numSides参数,这似乎是一个坏主意,因为它是类的要求。

If I refactor the Die class to not use the int constructor, everything works fine. But now someone has to remeber to initialize the numSides parameter, which seems like a bad idea, since it is a requirement for the class.

我怀疑这是一个糟糕的精神型号为DI。任何人都可以告诉我吗?

I suspect this is a bad mental model for DI. Can anyone enlighten me?

推荐答案

控制容器的反转是不是一个工厂。不要用它来解决像你的模具类的业务对象。反转的控制是用来让容器接管控制你的对象的生命周期模式。这方面的一个好处是,它还支持依赖注入模式。通常是创建,更改和处置

An inversion of control container is not a factory. Don't use it to resolve business objects like your Die class. Inversion Of Control is pattern used to let the container take control over the lifetime of your objects. A bonus of that is that it also supports the dependency injection pattern.

业务对象。因此没有必要使用容器他们。正如你刚才注意到了,他们不参加,这使得它很难使用的容器为他们构造的强制性参数。

Business objects is typically created, changed and disposed. Hence no need to use the container for them. And as you just noticed, they do take their mandatory parameters in the constructor which makes it hard to use the container for them.

您可以注册一个 DieFactory 容器中,让它走 IRandomProvider 在构造函数中:

You can register a DieFactory in the container and let it take the IRandomProvider in the constructor:

public class DieFactory
{
    public DieFactory(IRandomProvider provider)
    {}

    public Die Create(int numberOfSides)
    {
        return new Die(numberOfSides, _provider);
    }
}



但是,当然,这将是最好创建一个工厂用于创建所有相关的业务对象。然后,你可以把内核的依赖关系:

But it would of course be better to create a factory used to create all related business objects. Then you can take the kernel as a dependency:

public class AGoodNameFactory
{
    public DieFactory(IKernel kernel)
    {}

    public Die CreateDie(int numberOfSides)
    {
        var provider = _kernel.Resolve<IRandomProvider>();
        return new Die(numberOfSides, provider);
    }

    // other factories.
}



或者你可以只取 IRandomProvider 为直接在类的依赖,创造了模具类。

Or you could just take the IRandomProvider as a dependency directly in the class that creates the Die class.

这篇关于与构造器参数依赖注入不在接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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