2

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 argument?

Thanks in advance!

Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
Kassem
  • 8,116
  • 17
  • 75
  • 116
  • possible duplicate of [Is there a good/proper way of solving the dependency injection loop problem in the ASP.NET MVC ContactsManager tutorial?](http://stackoverflow.com/questions/1453128/is-there-a-good-proper-way-of-solving-the-dependency-injection-loop-problem-in-th) – Darin Dimitrov Feb 27 '11 at 16:58
  • Apparently yes! I did not see that thread before. Thanks for pointing it out. But still that doesn't solve my problem... :/ – Kassem Feb 27 '11 at 20:02
  • Here's an answer on SO that comments on the given approach in the referenced article: http://stackoverflow.com/questions/4776396/how-to-inject-a-model-state-wrapper-with-ninject/4851953#4851953. – Steven Mar 01 '11 at 07:37

1 Answers1

0

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

Ian
  • 67
  • 2
  • 14