如何Form.howDialog()使用MVP和被动视图?

概要

我正在试验Windows窗体应用程序中的MVP模式。

我想让我的演示者和视图平台都不可知,所以如果我希望将我的应用程序移植到另一个平台,比如Web或移动平台,我只需要使用平台相关的GUI来实现视图,而演示者仍然可以平台独立。

现在我想知道, 如何使用MVP和被动视图ShowDialog()?

到目前为止,我的理解是,被动的观点不应该知道/关心任何主持人。 他们甚至不知道它存在。 所以,在这个问题的答案中提出的解决方案并不合适,根据我:重构Form.ShowDialog()代码为MVP

一些代码示例有助于理解:

ApplicationView

public partial class ApplicationForm : Form, IApplicationView {
    // I ensure that all the IApplicationView events are raised 
    // upon button clicks text changed, etc.
    // The presenter, which this view ignores the existence, 
    // is subscribed to the events this view raises.
}

ApplicationPresenter

public class ApplicationPresenter 
    : Presenter<IApplicationView>
    , IApplicationPresenter {
    public ApplicationPresenter(IApplicationView view) : base(view) {
        View.OnViewShown += OnViewShown;
    }

    public void OnViewShown() {
        IAuthenticaitonView authView = new AuthenticationForm();
        IAuthenticationPresenter authPresenter = new AuthenticationPresenter(authView);
        authPresenter.ShowDialog(); // 1.                       
    }
}
  • 这是我挣扎的地方。 ApplicationPresenter与Universe中的主服务器相似,可能通过IAuthenticationViewIAuthenticationPresenter知道用户身份验证。
  • IAuthenticationView

    public interface IAuthenticationView : IDialogView {
        string ErrorMessage { get; set; }
        string Instance { get; set; }
        IEnumerable<string> Instances { get; set; }
        string Login { get; set; }
        string Password {get; set; }
    
        void EnableConnectButton(bool enabled);
    
       event VoidEventHandler OnConnect;
       event SelectionChangedEventHandler OnDatabaseInstanceChanged;
       event VoidEventHandler OnLoginChanged;
       event VoidEventHandler OnPasswordChanged;
    }
    

    IDialogView

    public interface IDialogView : IView {
        void ShowDialog();
    }
    

    IView

    public interface IView { 
        void Show();
    
        event VoidEventHandler OnViewInitialize;
        event VoidEventHandler OnViewLoad;
        event VoidEventHandler OnViewShown;
    }
    

    IAuthenticationPresenter

    public interface IAuthenticationPresenter : IPresenter<IAuthenticationView> {
        void OnConnect();
        void OnViewDatabaseInstanceChanged(SelectionChangedEventArgs e);
        void OnViewLoginChanged();
        void OnViewPasswordChanged();
    }
    

    IPresenter<V>

    public interface IPresenter<V> where V : IView {
        V View { get; }
    
        OnViewInitialize();
        OnViewLoad();
        ShowView();
    }
    

    基于这些前提:

  • 演示者应该是平台不可知的
  • 只有视图知道如何显示/显示自己 (WPF,Mobile,Silverlight,Web,WinForms ...)
  • 该视图必须提供一种展现自我的方式
  • 该视图不必是平台不可知的 ,因为显示会因平台而异
  • 但是主持人应该点时间来展示自己
  • 我来到这里:

    IView

    public interface IView {
        void OnShowView();
    }
    

    IPresenter<V>

    public interface IPresenter<V>where V : IView {
        void ShowView();
        event VoidEventHandler OnShowView;
    }
    

    Presenter<V>

    public abstract class Presenter<V> : IPresenter<V> {
        public Presenter(V view) { 
            View = view;
            OnShowView += View.OnShowView;
        }
    
        public void ShowView() { raiseShowViewEvent(); }
    
        public event VoidEventHandler OnShowView;
    
        private void raiseShowViewEvent() { if (OnShowView != null) OnShowView(); }
    }
    

    所以,按照我迄今为止奋斗的逻辑,我通过这样做来解决它:

    ApplicationForm

    public partial class ApplicationForm : Form, IApplicationView {
        private void ApplicationForm_Shown(object sender, EventArgs e) { raiseOnViewShown();  }            
    
        private void raiseOnViewShownEvent() { if (OnViewShown != null) OnViewShown(); }
    }
    

    ApplicationPresenter

    public void OnViewShown() {
        // This method is the subscriber of the IView.OnViewShown event
        // The event is raised with the ApplicationForm_Shown event.
        IAuthenticationView authView = new AuthenticationForm();
        IAuthenticationPresenter authPresenter = new AuthenticationPresenter(authView);
        authPresenter.ShowView(); // 1.
    }
    
  • 这引发了IAuthenticationView订阅的OnShowView事件。 然后,回到表单中,视图对事件的响应是:
  • AuthenticationForm

    public partial class AuthenticationForm : Form, IAuthenticationView {
        public void OnShowView() { ShowDialog(); }
    }
    

    然后,视图将自身显示为对话框/模式窗口。

    链接地址: http://www.djcxy.com/p/30227.html

    上一篇: How to Form.ShowDialog() using MVP and Passive Views?

    下一篇: Where to put model data and behaviour?