在ASP.NET MVC3模型绑定中使用依赖注入 [英] Using Dependency Injection in ASP.NET MVC3 Model Binder

查看:254
本文介绍了在ASP.NET MVC3模型绑定中使用依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVC3网站,尝试使用Ninject来解决我的依赖关系。我有以下情况:

I'm working on MVC3 website, trying to use Ninject to resolve my dependencies. I have the following scenario:

public class UserModelBinder : IModelBinder
{
    //[Inject]
    public UserDataService userData { get; set; }

    public object BindModel(
        ControllerContext controllerContext,
        ModelBindingContext bindingContext)
    {
        Guid UserID =
            (Guid)Membership.GetUser().ProviderUserKey;

        //userDataService = DependencyResolver.Current
        //    .GetService<UserDataService>();

        User user = userDataService.GetUser(UserID);

        return user;
    }
}

注意到注释的代码行? / em>

noticed the commented lines of code?

我将 Global.asax 中的活页夹注册为

ModelBinders.Binders[typeof(User)] = new UserModelBinder();

所以我无法通过施工进行注射。

So I can't really do injection through the construction.

UserDataService 具有一系列依赖关系: UserDataService - > UserRepository - >上下文。所以在这里使用Ninject会很好。

UserDataService has a chain of dependencies: UserDataService -> UserRepository -> Context. So it would be good to use Ninject here.

问题是,当我取消注释 [Inject] above userData 声明,并尝试获取Ninject注入对象作为参数,它不起作用的原因:我得到空引用异常。

The problem is, when I uncomment [Inject] above userData declaration, and try getting Ninject to inject object as a parameter, it does not work for some reason: I get null reference exceptions.

可能是 UserDataService 没有接口,我将对象绑定到自己:内核。绑定< UserDataService>()。ToSelf(); ??

(could it be that UserDataService does not have an interface and I'm binding the object to itself: kernel.Bind<UserDataService>().ToSelf(); ??)

我在代码中有另一个注释行: / p>

I have another commented line in the code:

userDataService = DependencyResolver.Current
    .GetService<UserDataService>();

当这是取消注释时,设置工作,我插入正确的对象,但现在我们依赖DependencyResolver,并不比说说 userDataService = new UserDataService()

When this is uncommented, the set up works, I get correct objects inserted, but now we depend on DependencyResolver and that is not much better than saying userDataService = new UserDataService()

我没有什么东西吗?有没有另外一种方法来注入一个对象作为参数,而不是引用对Ninject或DependencyResolver的依赖?

Am I missing something? Is there another way to inject an object as a parameter and not introducing dependency on Ninject or DependencyResolver?

推荐答案

做数据转换,不应该依赖任何服务,肯定不会触发任何数据库通信。这应该在你的应用程序的另一部分完成。您的Action方法应该只需要一个 Guid userId ,您应该从控制器内调用 userDataService.GetUser(UserID); (或下层,例如,在业务指南)。通过这样做,您的问题将不会存在。

A model binder should just do data conversion and should not depend on any services and certainly not trigger any database communication. That should be done in another part of your application. Your Action method should just take a Guid userId and you should call userDataService.GetUser(UserID); from within your controllers (or in a lower layer, for instance, inside a business command). By doing this, your problem will not exist.

这篇关于在ASP.NET MVC3模型绑定中使用依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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