views:

354

answers:

2

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

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

Schneider
Thanks for reply, yes i have this situation and i want to set ModelState of controller to IValidationDictionary in Bootswrapper file, not in controller. Can I do this? Or i need to set in on my own base controller class ?
Maksim Kondratyuk
A: 

I'm having the same exact issue. I was wondering if you found a way to resolve this? I really don't want to tie this logic to the controller when it should probably be in the bootstrapper.

Sergey
I moved this issue to controller:#region .ctorISourceService _sourceService;public SourceController(ISourceService sourceService){sourceService.Validation = new ModelStateWrapper(this.ModelState);_sourceService = sourceService;}#endregionand set instance for ISourceService in bootstrapper. I think about idea create base interface for all service like IValidationService with property for ModelState and create all services interfaces inherited form it, and create base controller where in constructor initialized IValidationDictionary in one place for code. Hope it helpful for you.
Maksim Kondratyuk
Thank you! I was thinking about something similar to that. But i didn't like that the ModelState instance gets passed around and in turns couples the validation to it. Here is my take on it if you're interested: http://stackoverflow.com/questions/905313/issues-with-my-mvc-repository-pattern-and-structuremap/924031#924031
Sergey