“私生子注射"和“私生子注射"的真正区别是什么?和“穷人的注射" [英] What is the real difference between "Bastard Injection" and "Poor Man's Injection"

查看:25
本文介绍了“私生子注射"和“私生子注射"的真正区别是什么?和“穷人的注射"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dependency Injection in .NET 一书中我知道应该在 当您使用IoC容器时,应用程序的>组合根对我很有意义.

From the Dependency Injection in .NET book I know that the object graph should be created at the Composition Root of the application which makes a lot of sense to me when you are using an IoC Container.

在我看到的所有尝试使用 DI 的应用程序中,总是有两个构造函数:

In all the applications I've seen when an attempt to use DI is being made, there are always two constructors:

  • 将依赖项作为参数和
  • 默认"一个没有参数,反过来调用另一个newing"删除所有依赖

然而,在上述书中,这被称为Bastard Injection 反模式".这就是我以前所知道的穷人注射液".

In the aforementioned book, however, this is called the "Bastard Injection anti-pattern" and that is what I used to know as "Poor Man's Injection".

现在考虑到这一切,我会说穷人的注射"将只是不使用 IoC Container,而是在上述 Composition Root 上手动编码所有对象图.

Now considering all this, I would say then that "Poor Man's Injection" would be just not using an IoC Container and instead coding all the object graph by hand on the said Composition Root.

所以我的问题是:

  1. 我是否正确理解了这些概念,还是完全偏离了轨道?
  2. 如果您仍然需要在 IoC 容器中注册所有依赖项,而不是在完全相同的组合根中手动编码它们,那么使用 IoC 容器的真正好处是什么?
  3. 如果我误解了什么是穷人注射液"真的是,有人可以澄清一下吗?

推荐答案

说到 DI,有很多术语的使用相互冲突.术语穷人的 DI 也不例外.对一些人来说,这意味着一件事,而对另一些人来说,这意味着不同的事情.

When it comes to DI, there's a lot of conflicting use of terminology out there. The term Poor Man's DI is no exception. To some people, it means one thing and to others it means something different.

我想用这本书做的一件事是为 DI 提供一种一致的模式语言.当涉及到所有这些使用冲突的术语时,我有两个选择:提出一个全新的术语,或者选择最普遍的用法(根据我的主观判断).

One of the things I wanted to do with the book was to supply a consistent pattern language for DI. When it came to all of those terms with conflicting use, I had two options: Come up with a completely new term, or pick the most prevalent use (according to my subjective judgment).

总的来说,我更喜欢重复使用现有的术语,而不是组成一种全新的(因此是陌生的)模式语言.这意味着在某些情况下(例如Poor Man's DI),您对名称的概念可能与书中给出的定义不同.这经常发生在模式书上.

In general, I've preferred to re-use existing terminology instead of making up a completely new (and thus alien) pattern language. That means that in certain cases (such as Poor Man's DI), you may have a different notion of what the name is than the definition given in the book. That often happens with patterns books.

至少让我感到欣慰的是,这本书似乎已经完成了对穷人的 DI 和私生子注射的准确解释,因为 O.P. 中给出的解释是正确的.

At least I find it reassuring that the book seems to have done its job of explaining exactly both Poor Man's DI and Bastard Injection, because the interpretation given in the O.P. is spot on.

关于 DI 容器的真正好处,我会向您推荐这个答案:反对控制反转容器的论据

Regarding the real benefit of a DI Container I will refer you to this answer: Arguments against Inversion of Control containers

P.S.2018 年 4 月 13 日: 我想指出,我多年前开始承认,Poor Man's DI 这个词在传达信息方面做得很差(原文如此!)原则的本质,所以多年来,现在,我改为称其为 PureDI.

P.S. 2018-04-13: I'd like to point out that I've years ago come to acknowledge that the term Poor Man's DI does a poor (sic!) job of communicating the essence of the principle, so for years, now, I've instead called it Pure DI.

P.P.S.2020 年 7 月 17 日:我们从 第二版中删除了术语Bastard Injection/a>.在第二版中,我们只是使用更通用的术语Control Freak 来指定您的代码依赖[s] 在除组合根之外的任何地方的易失性依赖".

P.P.S. 2020-07-17: We removed the term Bastard Injection from the second edition. In the second edition we simply use the more general term Control Freak to specify that your code "depend[s] on a Volatile Dependency in any place other than a Composition Root."

这篇关于“私生子注射"和“私生子注射"的真正区别是什么?和“穷人的注射"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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