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

  • Home
  • SEARCH
  • 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 481265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:58:37+00:00 2026-05-13T00:58:37+00:00

I have a wrapper on ModelStateDictionary which all my services accept. Is it possible

  • 0

I have a wrapper on ModelStateDictionary which all my services accept. Is it possible to configure the autofac to inject the controller ModelStateDictionary into the constructor of the wrapper and then inject it into service constructor?

//code
public class ModelValidation : IModelValidation { 
public ModelValidation(ModelStateDictionary msd){...}
..
..
}

public class CustomerService{
public CustomerService(IModelValidation mv){...}
..
}

Thanks

  • 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-13T00:58:38+00:00Added an answer on May 13, 2026 at 12:58 am

    Based on your comments I hereby revise my answer 🙂

    ModelStateDictionary is clearly not a service that should be resolved by the container, but rather data that should be provided at instantiation time. We can tell that from the fact that ModelState is owned by each Controller instance and is thus not available to the container at “resolve time”.

    Furthermore, each ModelValidation instance will be bound to a ModelStateDictionary instance and is thus also to be considered as data.

    In Autofac, when data must be passed to constructors (optionally in addition to other dependencies), we must use factory delegates. These delegates will handle dependency and data passing to the constructor. The sweet thing with Autofac is that these delegates can be autogenerated.

    I propose the following solution:

    Since both ModelValidation and CustomerService requires data in their constructors, we need two factory delegates (note: the parameter names must match the names in their corresponding constructor):

    public delegate IModelValidation ModelValidationFactory(ModelStateDictionary msd);
    public delegate CustomerService CustomerServiceFactory(ModelStateDictionary msd);
    

    Since your controllers shouldn’t know where these delegates comes from they should be passed to the controller constructor as dependencies:

    public class EditCustomerController : Controller
    {
        private readonly CustomerService _customerService;
    
        public EditCustomerController(CustomerServiceFactory customerServiceFactory
            /*, ...any other dependencies required by the controller */
              )
        {
            _customerService = customerServiceFactory(this.ModelState);
        }
    
    }
    

    The CustomerService should have a constructor similar to this (optionally handle some of this in a ServiceBase class):

    public class CustomerService
    {
        private readonly IModelValidation _modelValidation;
    
        public CustomerService(ModelStateDictionary msd,
                  ModelValidationFactory modelValidationFactory)
        {
            _modelValidation = modelValidationFactory(msd);
        }
    

    To make this happen we need to build our container like this:

    var builder = new ContainerBuilder();
    
    builder.Register<ModelValidation>().As<IModelValidation>().FactoryScoped();
    builder.Register<CustomerService>().FactoryScoped();
    
    builder.RegisterGeneratedFactory<ModelValidationFactory>();
    builder.RegisterGeneratedFactory<CustomerServiceFactory>();
    
    builder.Register<EditCustomerController>().FactoryScoped();
    

    So, when the controller is resolved (eg when using the MvcIntegration module), the factory delegates will be injected into the controllers and services.

    Update: to cut down on required code even more, you could replace CustomerServiceFactory with a generic factory delegate like I’ve described here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 230k
  • Answers 230k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You should use mouseenter and mouseleave. The mouseover and mouseout… May 13, 2026 at 2:00 am
  • Editorial Team
    Editorial Team added an answer A little more information might be helpful. If you want… May 13, 2026 at 2:00 am
  • Editorial Team
    Editorial Team added an answer Here are caching linksMSDN BeanSoftware Dotnetperls CodeProject Very good article… May 13, 2026 at 2:00 am

Related Questions

I have a class that I wish to expose as a remote service using
So I have a native 3rd party C++ code base I am working with
Is there anyway to verify if a Session has been disposed of by NHibernate?
I have a COM-Callable Wrapper on a .NET assembly. Some of the methods use
I have a shim library (shared, C++) which calls functions in another shared library

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.