如果基础设施的依赖注入? [英] Should infrastructure dependencies be injected?

查看:146
本文介绍了如果基础设施的依赖注入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有了这样的记录器,安全,配置等基础设施项目,如果这些东西确实被注入到每一个需要他们或他们应该被注入到服务定位器,然后类可以使用的服务定位器来解决依赖类(或者一些其它机制)?

With infrastructure items like loggers, security, configuration and so forth, should these things really be injected to every class that needs them or should they be injected into a service locator and then the classes can use the service locator to resolve the dependencies (or some other mechanism)?

这只是看起来真是可笑具有10个参数的构建函数通过DI来满足相关的所有类。它是一种code气味海事组织。我能理解像仓库或服务代理/连接器,但不能登录。

It just looks really ridiculous with all classes having 10 parameter ctors to satisfy dependencies via DI. Its a code smell IMO. I can understand things like repositories or service proxies/connectors but not logging.

推荐答案

这一切都取决于你在哪里画的基础设施和的code中的其余部分之间的界限。你认为一个数据库连接是基础设施?我不知道。

It all depends on where you draw the line between infrastructure and the rest of the code. Do you consider a database connection to be infrastructure? I do not.

属性注入是一个code气味,因为它隐藏的依赖关系和阶级没有​​被正确初始化时,构造完成。只用它来解决循环依赖。

Property injection is a code smell since it hides the dependencies and the classes are not properly initialized when the constructor is done. Only use it to solve cyclic dependencies.

有关记录的非常特殊的情况下,我用一个单独来获得日志。

For the very specific case of logging I do use a singleton to get the logger.

Normaly我会同意你的AOP,但我不运行时AOP框架风扇和球队不理解AOP概念。他们不太了解的IoC概念

Normaly I would agree with you on AOP, but I'm not a fan of runtime AOP frameworks and the team doesn't understand AOP concepts. They barely understand IoC concepts

您可能会发现我的国际奥委会介绍有用的。

You might find my IoC introduction useful.

这篇关于如果基础设施的依赖注入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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