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

查看:203
本文介绍了使用依赖注入在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;
    }
}

注意到了code注释行?

我注册的的Global.asax 粘合剂为

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

所以我真的不能通过建设做注。

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

UserDataService 有一系列相关的: UserDataService - &GT; UserRepository - &GT;背景。因此,这将是很好的在这里使用Ninject。

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

现在的问题是,当我取消注释 [注入] 上方用户数据的声明,并试图获得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 没有一个界面,我的对象绑定到自己: kernel.Bind&LT; UserDataService&GT;()ToSelf(); ?? 的)

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

我还有一个注释行,在code:

I have another commented line in the code:

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

在此注释掉,在成立的作品,我得到正确的物体插入,但现在我们依赖于DependencyResolver,这是不是不是说 userDataService =新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?

推荐答案

一个模型绑定应该只是做数据转换,而不应该依赖于任何服务,当然不会触发任何数据库的通信。这应该在应用程序的其他部分来完成。您的操作方法应该只取的Guid用户id ,你应该叫 userDataService.GetUser(用户名); 从你的控制范围内(或者在一个lowel层,例如,一个内部业务命令)。通过这样做,你的问题就不会存在了。

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 controls (or in a lowel layer, for instance, inside a business command). By doing this, your problem will not exist.

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

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