7

I need to wire my custom ModelBinder up to my DI container in MVC 3, but I can't get it working.

So. This is what I have: A ModelBinder with a constructor injected service.

public class ProductModelBinder : IModelBinder{
  public ProductModelBinder(IProductService productService){/*sets field*/}
  // the rest don't matter. It works.
}

My binder works fine if I add it like this:

ModelBinders.Binders.Add(typeof(Product),
     new ProductModelBinder(IoC.Resolve<IProductService>()));

But that is the old way of doing it, and I don't want that.

What I need is help on how to hook that modelbinder up to the IDependencyResolver I've registered.

According to Brad Wilson the secret is using a IModelBinderProvider implementation, but its very unclear as to how to wire that up. (in this post)

Does anyone have an example?

Christian Dalager
  • 6,603
  • 4
  • 21
  • 27
  • 1
    IModelBinderProvider would be your own implementation. It just so happens I wrote a blog post about just this thing http://buildstarted.com/2010/12/02/modelbinderproviders-automatic-binding-your-models-is-easy-as-pie/ Hope this helps – Buildstarted Dec 08 '10 at 20:45
  • Yeah, that's working well. I just replace the CreateInstance()stuff with var instance = (IModelBinder) DependencyResolver.Current.GetService(type); Thanks! . – Christian Dalager Dec 08 '10 at 21:06
  • I still think it should be possible to make a more clean implementation using generics. I need to sleep on that, I think ;) – Christian Dalager Dec 08 '10 at 21:09

2 Answers2

8

I faced the same situation when coding my MVC 3 app. I ended up with something like this:

public class ModelBinderProvider : IModelBinderProvider
{
    private static Type IfSubClassOrSame(Type subClass, Type baseClass, Type binder)
    {
        if (subClass == baseClass || subClass.IsSubclassOf(baseClass))
            return binder;
        else
            return null;
    }

    public IModelBinder GetBinder(Type modelType)
    {
        var binderType = 
            IfSubClassOrSame(modelType, typeof(xCommand), typeof(xCommandBinder)) ??
            IfSubClassOrSame(modelType, typeof(yCommand), typeof(yCommandBinder)) ?? null;

        return binderType != null ? (IModelBinder) IoC.Resolve(binderType) : null;
    }
}

Then I registered this in my IoC container (Unity in my case):

_container.RegisterType<IModelBinderProvider, ModelBinderProvider>("ModelBinderProvider", singleton());

This works for me.

kidoman
  • 2,402
  • 5
  • 26
  • 35
6

You need to write your own IModelBinderProvider and register it with the ModelBinderProviders.BinderProviders collection:

public class YourModelBinderProvider : IModelBinderProvider {
    public IModelBinder GetBinder(Type modelType) {
         if(modelType == typeof(Product)) {
             return new ProductModelBinder(...);
         }
         return null;
    }
}

In Global.asax:

ModelBinderProviders.BinderProviders.Add(new YourModelBinderProvider());
marcind
  • 52,944
  • 13
  • 125
  • 111
  • Is it better to register the IModelBinderProvider in IoC so that MVC automatically requests it or register it explicitly using BinderProviders.Add(...) ? Also, if we are trying to handle multiple binders (based on the model type), is there a pattern you would recommend? – kidoman Dec 09 '10 at 07:14
  • You could do it either way. I guess I just prefer the traditional approach. – marcind Dec 09 '10 at 07:44
  • How are you trying to handle multiple binders? The firs binder provider that returns a non-null binder wins. So it's just a matter of ordering things appropriately. – marcind Dec 09 '10 at 07:44
  • Have a look at my answer below... I am currently using this is a development project. – kidoman Dec 09 '10 at 08:57