TL;DR: How can I consolidate logic shared by two custom ModelBinder implementations into a single base class, when both implementations rely on Autofac to inject a (common) dependency into them?
While reviewing some code in an ASP.NET MVC project I’m working on, I realized that I have two custom model binders that essentially do they same thing. They both inherit from DefaultModelBinder, and they both encode a single property on two separate view model classes, using an IEncodingService that is injected into their constructors.
public class ResetQuestionAndAnswerViewModelBinder : DefaultModelBinder {
public ResetQuestionAndAnswerViewModelBinder(IEncodingService encodingService) {
encoder = encodingService;
}
private readonly IEncodingService encoder;
public override object BindModel(ControllerContext controllerContext,
ModelBindingContext bindingContext) {
var model = base.BindModel(controllerContext, bindingContext) as ResetQuestionAndAnswerViewModel;
if (model != null) {
var answer = bindingContext.ValueProvider.GetValue("Answer");
if ((answer != null) && !(answer.AttemptedValue.IsNullOrEmpty())) {
model.Answer = encoder.Encode(answer.AttemptedValue);
}
}
return model;
}
}
public class ConfirmIdentityViewModelBinder : DefaultModelBinder {
public ConfirmIdentityViewModelBinder(IEncodingService encodingService) {
encoder = encodingService;
}
private readonly IEncodingService encoder;
public override object BindModel(ControllerContext controllerContext,
ModelBindingContext bindingContext) {
var model = base.BindModel(controllerContext, bindingContext) as ConfirmIdentityViewModel;
if (model != null) {
var secretKey = bindingContext.ValueProvider.GetValue("SecretKey");
if ((secretKey != null) && !(secretKey.AttemptedValue.IsNullOrEmpty())) {
model.SecretKeyHash = encoder.Encode(secretKey.AttemptedValue);
}
}
return model;
}
}
I wrote a generic base class for both of these classes to inherit from:
public class EncodedPropertyModelBinder<TViewModel> : DefaultModelBinder
where TViewModel : class {
public EncodedPropertyModelBinder(IEncodingService encodingService,
string propertyName) {
encoder = encodingService;
property = propertyName;
}
private readonly IEncodingService encoder;
private readonly string property;
public override object BindModel(ControllerContext controllerContext,
ModelBindingContext bindingContext) {
var model = base.BindModel(controllerContext, bindingContext) as TViewModel;
if (model != null) {
var value = bindingContext.ValueProvider.GetValue(property);
if ((value != null) && !(value.AttemptedValue.IsNullOrEmpty())) {
var encodedValue = encoder.Encode(value.AttemptedValue);
var propertyInfo = model.GetType().GetProperty(property);
propertyInfo.SetValue(model, encodedValue, null);
}
}
return model;
}
}
Using Autofac, how would I inject the IEncodingService into the base class constructor, while forcing derived classes to provide the name of the property to encode?
I would actually approach this slightly differently, by favoring composition over inheritance. This means I would encapsulate the details of the property manipulation, and pass different implementations to a single model binder.
First, define an interface which represents binding a single property:
Then, implement it using the parameters originally from
EncodedPropertyModelBinder:Next, implement
EncodedPropertyModelBinderusing the new interface:Finally, register two versions of the view model using Autofac named instances, passing in different configurations of
PropertyBinder: