是否有替代混蛋注射? (男子AKA可怜的注射通过默认构造函数) [英] Is there an alternative to bastard injection? (AKA poor man's injection via default constructor)

查看:146
本文介绍了是否有替代混蛋注射? (男子AKA可怜的注射通过默认构造函数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最常忍不住要使用王八注射液在少数情况下。当我有一个正确的依赖注入的构造函数:

I most commonly am tempted to use "bastard injection" in a few cases. When I have a "proper" dependency-injection constructor:

public class ThingMaker {
    ...
    public ThingMaker(IThingSource source){
        _source = source;
    }

不过,上课我打算为公共API (类,其他开发团队会消耗),我永远无法找到比写一个默认的混蛋的构造最一个更好的选择-likely需要依赖:

But then, for classes I am intending as public APIs (classes that other development teams will consume), I can never find a better option than to write a default "bastard" constructor with the most-likely needed dependency:

    public ThingMaker() : this(new DefaultThingSource()) {} 
    ...
}

这里的明显缺点是,这个创建于DefaultThingSource一个静态的依赖;理想情况下,就没有这样的相关性,而消费者总是会注入任何IThingSource他们想要的。然而,这是太难用了;消费者希望新的一个ThingMaker,开始工作使事情,否则在需要时再个月后,注射的东西。这使得其中一些选项在我看来:

The obvious drawback here is that this creates a static dependency on DefaultThingSource; ideally, there would be no such dependency, and the consumer would always inject whatever IThingSource they wanted. However, this is too hard to use; consumers want to new up a ThingMaker and get to work making Things, then months later inject something else when the need arises. This leaves just a few options in my opinion:

  1. 省略混蛋构造;强制ThingMaker的消费者了解IThingSource,了解如何ThingMaker与IThingSource交互,找到或编写一个具体的类,然后在自己的构造函数调用注入一个实例。
  2. 省略混蛋构造函数,并提供一个独立的工厂,容器或其他引导类/方法;以某种方式使消费者明白,他们不需要写自己的IThingSource;强制ThingMaker的消费者查找和了解工厂或引导程序并使用它。
  3. 保持混蛋构造,使消费者对新达的对象,并运行它,并与DefaultThingSource可选的静态依赖应对。

男孩,#3肯定看起来有吸引力。是否有其他更好的选择吗? #1或#2似乎不值得。

Boy, #3 sure seems attractive. Is there another, better option? #1 or #2 just don't seem worth it.

推荐答案

据我了解,这个问题涉及到如何暴露一个松耦合的API和一些适当的默认值。在这种情况下,你可能有一个很好的本地默认,在这种情况下,依赖可视为可选。处理可选依赖一种方法是使用属性注入而不是构造器注入 - 事实上,这是这类海报场景财产注射。

As far as I understand, this question relates to how to expose a loosely coupled API with some appropriate defaults. In this case, you may have a good Local Default, in which case the dependency can be regarded as optional. One way to deal with optional dependencies is to use Property Injection instead of Constructor Injection - in fact, this is sort of the poster scenario for Property Injection.

不过,庶子注射的真正的危险是,当默认的是海外默认,因为这将意味着默认构造函数沿不希望的耦合,实现默认大会一拖再拖。据我了解这个问题,但是,预期违约将起源于同一个组件,在这种情况下,我看不出有什么特别的危险。

However, the real danger of Bastard Injection is when the default is a Foreign Default, because that would mean that the default constructor drags along an undesirable coupling to the assembly implementing the default. As I understand this question, however, the intended default would originate in the same assembly, in which case I don't see any particular danger.

在任何情况下,你也可以考虑一个门面在我以前的答案之一描述:<一href="http://stackoverflow.com/questions/2045904/dependency-inject-di-friendly-library/2047657#2047657">Dependency注入(DI)&QUOT;友好&QUOT;库

In any case you might also consider a Facade as described in one of my earlier answers: Dependency Inject (DI) "friendly" library

顺便说一句,这里使用的术语是基于我的书。

BTW, the terminology used here is based on the pattern language from my book.

这篇关于是否有替代混蛋注射? (男子AKA可怜的注射通过默认构造函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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