0

I'm using a custom mvp framework, that when a view is created it tries to resolve for it's correct presenter and creates a new instance of it, but the presenter needs the view for its constructor.

Is there a way to inject dependency of the view into the constructor of the presenter using simple injector?

I've tried to use lifetime scoping but, since the view resolves/creates the presenter in its own constructor it seems that it's not resolved yet when it creates the instance of the presenter, and injects a new instance and making an infite loop and a stack overflow exception

EDIT:

The code looks something like this:

public class MyView1 : IView
{
    protected IPresenter<MyView> presenter;

    public MyView1()
    {
       /// some cocde here;
       presenter = ResolvePresenterBinding();
    }

    IPresenter<MyView> ResolvePresenterBinding()
    {
       //some stuff here to resolvethe apropiate Presenter and create the instance using the IOC Container
    }    
}

public interface IPresenter<TView> where TView : IView
{
}

public class MyPresenter1 : IPresenter<MyView>
{
    IView View;
    public MyPresenter1(IView view, //other dependencies here)
    {
        this.View = view;
    }
}
4
  • Can you show some code to illustrate the question?
    – Steven
    Jun 30, 2015 at 4:08
  • Actually the original code is in this article codeproject.com/Articles/522809/… . It already has support for Unity, Ninject and Struture Map, but I'm already using simple injector and I prefer it over the other IOC frameworks
    – DkAngelito
    Jun 30, 2015 at 5:46
  • Perhaps this q/a is useful to you: stackoverflow.com/questions/15123515/…
    – Steven
    Jun 30, 2015 at 6:04
  • It was very helpful Steven, since this is a winforms port of WebFormsMvp. I tried to do what you said, to make the view a property instead of a constructor argument, but that can't be done easily since the view type is defined as covariant. Your proposed PresenterFactory I think that shouldnt work for me (haven't tested) since the views are already registered and I guess that ResolveUnregisteredType is not going to be called, so I think that creating a new container would work (like the trick with unity) but I'm not sure if this is a good practice and how it's going to impact the performance
    – DkAngelito
    Jun 30, 2015 at 7:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.