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 プロジェクトに表示する 1 つの方法は、「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 に答える