Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3346186
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:16:26+00:00 2026-05-18T01:16:26+00:00

TL;DR: How can I consolidate logic shared by two custom ModelBinder implementations into a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T01:16:26+00:00Added an answer on May 18, 2026 at 1:16 am

    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:

    public interface IPropertyBinder
    {
        void SetPropertyValue(object model, ModelBindingContext context);
    }
    

    Then, implement it using the parameters originally from EncodedPropertyModelBinder:

    public sealed class PropertyBinder : IPropertyBinder
    {
        private readonly IEncodingService _encodingService;
        private readonly string _propertyName;
    
        public PropertyBinder(IEncodingService encodingService, string propertyName)
        {
            _encodingService = encodingService;
            _propertyName = propertyName;
        }
    
        public void SetPropertyValue(object model, ModelBindingContext bindingContext)
        {
            var value = bindingContext.ValueProvider.GetValue(_propertyName);
    
            if(value != null && !value.AttemptedValue.IsNullOrEmpty())
            {
                var encodedValue = _encodingService.Encode(value.AttemptedValue);
    
                var property = model.GetType().GetProperty(_propertyName);
    
                property.SetValue(model, encodedValue, null);
            }
        }
    }
    

    Next, implement EncodedPropertyModelBinder using the new interface:

    public class EncodedPropertyModelBinder : DefaultModelBinder
    {
        private readonly IPropertyBinder _propertyBinder;
    
        public EncodedPropertyModelBinder(IPropertyBinder propertyBinder)
        {
            _propertyBinder = propertyBinder;
        }
    
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var model = base.BindModel(controllerContext, bindingContext);
    
            if(model != null)
            {
                _propertyBinder.SetPropertyValue(model, bindingContext);
            }
    
            return model;
        }
    }
    

    Finally, register two versions of the view model using Autofac named instances, passing in different configurations of PropertyBinder:

    builder.
        Register(c => new EncodedPropertyModelBinder(new PropertyBinder(c.Resolve<IEncodingService>(), "Answer")))
        .Named<EncodedPropertyModelBinder>("AnswerBinder");
    
    builder.
        Register(c => new EncodedPropertyModelBinder(new PropertyBinder(c.Resolve<IEncodingService>(), "SecretKey")))
        .Named<EncodedPropertyModelBinder>("SecretKeyBinder");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.