Created
November 22, 2010 10:50
-
-
Save DerAlbertCom/709805 to your computer and use it in GitHub Desktop.
DependicyInjection in ModelBinder MVC 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Web.Mvc; | |
namespace NewInMVC3.ModelBinder | |
{ | |
public class GenericModelBinderProvider : IModelBinderProvider | |
{ | |
public IModelBinder GetBinder(Type modelType) | |
{ | |
var genericType = typeof (IModelBinder<>).MakeGenericType(modelType); | |
return (IModelBinder) DependencyResolver.Current.GetService(genericType); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Mvc; | |
namespace NewInMVC3.ModelBinder | |
{ | |
public interface IModelBinder<T> : IModelBinder where T : class {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Mvc; | |
using NewInMVC3.Models; | |
namespace NewInMVC3.ModelBinder | |
{ | |
public class PersonModelBinder : IModelBinder<Person> | |
{ | |
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) | |
{ | |
return new Person(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment