0
votes

EDIT

The particular part that I am confused about is the error.

5) Injection of dependency ModelStateDictionary into parameter dictionary of constructor of type ModelStateDictionary

ModelStateDictionary has a constructor that accepts a ModelStateDictionary object. Is this why / where the error is occurring? If so, how do I resolve it as ModelStateDictionary is not a n object that I can directly modify.

Original

I've been trying to get dependency injection setup to work on for my controllers on in an asp.net mvc app. But I end up with a cyclic dependency on the System.Web.Mvc.ModelStateDictionary. I've done some searching ... here and here and others and I've tried some of the suggestions like creating a property (maybe I just don't know where to place them) instead of passing it through the constructor. However it seems to me that ninject is having a problem with the System.Web.Mvc.ModelStateDictionary class or definition. I get the following error ...

Activation path:
5) Injection of dependency ModelStateDictionary into parameter dictionary of constructor of type ModelStateDictionary
4) Injection of dependency ModelStateDictionary into parameter modelState of constructor of type ModelStateWrapper
3) Injection of dependency IValidationDictionary into parameter validationDictionary of constructor of type ProjectService
2) Injection of dependency IProjectService into parameter prjService of constructor of type ProjectController
1) Request for ProjectController

My classes are pretty much defined as follows. I have obviously removed some of the extraneous stuff ...

public class ProjectController : Controller
{
    private IProjectService _prjService;
    private IMembershipService _membershipService;

    public ProjectController(IProjectService prjService,IMembershipService membershipService )
    {
        _membershipService = membershipService;
        _prjService = prjService;
    }

}


public class ProjectService : ServiceBase, IProjectService
{
    public ProjectService(IValidationDictionary validationDictionary) : base(validationDictionary) { }
}


public class ServiceBase
{
    private readonly IValidationDictionary _validationDictionary;

    public IValidationDictionary ValidationDictionary { get { return _validationDictionary; } }

    public ServiceBase(IValidationDictionary validationDictionary)
    {
        _validationDictionary = validationDictionary;
    }

}


public interface IProjectService
{
    // interface has other properties 

    IValidationDictionary ValidationDictionary { get; }
}

public class ModelStateWrapper : IValidationDictionary
{
    private ModelStateDictionary _modelState;

    public ModelStateWrapper(ModelStateDictionary modelState)
    {
        _modelState = modelState;
    }

    public ModelStateWrapper()
    {

    }

    public void AddError(string key, string errorMessage)
    {
        _modelState.AddModelError(key, errorMessage);
    }

    public bool IsValid
    {
        get { return _modelState.IsValid; }
    }
}

The definition of ModelStateDictionary is as follows ... at least as far as I think it pertains to this problem.

[Serializable]
public class ModelStateDictionary : IDictionary<string, ModelState>, ICollection<KeyValuePair<string, ModelState>>, IEnumerable<KeyValuePair<string, ModelState>>, IEnumerable
{
    public ModelStateDictionary();

    public ModelStateDictionary(ModelStateDictionary dictionary);
}

The bindings I've set up for ninject are as follows ...

        kernel.Bind<IMembershipService>().To<AuthMembershipService>();
        kernel.Bind<IProjectService>().To<ProjectService>();
        kernel.Bind<IValidationDictionary>().To<ModelStateWrapper>();

Please let me know if I can provide any more information ... I initially tried to use structuremap but couldn't make the DI work ... I seem to have at least got ninject set up and working for the most part.

Thanks,

1
Show us the code of the ModelStateWrapper. - Steven
apologies Steven, I thought I had added it. I've added it now. - Kevin Donde

1 Answers

1
votes

Ninject chooses the constructor with the most parameters it knows how to create by default. In this case the second constructor is choosen. This would result in a stackoverflow since it would have to create a ModelStateWrapper to inject into the ModelStateWrapper and andother to inject into the second one, ......

Unless there is a really good reason for the second construcotr you should simply delete it. Otherwise you have to give here enough information that we can understand why there is this second constructor.