views:

376

answers:

2

Hey all

I have an ASP.net MVC project in the works at the moment, and I'm wondering if the following could be possible: I have a custom ModelBinder class that has a reference to a service (essentially a fetcher) as a dependency. I want the dependency to be injected using an IoC container (currently Ninject) but there seems to be nowhere in the method chain that I can plug in something that says load the model binder from my IoC container.

My first thought is to have a generic object binder that then tries to retrieve a specific ModelBinder from the container, returning null if not found, and then stetting this up as a binder, i.e. something like: ModelBinders.Binders.Add(typeof(object),typeof(NinjectModelBinder));

but I’m unsure

  • a) if this will work
  • b) if it's really the right thing to do

I could forgo the resolving of the complex object until the Action method but it would be cleaner and more desirable to be able to provide the complex object (which is essentially loaded and built from the data access layer) as a parameter to the action.

Any thoughts/help appreciated.

A: 

I personally use setter injection in my scenario similar to yours. After looking it up, NInject calls this property injection. It works and gets the job done.

Ben Robbins
+2  A: 

I think you're going to have to make a service locator call either in the model binder, or to build up the model binder, or both.

    ModelBinders.Binders.Add(typeof(Customer), Resolve<CustomerBinder>());
Matt Hinze
Yes I figured that might be the case.
buildmaster