团结国际奥委会和MVC 3测试版 - 传递给IRepository构造控制器 [英] Unity IoC and MVC 3 Beta - Passing IRepository to Controller Constructor

查看:138
本文介绍了团结国际奥委会和MVC 3测试版 - 传递给IRepository构造控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC 3做了什么变化?我曾尝试在互联网上所有的例子设立统一作为我IoC容器,但我不断收到一个错误说,统一解决不了我的UserController的。这是我的构造我的UserController的:

 公共UserController中(IUserService userService)
{
    _userService = userService;
}

我注册了IUserService,这是没有问题的。我不断收到错误,不管我什么例子。有没有人有一个很好的教程,或code,与Asp.Net MVC 3?

工作

有关参考,我曾尝试<一个href=\"http://weblogs.asp.net/shijuvarghese/archive/2010/05/07/dependency-injection-in-asp-net-mvc-nerddinner-app-using-unity-2-0.aspx\"相对=nofollow>这个,<一个href=\"http://weblogs.asp.net/shijuvarghese/archive/2008/10/24/asp-net-mvc-tip-dependency-injection-with-unity-application-block.aspx\"相对=nofollow>这个,<一个href=\"http://www.davidhayden.com/blog/dave/archive/2008/02/14/ASPNETMVCFrameworkUnityDependencyInjectionContainerPartII.aspx\"相对=nofollow>这个和<一个href=\"http://blogs.microsoft.co.il/blogs/gilf/archive/2009/02/08/how-to-use-unity-container-in-asp-net-mvc-framework.aspx\"相对=nofollow>这个 ......以及大量的其他资源。

错误:

 类型UserController中无法构建。您必须配置为提供此值的容器。

ErrorLine:

 控制器= MvcUnityContainer.Container.Resolve(controllerType)作为一个IController;

配置:

  MvcUnityContainer.Container =新UnityContainer()RegisterType&LT; IUserService,UserService&GT;();ControllerBuilder.Current.SetControllerFactory(typeof运算(UnityControllerFactory));


解决方案

要回答你的问题是什么已经改变MVC3:MVC3现在有依赖注入的原生支持。伴随着这种变化已经到来的方式控制对象被激活了重新设计。看看布拉德·威尔逊的后期(约3.0 MVC整个系列)的详细信息:

http://bradwilson.typepad.com/blog/2010/10/service-location-pt10-controller-activator.html

开发谁previously由DefaultControllerFactory派生只是为了覆盖依赖注入目的GetControllerInstance方法现在应该实现IControllerActivator代替实施IControllerFactory。

总之,团结控制器工厂(还有Ninject控制器工厂)很可能会才会发布新的兼容版本被打破。快速谷歌没有发现这个,但我不知道它是否工作。

Did something change in MVC 3? I have tried all the examples on the Internet for setting up Unity as my IoC Container, but I keep getting an error saying that Unity cannot resolve my UserController. Here is my constructor on my UserController:

public UserController(IUserService userService)
{
    _userService = userService;
}

I have the IUserService registered, that is not the problem. I keep getting errors, no matter what example I try. Does anyone have a good tutorial, or code, that works with Asp.Net MVC 3?

For reference, I have tried this, this, this, and this ... and tons of others.

Error:

The type UserController cannot be constructed.  You must configure the container to supply this value.

ErrorLine:

controller = MvcUnityContainer.Container.Resolve(controllerType) as IController;

Configuration:

MvcUnityContainer.Container = new UnityContainer().RegisterType<IUserService, UserService>();

ControllerBuilder.Current.SetControllerFactory(typeof(UnityControllerFactory));

解决方案

To answer your question "What has changed in MVC3": MVC3 now has native support for dependency injection. Along with that change has come a redesign of the way controller objects are activated. Check out Brad Wilson's post (and the whole series about MVC 3.0) for more information:

http://bradwilson.typepad.com/blog/2010/10/service-location-pt10-controller-activator.html

Developers who previously implemented IControllerFactory by deriving from DefaultControllerFactory just to override the GetControllerInstance method for dependency injection purposes should now implement IControllerActivator instead.

In short, the unity controller factory (as well as the Ninject controller factory) are probably going to be broken until they release a new compatible version. A quick google did find this, however I have no idea if it works.

这篇关于团结国际奥委会和MVC 3测试版 - 传递给IRepository构造控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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