在asp.net MVC2使用Unity我的程序需要管理的容器的生命周期? [英] In asp.net MVC2 using unity does my program need to manage the lifetime of the container?

查看:81
本文介绍了在asp.net MVC2使用Unity我的程序需要管理的容器的生命周期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把团结容器创建/设置在Global.asax。并且使容器的静态属性上类,因为我不知道如何团结工程或容器是否需要保持活着,并参考其他地方。什么是团结的初始化/配置的推荐位置MVC 2?

I'm putting the unity container creation/setup in the global.asax. and making the container a static property on that class since i'm not sure how unity works or if the container needs to be kept alive and references elsewhere. What's the recommended location of unity initialization/configuration for mvc 2?

推荐答案

您不应该需要保留明确提及的容器。容器应线了请求的对象图(控制器在这种情况下),让出了道路。

You shouldn't need to keep an explicit reference around for the container. A container should wire up the requested object graph (Controllers in this case) and get out of the way.

看看在 MVCContrib IControllerFactory的特定容器的实现。

Take a look at the container-specific implementations of IControllerFactory in MVCContrib.

这是说,我喜欢WindsorControllerFactory很多比UnityControllerFactory更好,但你可以实现使用相同的模式(构造器注入)作为一个WindsorControllerFactory UnityControllerFactory。

That said, I like the WindsorControllerFactory a lot better than the UnityControllerFactory, but you could implement a UnityControllerFactory that uses the same pattern (Constructor Injection) as the WindsorControllerFactory.

如果你想像我们这样做,你的Global.asax应该是这样的:

If you imagine that we do that, your Global.asax should look like this:

var container = new UnityContainer();

// configure container

var controllerFactory = new UnityControllerFactory(container);
ControllerBuilder.Current.SetControllerFactory(controllerFactory);

的ControllerFactory 保存到容器的引用,这样你就可以让它走出去的范围中的Application_Start - 它会停留在因为的Con​​trollerFactory保持周围

controllerFactory holds a reference to the container, so you can let it go out of scope in Application_Start - it's going to stay around because the ControllerFactory stays around.

这篇关于在asp.net MVC2使用Unity我的程序需要管理的容器的生命周期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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