I am facing a problem of cyclic dependency when creating the following MVP design (for winforms) using windsor container.
My presenter depends on the view and model:
ConcretePresenter(IView view, IModel model)
{
this.view = view;
this.model = model;
}
My view depends on the presenter:
ConcreteView(ConcretePresenter presenter)
{
//actual requirement that the presenter use the current instance of the view and a model object
//new presenter(this, new model())
this.presenter = presenter;
}
I am registering all components using Windsor castle (in a separate composition root class) as below:
IWindsorContainer container;
container = new WindsorContainer();
container.Register(Component.For<ConcretePresenter>().ImplementedBy<ConcretePresenter>());
container.Register(Component.For<IModel>().ImplementedBy<ConcreteModel>());
container.Register(Component.For<IView>().ImplementedBy<ConcreteView>());
Resolving the View brings up the issue of cyclic reference issue:
container.Resolve<ConcreteView>(); //doesn't resolve because of cyclic dependency
A possible solution would be to remove the constructor injection from the view and resolve the presenter separately. But this causes me to use the container in two places which i was not looking to do and probably is wrong.
ConcreteView()
{
container.Resolve<ConcretePresenter>(); //resolving at 2 different points
}
Is there any better solution to this. Am I doing something wrong in MVP itself?