使用依赖注入注入不同类型的类? [英] Inject different type of class using Dependency Injection?

查看:124
本文介绍了使用依赖注入注入不同类型的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


嗨朋友们,



希望一切顺利。



我是依赖注入的新手。我在阅读有关Ninject时遇到了疑问。



在Ninject wiki中,我看到了依赖注入的基本示例。由此我怀疑了。





这是链接。 https://github.com/ninject/ninject/wiki/Dependency-Injection-By-Hand [ ^ ] < br $> b $ b

Hi Friends,

Hope all doing well.

I'm new to dependency injection. i got a doubt while reading about Ninject.

In Ninject wiki i saw an basic example for Dependency Injection. From that my doubt arises.


This is the link. https://github.com/ninject/ninject/wiki/Dependency-Injection-By-Hand[^]

class Samurai
{
    readonly IWeapon weapon;
    public Samurai(IWeapon weapon)
    {
        this.weapon = weapon;
    }

    public void Attack(string target)
    {
        this.weapon.Hit(target);
    }
}
class Shuriken : IWeapon
{
    public void Hit(string target)
    {
        Console.WriteLine("Pierced {0}'s armor", target);
    }
}
class Program
{
    public static void Main() 
    {
        var warrior1 = new Samurai(new Shuriken());
        var warrior2 = new Samurai(new Sword());
        warrior1.Attack("the evildoers");
        warrior2.Attack("the evildoers");
    }
}





以下是该链接中提到的声明。

当类紧密耦合时,它们不能在不改变实现的情况下互换。为了避免紧密耦合类,我们可以使用接口来提供间接级别。



现在我怀疑了。如果我想创建名为Dress的新类并注入Samurai类。那个时候我也需要重写Samurai课程,如下所示





Below is the statement mentioned in that link.
"When classes are tightly coupled, they cannot be interchanged without altering their implementation. In order to avoid tightly coupling classes, we can use interfaces to provide a level of indirection."

Now my doubt is. If i want to create new class called Dress and inject to Samurai class. That time also i need to rewrite Samurai class know like below

class Samurai
{
    readonly IWeapon weapon;
    readonly IDress dress
    public Samurai(IWeapon weapon, IDress dress)
    {
        this.weapon = weapon;
        this.dress = dress;
    }

    public void Attack(string target)
    {
        this.weapon.Hit(target);
    }
    
     public void Wear(){


     }
}



。或者我还有其他选择???


. Or else do i have any other option???

推荐答案

您对DI的期望是什么?



DI没有神奇地扩展功能。

DI允许从外部显式传递依赖关系而不是隐式地实现依赖关系。

如果你在Samurai类中添加一些新的依赖,你必须提供将它们(DI)传递给构造函数的方法。



你的问题被问错了。它应该是:



我有一个Samurai类,它有一个新的依赖项添加到Dress。我是否必须将该依赖项传递给构造函数来实现DI?



答案:是。



干杯

Andi
What is your expectation on DI?

DI does not magically extend functionality.
DI allows to pass dependencies explicitly from outside instead of implicitly instanciating the dependencies.
If you add in your Samurai class some new dependency, you have to provide means to pass them (DI) to the constructor.

Your question is asked the wrong way round. It should be:

"I have a Samurai class that has a new dependency added to Dress. Do I have to pass that dependency to the constructor to implement DI?"

Answer: "yes".

Cheers
Andi


如果你要扩展一个Class来使用另一个接口的另一个实例,那么......是的......你将需要更改Class:如果你没有,那么Class会怎样是否知道或能够使用添加的界面?



然而,一个选项将扩展一个类继承自的大范围实体。或者注入一个接口的实例。



例如,你可以有一个名为ISamuraiGear的界面,可以包括Weapon和Dress。然后Samurai类将获得注入的实例。因此,您可以在不添加其他界面的情况下扩展WarriorGear。



但是,当时Samurai类的一个实例想要使用/调用它的一些属性/方法换装实例,是的,很明显,Samurai类的实例必须更改为显式使用/调用Dress实例。



imho,你引用的文章是DI的可怕例子,您可以在CodeProject上找到更好的解释:[ ^ ]。



我最喜欢的解释DI是John Munsch对如何解释5岁儿童的依赖注射?这一问题的经典答案:
If you are going to extend a Class to use another instance of another interface, then ... yes ... you will need to change the Class: if you didn't how would the Class ever "know" about, or be able to use, the added interface ?

However an option would be extend some larger-scope entity that the Class inherits from ... or is injected an instance of an interface of.

For example, you could have an interface called ISamuraiGear that could include both Weapon and Dress. The Samurai class would then get an instance of that injected. So, you could extend WarriorGear without adding another interface.

But, at the point an instance of the Samurai class would want to use/invoke some Property/Method of its Dress instance, yes, obviously the instance of the Samurai class will have to be changed to explicitly use/invoke the Dress instance.

imho, the article you cite is a terrible example of DI, and you can find better explanations here on CodeProject: [^].

My favorite explanation of DI is John Munsch's classic answer to the question "how do you explain dependency injection to a 5 year-old ?:"
当你自己从冰箱里拿出东西时,你可能会引发问题。你可能会把门打开,你可能得到妈咪或爸爸不希望你拥有的东西。你甚至可能正在寻找我们甚至没有或已经过期的东西。



你应该做的是说明需要,我需要喝点东西午餐时,然后我们会确保你坐下来吃东西。
When you go and get things out of the refrigerator for yourself, you can cause problems. You might leave the door open, you might get something Mommy or Daddy doesn't want you to have. You might even be looking for something we don't even have or which has expired.

What you should be doing is stating a need, "I need something to drink with lunch," and then we will make sure you have something when you sit down to eat.

[ ^ ]。链接是StackOverflow; Mark Seemann(曼宁出版社,2011年),Ch。在文章中引用了.NET中的依赖注入。 1,p。 4.



在那本书中Seemann说:依赖注入(DI)是一组相关的模式和原则。它是一种思考和设计代码的方法。这是一项特定的技术。使用DI的最终目的是在面向对象的范例内创建可维护的软件。...隔离的DI只是一个小东西,但它与大型复杂的紧密相连

面向对象软件设计的原则和模式。

[^]. The link is to StackOverflow; and the text is also quoted in "Dependency Injection in .NET" by Mark Seemann (Manning Press, 2011), Ch. 1, p. 4.

In that book Seemann states: "Dependency Injection (DI) is a set of related patterns and principles. It’s a way to think about and design code more than it’s a specific technology. The ultimate purpose of using DI is to create maintainable software within the object-oriented paradigm. ... DI in isolation is just a small thing, but it’s closely interconnected with a large complex
of principles and patterns for object-oriented software design."


这篇关于使用依赖注入注入不同类型的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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