0

我以前在 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;
    }
...

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

请我需要一个手,伙计们。谢谢你。

4

1 回答 1

0

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

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();
    }
}
于 2015-12-06T21:18:38.690 回答