Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controller ModelState with ModelStateWrappper

HI, to all, i am use Structure Map to implement dependency-injection. I created ModelStateWrapper class to send Model state in service layer, which in constructor get reference to ModelState of controller. In bootswrapper i registered my type:

ForRequestedType<ISourceService>()
            .TheDefaultIsConcreteType<SourceService>();
ForRequestedType<IValidationDictionary>()
        .TheDefaultIsConcreteType<ModelStateWrapper>();

How i can give reference of controller's model state to ModelStateWrapper here ?

p.s. sorry for my english :)

like image 634
Maksim Kondratyuk Avatar asked May 17 '26 20:05

Maksim Kondratyuk


1 Answers

You need to provide more information, but this is my best guess as to what you have:

public class ModelStateWrapper : IValidationDictionary
{
    ...
     private readonly ModelState _modelState;
     public ModelStateWrapper(ModelState modelState)
     {
          _modelState = modelState;
     }
    ...
}

If you want to pass a variable (the controller's model state in this case) to the ModelStateWrapper you almost certainly need to do that explicitly by calling the ObjectFactory.

Example:

MyController : Controller 
{
   ...
   public MyAction()
   {
      ...
      IValidationDictionary validationDictionary = ObjectFactory
          .With<ModelState>(this.ModelState)
          .GetInstance<IValidationDictionary>();
      ...
   }
   ...
}

See this documentation for details:

Passing Arguments to StructureMap at Runtime

like image 175
Jack Ukleja Avatar answered May 19 '26 15:05

Jack Ukleja



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!