Example IModelBinderProvider implementation for ModelBinder constructor injection in MVC 3
Asked Answered
T

2

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?

Tillfourd answered 8/12, 2010 at 20:35 Comment(3)
IModelBinderProvider would be your own implementation. It just so happens I wrote a blog post about just this thing buildstarted.com/2010/12/02/… Hope this helpsCircumnutate
Yeah, that's working well. I just replace the CreateInstance()stuff with var instance = (IModelBinder) DependencyResolver.Current.GetService(type); Thanks! .Tillfourd
I still think it should be possible to make a more clean implementation using generics. I need to sleep on that, I think ;)Tillfourd
F
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.

Fustian answered 9/12, 2010 at 7:21 Comment(0)
C
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());
Callean answered 8/12, 2010 at 21:13 Comment(4)
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?Fustian
You could do it either way. I guess I just prefer the traditional approach.Callean
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.Callean
Have a look at my answer below... I am currently using this is a development project.Fustian

© 2022 - 2024 — McMap. All rights reserved.