IoC容器使用情况的反模式.为什么IoC容器如此复杂且使用得如此“花哨"?道路? [英] Antipatterns of IoC container usage. Why IoC containers are so complex and used so "fancy" way?

查看:93
本文介绍了IoC容器使用情况的反模式.为什么IoC容器如此复杂且使用得如此“花哨"?道路?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始认真考虑使用IoC容器会导致创建过度设计的解决方案(至少促使我尝试使用各种不必要的功能:).

I'm seriously start thinking that usage of IoC container provokes to create overdesigned solutions (at least it provokes me to try to use various unnecessary features:).

是时候将我的"IoC"反模式列表与社区的同步了..

It's the time to synchronize my "IoC" antipatterns list with community one's..

我的短暂经验表明,启动每个应用程序一次调用Resolve方法就足以解决一些基础结构单例,并以此启动瞬态对象工厂",这可能会产生新的寿命更短的谷物工厂".即使要使这些工厂线程安全(例如,每个线程创建一个实例),也很容易通过在工厂中添加10条代码行来实现....这些工厂仍然比库与IoC工具的集成"要简单得多.拦截?只需创建自己的包装器即可...生命周期管理器/依赖策略/父容器?在引导程序中仅调用一次解决",您就不会考虑了.

My short experience tell that it is absolutely enough to call Resolve method once per application at startup to resolve some infrastructure singletons and initiate with them "transient object's factory" that could produce new "smaller life time grain factories" . Even to make those factories thread safe (e.g. create one instance per thread) is so easy to achieve by adding 10 code lines into factory... Still those factories are much more simpler then "library's integration with IoC tool". Interception? Just create your own wrappers... Life time managers / dependency strategies/ parent containers? Call the Resolve only once at bootstrapper and you won't think about that.

您能否帮助我理解为什么开发人员在不同的应用程序层上多次调用Resolve(通过传递容器或通过将委托传递给容器),然后有很多事情要考虑?我真的很担心我会错过一些事情.

Could you help me to understand why developers call Resolve several times on different application layers (by passing container or by passing delegate to container) and then have a lot of things to think about? I really worry that I miss something.

推荐答案

当我不清楚如何使用IoC容器时,我决定停止使用它,因为我认为这只是简单依赖注入的过度复杂性

When it was not clear to me how to use an IoC container, I decided to stop using it, because I thought was just an overcomplication over the simple dependency injection.

这是事实,尽管在注入过量的情况下,即使没有IoC也有可能跌落. 不久前,我读了ninject作者的一些帖子,这使我大开眼界.

It is true though that even without IoC is possible to fall in the over-injection cases. A while ago I read some posts from the author of ninject that opened my mind.

您已经知道应该仅在上下文根内部使用注入器.但是,为了避免过度注入,我决定为注入工厂引入例外规则.

As you already know the injector should be used only inside the context root. However, in order to avoid over-injections, I decided to introduce an exception of the rule for injected factories.

在我的框架中,工厂(只有工厂)可以使用注入器容器.工厂在上下文根中绑定在容器中,因此可以注入.工厂成为有效的依赖关系,并使用注入器容器促进依赖关系注入,从而在其他对象内部创建新对象.

In my framework, factories (and only factories) can use the injector container. Factories are binded in the container in the context root and therefore can be injected. Factories become valid dependencies and are used to create new objects inside other objects, using the injector container to facilitate dependencies injection.

这篇关于IoC容器使用情况的反模式.为什么IoC容器如此复杂且使用得如此“花哨"?道路?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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