【问题标题】:MVP pattern in winforms - Handling eventswinforms中的MVP模式——处理事件
【发布时间】:2014-04-16 01:49:25
【问题描述】:

我刚开始使用 C# 和 MVP 设计模式。 当涉及到事件处理时,我对具体的实现持怀疑态度。我知道,视图不应该知道演示者,演示者应该通过视图界面控制视图。

假设我有 2 个文本框并且想要检查错误。如果发生错误,我想更改文本框 Text 属性。创建一个 EventHandler 并使用 sender 对象来验证用户当前正在使用的女巫文本框是错误的方法吗?

类似:

IView:

interface IMainView
{
    event KeyPressEventHandler KeyPressed;
}

查看:

public partial class MainView : Form, IMainView
{
    public frmInterakcija()
    {
        InitializeComponent();
        this.textBox1.Name = "textBox1";
        this.textBox2.Name = "textBox2";
        new MainPresenter();
        Bind();
    }

    private void Bind()
    {

       this.textBox1.KeyPress += KeyPressed;
       this.textBox2.KeyPress += KeyPressed;
    }
}

演讲者:

class MainPresenter
{
    private IMainView _view;

    public MainPresenter(IMainView view) 
    {
        _view = view;
        this.initialize();

    }

    public void initialize()
    {
        _view.KeyPressed += _view_textBoxKeyPressed;
    }

    public void _view_textBoxKeyPressed(object sender, EventArgs e)
    {
        if (sender.GetType() == typeof(TextBox))
        {
            TextBox textBox = (TextBox)sender;
            if (textBox.Name.Equals("textbox1") 
                {...} // Do validation/changes on textbox1
            else ...

        }
    }
 }

或者我应该为我拥有的每个文本框创建事件处理程序并通过属性更新/处理错误而不是上面的这个? (我猜这会使我的代码变得多余)

什么是正确的方法?

【问题讨论】:

    标签: c# winforms design-patterns mvp


    【解决方案1】:

    恕我直言,presenter 应该不知道查看特定对象(例如代码中的 textbox)。这种逻辑不应该出现在presenter中。并且presenter一定不知道UI中控件的ID,那就更糟了。请记住,这样做的好处之一应该是您可以通过模拟视图来测试演示者,如果您有特定于 UI 的代码,您将无法对演示者进行单元测试。

    在我看来,这确实是两个不同的events,因为你在做不同的逻辑。我会提出两个不同的events,一个会做验证,另一个会做自己的逻辑。演示者不必检查发件人是 textbox 还是 textbox 的 id。另外,如果您有另一个文本框,那么在当前实现中您将需要另一个 if 条件。

    另外,在视图中,应该是new MainPresenter(this);

    【讨论】:

    • 感谢您的回复。我现在很清楚了。
    【解决方案2】:

    您的演示者绝对不应在其中包含特定于视图的类型(例如控件、事件等),因为在测试演示者的逻辑时,这些类型很难伪造。相反,您应该有类似以下的内容。

    IView:

    interface IMainView
    {
        // give these better names based on what they actually represent (e.g. FirstName and LastName)
        // you could also add setters if you needed to modify their values from the presenter
        string Text1 { get; } 
        string Text2 { get; }
    
        // provide a way to bubble up validation errors to the UI
        string ErrorMessage { get; set; }
    }
    

    演讲者:

    class MainPresenter
    {
        private IMainView _view;
    
        public MainPresenter(IMainView view) 
        {
            _view = view;
        }
    
        public void ValidateText1()
        {
            if (/* some validation is false */)
            {
                _view.ErrorMessage = "Text1 isn't valid";
            }
        }
    
        public void ValidateText2()
        {
            if (/* some validation is false */)
            {
                _view.ErrorMessage = "Text2 isn't valid";
            }
        }
     }
    

    查看:

    public partial class MainView : Form, IMainView
    {
        var readonly MainPresenter _presenter;
    
        public frmInterakcija()
        {
            InitializeComponent();
            _presenter = new MainPresenter(this);
        }
    
        private void textBox1_KeyPress(object sender, KeyPressEventArgs eventArgs)
        {
            _presenter.ValidateText1();
        }
    
        private void textBox2_KeyPress(object sender, KeyPressEventArgs eventArgs)
        {
            _presenter.ValidateText2();
        }
    
        #region Implementation of IMainView
    
        public string Text1
        {
            get { return textBox1.Text; }
        }
    
        public string Text2
        {
            get { return textBox2.Text; }
        }
    
        public string ErrorMessage
        {
            get { return labelErrorMessage.Text; }
            set { labelErrorMessage.Text = value; }
        }
    
        #endregion
    }
    

    【讨论】:

      猜你喜欢
      • 2011-01-27
      • 2010-10-26
      • 2021-11-07
      • 1970-01-01
      • 1970-01-01
      • 2020-08-31
      • 1970-01-01
      • 2022-01-23
      • 2011-01-01
      相关资源
      最近更新 更多