Controller ModelState with ModelStateWrappper

2019-05-11 04:36发布

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 :)

1条回答
姐就是有狂的资本
2楼-- · 2019-05-11 04:44

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

查看更多
登录 后发表回答