使用依赖注入时,所有新操作符都去哪儿了? [英] When using dependency injection, where do all the new operators go?

查看:19
本文介绍了使用依赖注入时,所有新操作符都去哪儿了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读有关依赖注入的内容,并且我理解一个基本概念,即方法应该从调用者那里接收它需要的东西,而不是自己创建这样的项目.结果,new 操作符几乎完全从方法中删除(当然,某些基本对象将被免除 - 我发现的一个例子是诸如 StringBuilders 之类的东西这似乎让他们不得不传入是疯了).

I've been reading about dependency injection, and I understand the basic concept that a method should receive what it needs from its caller rather than creating such items itself. As a result, new operators get removed from the method almost entirely (certain basic objects would be exempt, of course - one example of this I found was for things like StringBuilders which seem like they'd be insane to have to pass in).

我的问题看似简单,但我怀疑答案实际上相当复杂:所有 new 运算符都去哪里了?

My question sounds deceptively simple, but I suspect the answer is actually fairly complex: Where do all of the new operators go?

答案乍一看似乎很简单:new 运算符只是被推送到调用需要该对象的方法的方法.然而,问题在于调用方法可能也在测试中,因此 new 从调用方法推到调用方法,直到最终到达根方法(此时看起来很疯狂,无法测试),这会创建大量的对象,以便在调用堆栈的各个点使用.当您考虑到该根方法是大量其他方法的根时,情况变得更加复杂,因此需要为每种可能性创建对象.这也会造成性能问题,因为您很快就会留下大量从未实际使用过的对象,但无论如何以防万一"必须实例化.

The answer seems straightforward at first: the new operator just gets pushed to the method which calls the method that needs the object. The problem with this, however, is that the calling method is likely also under test and so that new gets pushed up from calling method to calling method until eventually you get to a root method (which at this point seems crazy untestable) that creates an obscene amount of objects to get used at various points down the call stack. The situation becomes even more complicated when you consider that that root method is the root of a large variety of other methods, and so would need to create objects for every possibility. This also creates a performance problem since you would quickly be left with a large number of objects which are never actually used, but which must be instantiated anyway "just in case".

对我来说很明显我错过了一些重要的知识,这些知识对其他开发人员来说是如此明显,以至于没有人想在博客文章中描述它.然而,我显然不能知道我不知道的东西,所以我谦虚地问我秘密:所有的new运算符都去哪里了?

It is quite obvious to me that I have missed some vital piece of knowledge which is so apparent to other developers that nobody thinks to describe it in a blog post. However, I obviously cannot know what I do not know, so I humbly ask that I be let in on the secret: Where do all the new operators go?

我应该提到我正在使用 PHP 进行开发,所以每个请求都从同一点开始,而且 index.php 中的根方法"似乎需要涵盖应用程序的所有内容可以做,以确保它为它在当前请求中做的所有事情提供一个对象.同样,这里有一个根本性的误解,我真的很想纠正它.

I should mention that I'm developing with PHP, so every request starts at the same point, and it seems like that root "method" in index.php would need to cover everything the application could do, in order to ensure it provides an object for everything it will do in the current request. Again, there's a fundamental misunderstanding here, and I'd really love to correct it.

推荐答案

您几乎做对了:它们最终都在您的组合根中.请参阅对 Mark Seeman 的采访,更接近底部,解释原因这是一个很好的做法,很重要.

You've pretty much got it right: they all end up in your composition root. See this interview with Mark Seeman, nearer to the bottom, for an explanation of why this is a good practice and is important.

还要注意:依赖注入是针对服务的,而不是实体或值对象.所以例如注入 IUserRepository 是有道理的,但 IUser 不是那么多——当然也不是 StringBuilders.鉴于您对更原始类型的附加说明,这可能有助于澄清问题:划分实际上并不是它们有多原始,而是它们在系统中的作用.

Also of note: dependency injection is meant for services, not entities or value objects. So e.g. injecting IUserRepository makes sense, but IUser not so much---and certainly not StringBuilders. That might help clarify things, in light of your parenthetical about more primitive types: the division is not really how primitive they are, but rather what their role is in the system.

这篇关于使用依赖注入时,所有新操作符都去哪儿了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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