为什么你想要依赖注入没有配置? [英] Why would you want Dependency Injection without configuration?

查看:198
本文介绍了为什么你想要依赖注入没有配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读了这个问题中的精彩答案后, / a>,我看了Justin Etheredge的截图。这一切看起来都很不错,用最少的设置就可以直接从代码中获得DI。

After reading the nice answers in this question, I watched the screencasts by Justin Etheredge. It all seems very nice, with a minimum of setup you get DI right from your code.

现在问题是:一个不使用配置文件的DI框架?这不是使用DI基础设施的整个点,以便你可以改变行为(战略,所谓)后建立/释放/无论什么代码?

Now the question that creeps up to me is: why would you want to use a DI framework that doesn't use configuration files? Isn't that the whole point of using a DI infrastructure so that you can alter the behaviour (the "strategy", so to speak) after building/releasing/whatever the code?

任何人都可以给我一个很好的用例,使用非配置的DI,如Ninject验证?

Can anyone give me a good use case that validates using a non-configured DI like Ninject?

推荐答案

我不认为你不需要一个DI框架我想你想要一个DI框架与你需要的配置

I don't think you want a DI-framework without configuration. I think you want a DI-framework with the configuration you need.

我以春天为例。回到老的日子,我们习惯把所有的内容放在XML文件中,使一切都可以配置。

I'll take spring as an example. Back in the "old days" we used to put everything in XML files to make everything configurable.

当切换到完全注释的时候, 应用程式包含。因此,给定的
服务例如可以具有针对常规运行时的一个实现,其中在应用的存根版本中存在属于
的另一个实现。此外,当进行集成测试的接线时,您可能正在使用第三个实现。

When switching to fully annotated regime you basically define which component roles yor application contains. So a given service may for instance have one implementation which is for "regular runtime" where there is another implementation that belongs in the "Stubbed" version of the application. Furthermore, when wiring for integration tests you may be using a third implementation.

当以这种方式查看问题时,您很快就意识到大多数应用程序只包含非常有限的一组组件角色
在运行时 - 这些是实际上导致使用不同版本的组件的事情。通常,组件的给定实现始终绑定到此角色;它实际上是该实现的原因。

When looking at the problem this way you quickly realize that most applications only contain a very limited set of component roles in the runtime - these are the things that actually cause different versions of a component to be used. And usually a given implementation of a component is always bound to this role; it is really the reason-of-existence of that implementation.

所以如果你让配置只需指定你需要的组件角色,你可以离开没有更多的配置。
当然,总是会有异常,但是你只是处理异常。

So if you let the "configuration" simply specify which component roles you require, you can get away without much more configuration at all. Of course, there's always going to be exceptions, but then you just handle the exceptions instead.

这篇关于为什么你想要依赖注入没有配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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