0

I used to code in ASP.Net MVC, now I have a WinForms project. I read that MVP pattern is best for WinForms. But, I'm confused on how to inject multiple dependencies into Presenter.

For example I need to load a view called "UserLoginView". The presenter requires 3 parameters.

public partial class UserLoginView : IUserLoginView
{
    public event Action OnFormLoad;

    private UserLoginPresenter _userLoginPresenter;

    public UserLoginView()
    {
         InitializeComponent();

         //This is my problem
         var userService = EngineContext.Current.Resolve<IUserService>();
         var authenticationService = EngineContext.Current.Resolve<IAuthenticationService>();

         _userLoginPresenter = new UserLoginPresenter(this, userService,
             authenticationService);
    }
}

public class UserLoginPresenter
{
    private readonly IUserLoginView view;
    private readonly IUserService _userService;
    private readonly IAuthenticationService _authenticationService;

    public UserLoginPresenter(IUserLoginView userLoginView,
        IUserService userService,
        IAuthenticationService authenticationService)
    {
        this.view = userLoginView;
        this._userService = userService;
        this._authenticationService = authenticationService;
    }
...

What is the right way to inject dependencies to presenter?

Please I need a hand guys. Thank you.

Vince Tino
  • 142
  • 13
  • 1
    I would say that your views should never instantiate presenters themselve. And I would strongly recommend you to pass your presenters to your views (I prefer via-constructor delegation) so that you could create presenters elsewhere. Just assume your views are subcomponents that typically need a presenter reference only -- if so, your views never care how to instantiate a presenter, hence you get rid of the whole complexity that might be hidden behind a presenter or its model. – Lyubomyr Shaydariv Dec 01 '15 at 13:36
  • @LyubomyrShaydariv thanks for the comment, I will take your advice. But I'm still confused on how to create a Presenter with multiple dependencies. Is it okay to always ask the IOC container for an instance, then pass it to the Presenter? – Vince Tino Dec 02 '15 at 02:59
  • Yes, absolutely. Presenters can typically just accept necessary instances. Also, out of my experience, you probably might want to be interested in delegating, let's say, service instances to models rather than presenters, as the latter can (and typically should) be a thin layer/mediator between models and views: presenters just should not contain business logic. – Lyubomyr Shaydariv Dec 02 '15 at 06:24
  • Still, I have no idea how to solve the problem. If you could possibly provide some code :) – Vince Tino Dec 03 '15 at 01:38
  • What is the most complicated thing for you so far? I mean, was my previous comment too vague? – Lyubomyr Shaydariv Dec 03 '15 at 12:14
  • @LyubomyrShaydariv there's nothing wrong with your comment. Actually, your answer is helpful. I just need some examples :) – Vince Tino Dec 07 '15 at 02:22
  • Sorry I couldn't help you. I have some generic code base for MVP, it's Java-oriented and it's really hard to extract the essentials, especially converting to C#. So that's why I was asking for suggestive Q/A. – Lyubomyr Shaydariv Dec 07 '15 at 08:23

0 Answers0