I’ve this controller
public class AdminController : Controller
{
private IAdministratorService _administratorService;
public AdminController(IAdministratorService administratorService)
{
_administratorService = administratorService;
}
}
And I’ve this:
private ModelStateDictionary _modelState;
public AdministratorService(IRepository repository, ModelStateDictionary modelState)
{
_repository = repository;
_modelState = modelState;
}
I’ve configured Dependency Injection for the Controllers so it would load properly except for sending the ModelState from the Container. How do you do it?
Here is one way to handle this problem…
Controller…
Service…
Wrapper Interface…
Wrapper implementation…
The use of the ModelStateWrapper allows the service classes to be loosely coupled with MVC. Although, we do have a tight coupling between the AdminController and the ModelStateWrapper because of the New statement, but I don’t really care because the model state is MVC specific anyway. By doing this, you would not need to register ModelStateWrapper or ModelState with StructureMap.
In your unit tests, you could call the Initialize method on the service after creating the controller to pass in your testing model state and check the validation errors.
I know you had said you were using a ModelStateWrapper, but just wanted to add a more complete example that might help others…