这是&QUOT一个很好的例子;庶子注射反模式" ? [英] Is this a good example of "Bastard Injection anti-pattern" ?

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

问题描述

我看到铅开发人员编写这样的代码并在使用.NET依赖注入读书马克塞曼的书,我想知道如果特定的新是洋,从而混蛋注入??

 公共类SessionInitServiceManager 
{
保护只读ICESTraceManager _traceManager;
保护只读ILogger _logger;
保护只读IAggregateCalls _aggregator;
保护只读IMultiCoreRepository _repository;

公共SessionInitServiceManager(ICESTraceManager traceManager,
ILogger记录仪,
IAggregateCalls聚合,
IMultiCoreRepository库)
{
_traceManager = traceManager;
_logger =记录仪;
_aggregator =聚合;
_repository =库;
}

公共SessionInitServiceManager():这个(新CESTraceManager(),
新记录仪(BusinessServices.authenticateUser),
新聚合器(),
新RepositoryFactory()。BuildMultiCoreRepository()){}


解决方案

这肯定看起来像注射庶子一个典型的例子。究其原因,就是因为你有什么表现为四家外资默认。外国默认是指其中的类型来自不同模块/项目/ DLL中的默认值。我会丙基包括命名空间到这一定义,因为名字空间可以表示在在未来的时候,你做出突破到它自己的模块的边界。当你决定要使用本地默认的(我将在未来分裂成它自己的模块呢?)这是更多的被铭记这一点。



方式这止跌T巴斯塔德被注入将是所有这些类生活在同一模块中。使这个如此糟糕的事情是,因为你一起拖动礼仪,现在你的类是紧密耦合到这些类。如果我选择用我自己的日志记录的版本,我必须沿着DLL文件记录等需要,即使我不使用,否定的模块化应用设计带来的好处。


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()) { }

解决方案

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 it's own Module in the future?).

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 decencies 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.

这篇关于这是&QUOT一个很好的例子;庶子注射反模式" ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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