使用 Unity IoC 的 MVC2 - 如何解析控制器?
我有一个关于我设置的 Unity 容器如何解决控制器依赖关系的问题。我一直在寻找对此的解释,但没有找到任何关于这个主题的真正明确的内容。也许答案就在我眼前...看一下下面的代码,我相信很多 MVC 人员都看过:
protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
IController controller;
try
{
controller = container.Resolve(controllerType) as IController;
}
catch (Exception ex)
{
throw new InvalidOperationException(string.Format("Error resolving controller {0}", controllerType.Name), ex);
}
return controller;
}
这就是我在控制器工厂中对 GetControllerInstance
的重写我已经设置了。显然,控制器类型是从 Unity 容器中解析出来的,但是它们首先是如何在容器中注册的呢? MVC 框架如何知道在容器中注册类型?我意识到类型是从该容器中解析出来的,但我不喜欢不知道它是如何解析控制器类型的。
I have a question concerning how the Unity container I have set up is resolving controller dependencies. I've been searching around for an explanation of this but haven't found anything that is real clear on the subject. And maybe the answer is staring me in the face... Take a look at the following code that I'm sure many MVC guys have seen:
protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
IController controller;
try
{
controller = container.Resolve(controllerType) as IController;
}
catch (Exception ex)
{
throw new InvalidOperationException(string.Format("Error resolving controller {0}", controllerType.Name), ex);
}
return controller;
}
So that is my override of GetControllerInstance
in my controller factory that I have set up. Obviously, the controller types are being resolved out of the Unity container but how are they getting registered in the container in the first place? How does the MVC framework know to register types in the container? I realize that the types are being resolved out of that container but I don't like not knowing how it is resolving the controller types.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它是 DefaultControllerFactory 负责这。它的工作原理如下:
ASP.NET MVC 使用反射列出所有程序集中从
Controller
继承的所有类型并缓存它们。如果您确实想进一步深入了解源代码或使用反射器并在DefaultControllerFactory
类型上检查此方法:当请求到来时,它使用路由表来确定哪个是当前控制器并尝试在缓存的控制器类型列表中找到它。如果找到,它将调用传递给定类型的 GetControllerInstance 方法,以便 DI 框架可以提供给定类型的控制器实例。
It is the DefaultControllerFactory which is responsible for this. Here's how it works:
ASP.NET MVC uses reflection to list all types that inherit from
Controller
in all assemblies and caches them. If you really want to dig further look at the source code or use reflector and checkout this method on theDefaultControllerFactory
type:When a request comes it uses the routing table to determine which is the current controller and tries to find it in the list of cached controller types. If it finds one it calls the
GetControllerInstance
method passing the given type so that the DI framework could provide the controller instance given its type.MVC 不向 Unity 注册类型。
Unity 容器会检查您所请求的内容,看看它是否可以用它所知道的内容来实例化它。
MVC does not register the types with Unity.
The Unity container inspects what you ask for to see if it can instantiated it with what it knows about.