IOC容器的使用;特别是温莎 [英] Usage of IoC Containers; specifically Windsor

查看:279
本文介绍了IOC容器的使用;特别是温莎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这个问题的答案是这样的obivous已经没有人打扰这个写作,但其晚,我真的不能让我的头解决这个问题。

I think the answer to this question is so obivous that noone has bothered writing about this, but its late and I really can't get my head around this.

我一直在读入IoC容器中,(温莎在这种情况下),我很想念你是如何从你的code各部分聊到容器中。

I've been reading into IoC containers (Windsor in this case) and I'm missing how you talk to the container from the various parts of your code.

我得到的DI,我一直在做穷人芒DI(空构造函数调用使用默认参数实现重载注射构造函数)一段时间,我完全可以看到容器的效益。然而,林缺少一个重要的一块信息;你应该怎么每一次引用容器你从它需要服务?

I get DI, I've been doing poor mans DI (empty constructors calling overloaded injection constructors with default parameter implementations) for some time and I can completely see the benefit of the container. However, Im missing one vital piece of info; how are you supposed to reference the container every time you need a service from it?

我创建一个单一的全球insance我绕过?当然不是!

Do I create a single global insance which I pass around? Surely not!

我知道我应该把这个:

WindsorContainer container = new WindsorContainer(new XmlInterpreter());

(例如)时,我想我的加载XML配置,但后来我该怎么办集装箱?是否创建一个新的容器,每次此后通过一些内部静态majicks坚持加载的配置或以其他方式,或做我每次都(我想不会,或者生命周期想不出工作)。

(for example) when I want to load my XML config, but then what do I do with container? Does creating a new container every time thereafter persist the loaded config through some internal static majicks or otherwise, or do I have to reload the config every time (i guess not, or lifecycles couldnt work).

未能明白这是$ P $从工作了生命周期是如何工作的pventing我,并获得与使用一些国际奥委会awsomeness

Failing to understand this is preventing me from working out how the lifecycles work, and getting on with using some IoC awsomeness

谢谢,

安德鲁

推荐答案

99这是每个应用程序一个容器实例的情况%。通常你初始化它的Application_Start(对于Web应用程序),<一个href=\"https://github.com/castleproject/Castle.MonoRail-READONLY/blob/45ac205867396b1b7ad287a872e5b20afd0af837/src/TempWeb/Global.asax.cs\"相对=nofollow>这样的。

99% of the cases it's one container instance per app. Normally you initialize it in Application_Start (for a web app), like this.

之后,它真的取决于容器的消费。例如,一些框架,如单轨并的 ASP.NET MVC 让您拦截实例(在这种情况下,控制器)的创建,所以你只要注册在容器中的控制器和它们的依赖关系就是这样,只要你得到的容器负责注入其依赖每个控制器照顾的请求。例如,见<一href=\"http://mvccontrib.$c$cplex.com/SourceControl/changeset/view/6aa25407de83#src%2fSamples%2fMvcContrib.Samples.WindsorControllerFactory%2fControllers%2fHomeController.cs\"相对=nofollow>这个ASP.NET MVC控制器。
在这些框架,你很少需要调用甚至引用您的类容器,这是推荐的用法。

After that, it's really up to the consumer of the container. For example, some frameworks, like Monorail and ASP.NET MVC allow you to intercept the creation of the instances (the controllers in this case), so you just register the controllers and their dependencies in the container and that's it, whenever you get a request the container takes care of injecting each controller with its dependencies. See for example this ASP.NET MVC controller. In these frameworks, you hardly ever need to call or even reference the container in your classes, which is the recommended usage.

其他框架不会让你在创建过程中很容易获得(如Web表单),所以你不得不求助于像的这个,或的所需要的依赖(即显式调用容器)。拉的依赖,使用静态网关像<一个容器href=\"https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/trunk/commons/Rhino.Commons/RhinoContainer/IoC.cs\"相对=nofollow>这个或由<一个描述的href=\"http://stackoverflow.com/questions/367178/usage-of-ioc-containers-specifically-windsor#367190\">maxnk.请注意,这样做,你实际使用的容器作为服务定位器,它不脱钩的事情,以及控制反转。 (见差值这里和的这里

Other frameworks don't let you get in the creation process easily (like Webforms) so you have to resort to hacks like this one, or pull the required dependencies (that is, explicitly calling the container). To pull dependencies, use a static gateway to the container like this one or the one described by maxnk. Note that by doing this, you're actually using the container as a Service Locator, which doesn't decouple things as well as inversion of control. (see difference here and here)

希望这会清除你的疑惑。

Hope this clears your doubts.

这篇关于IOC容器的使用;特别是温莎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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