这是“ Bastard注入反模式”的一个好例子吗? [英] Is this a good example of the "Bastard injection anti-pattern"?

查看:74
本文介绍了这是“ Bastard注入反模式”的一个好例子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到主要的开发人员正在编写这样的代码,并阅读Mark Seemann的书。NET中的依赖注入 。我想知道特定的新是否是外国,因此是混混注入。 ??

I see lead developers writing code like this and upon reading Mark Seemann's book "Dependency Injection in .NET" I'm wondering if the specific "new" is "foreign", thus "Bastard Injection"??

public class SessionInitServiceManager
{
    protected readonly ICESTraceManager _traceManager;
    protected readonly ILogger _logger;
    protected readonly IAggregateCalls _aggregator;
    protected readonly IMultiCoreRepository _repository;

    public SessionInitServiceManager(ICESTraceManager traceManager,
                                     ILogger logger,
                                     IAggregateCalls aggregator,
                                     IMultiCoreRepository repository)
    {
        _traceManager = traceManager;
        _logger = logger;
        _aggregator = aggregator;
        _repository = repository;
    }

    public SessionInitServiceManager() : this(new CESTraceManager(),
                                              new Logger("BusinessServices.authenticateUser"),
                                              new Aggregator(),
                                              new RepositoryFactory().BuildMultiCoreRepository()) { }


推荐答案

这肯定看起来像是混蛋注射液的经典例子。之所以这样,是因为您有四个外部默认值。外部默认值是指默认值,其中类型来自其他模块/项目/ DLL。我会建议在该定义中包括名称空间,因为名称空间可以表示边界,将来您可以在其中突破自己的模块。当您决定使用本地默认值时,这是更要注意的一点(将来我会将此拆分成自己的模块吗?)。

This for sure looks like a classic example of Bastard Injection. The reason why is because you have what appears as four Foreign Defaults. Foreign Default refers to a default value in which the type comes from a different module/project/DLL. I would propyl include namespace into that definition, because name spaces can signify boundaries in which at a future point you make breakout into its own module. This is more of being mindful about that when you decide to use a local default (Would I split this into its own module in the future?).

混蛋注入将不是所有这些类都生活在同一个模块中。之所以如此糟糕,是因为您将依赖关系拖到了一起,现在您的类已与这些类紧密耦合。如果我选择使用自己的日志记录版本,则即使不使用DLL,也必须使用DLL进行日志记录,等等,否定了模块化应用程序设计的好处。

The way this wouldn’t be Bastard Injection would be that all these classes live within the same module. The thing that makes this so bad is because you drag the dependencies along, and now your class is tightly coupled to these classes. If I choose to use my own version of logging I have to take along the DLL for logging and so on, even though I don’t use, negating the benefits of modular application design.

这篇关于这是“ Bastard注入反模式”的一个好例子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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