如何在WinForms中将依赖项注入Presenter(MVP)?

时间:2015-12-01 11:19:15

标签: winforms dependency-injection mvp passive-view

我曾经在ASP.Net MVC中编码,现在我有一个WinForms项目。我读到MVP模式最适合WinForms。但是,我对如何将多个依赖项注入Presenter感到困惑。

例如,我需要加载一个名为“UserLoginView”的视图。演示者需要3个参数。

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;
    }
...

将依赖项注入演示者的正确方法是什么?

我需要一个帮手。谢谢。

1 个答案:

答案 0 :(得分:0)

您在哪里实例化UserLoginView?我会说你应该在同一个地方创建你的视图和演示者。与下面的示例类似,UserLoginClient.ShowUserLogin()解析依赖项,创建视图,并将演示者连接到视图。我不会在视图构造函数中创建presenter,因为视图不应该依赖于表示逻辑。

public class UserLoginClient
{
    public void ShowUserLogin()
    {
         var users = EngineContext.Current.Resolve<IUserService>();
         var auth = EngineContext.Current.Resolve<IAuthenticationService>();
         var view = new UserLoginView();
         var presenter = new UserLoginPresenter(view, users, auth);

         //Now show view or something
    }
}

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

    public UserLoginView()
    {
         InitializeComponent();
    }
}

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

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

编辑:创建新子视图并在MVP项目中显示它们的一种方法是让'MainPresenter'向'MainView'询问'CreateUserLoginView()'并返回对新视图的引用。就这样:

public class MainPresenter
{
    private IMainView view;
    ....

    public void OnUserLoginRequest()
    {
        // Resolve the using DI-framework or get them from a factory
        var users = EngineContext.Current.Resolve<IUserService>();
        var auth = EngineContext.Current.Resolve<IAuthenticationService>();

        var userLoginView = view.CreateUserLoginView();
        var presenter = new UserLoginPresenter(userLoginView, users, auth);
        ...
    }
}

public interface IMainView
{
    ...
    IUserLoginView CreateUserLoginView();
}

public class MainView : IMainView
{
    public IUserLoginView CreateUserLoginView()
    {
         return new UserLoginView();
    }
}