Managing Constructor Dependency Injection (MS Unity)
I am building a multi-layered application and trying to keep the layers as much as possible so I'm using an IoC container for this purpose. Anyway, I'm trying to expand on this article to move my business logic validation into the service layer. I managed to resolve all dependency issues except the dependency of the ModelStateWrapper class on the ModelState itself. Here are my classes:
public interface IValidationDictionary
{
void AddError(string key, string errorMessage);
bool IsValid { get; }
}
public class ModelStateWrapper : IValidationDictionary
{
private ModelStateDictionary _modelState;
public ModelStateWrapper(ModelStateDictionary modelState)
{
_modelState = modelState;
}
public void AddError(string key, string errorMessage)
{
_modelState.AddModelError(key, errorMessage);
}
public bool IsValid
{
get { return _modelState.IsValid; }
}
}
The ModelStateWrapper class resides in a Services folder in my MVC3 application. While the IValidationDictionary is inside an Abstract folder inside my Services layer. In my Unity configuration I did the following:
.RegisterType<IValidationDictionary, ModelStateWrapper>(
new HttpContextLifetimeManager<IValidationDictionary>())
So, now is there anything I could do to inject the ModelState object into the ModelStateWrapper class using the IoC? Or do I have to explicitly/manually instantiate the ModelStateWrapper in the controllers and pass in the ModelState as an argume开发者_如何学JAVAnt?
Thanks in advance!
I think you need to move your modelstatewrapper class to a common assembly. You can reference this common assembly from your service layer, business logic layer, etc. The common assembly can contain your domain classes, dto's, service definitions, etc etc You create a bootstrapper class which registers all types from the common assembly into your container. Call this bootstrapper from the service, BL layer, etc.
I hope this helps
Greetings
精彩评论