trailmax trailmax - 1 year ago 90
C# Question

Using Dependency Injection in ASP.NET MVC3 Model Binder

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

public class UserModelBinder : IModelBinder
public UserDataService userData { get; set; }

public object BindModel(
ControllerContext controllerContext,
ModelBindingContext bindingContext)
Guid UserID =

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

User user = userDataService.GetUser(UserID);

return user;

noticed the commented lines of code?

I do register the binder in

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

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

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

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

(could it be that
does not have an interface and I'm binding the object to itself:

I have another commented line in the code:

userDataService = DependencyResolver.Current

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()

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download