在被动视图中实现事件的好方法是什么?

发布于 2024-08-10 08:47:01 字数 1358 浏览 2 评论 0 原文

我正在学习被动视图模式以保留我的 C# WinForms 应用程序 更容易测试和维护。

到目前为止效果很好,但我想知道是否有更好的方法来实现 事件比我现在做的方式(并保持它们可测试)。这是 它是什么样子的(省略与此示例无关的代码)。基本上是什么 我正在寻找是否有一种方法可以跳过连接事件 Presenter 和表单中,我更喜欢在 Presenter 中完成所有工作。

我的视图如下所示:

public interface IApplicationView
{
    event EventHandler Find;
}

我的演示者如下所示:

public class ApplicationPresenter
{
    private IApplicationView _view;
    private IApplicationDomain _domain;

    public ApplicationPresenter(IApplicationView view) : this(view, new ApplicationDomain()) {}
    public ApplicationPresenter(IApplicationView view, IApplicationDomain domain) {
        _view = view;
        _domain = domain;

        HookupEventHandlersTo(view);
    }

    private void HookupEventHandlersTo(IApplicationView view)
    {
        view.Find += delegate { FindAction(); };
    }

    public void FindAction()
    {
        // ...
    }
}

我的 WinForm 如下所示:

public partial class Form1 : Form, IApplicationView
{
    private ApplicationPresenter _presenter;
    public event EventHandler Find = delegate {};

    public Form1()
    {
        InitializeComponent();
        _presenter = new ApplicationPresenter(this);
        HookupEvents();
    }

    private void HookupEvents()
    {
        searchButton.Click += Find;
    }
}

谢谢!

I am learning the Passive View pattern to keep my C# WinForms application
easier to test and maintain.

It has worked well so far but I wonder if there is a better way to implement
Events than the way I am doing it now (and keeping them testable). This is
what it looks like (omitting code not relevant for this example). Basically what
I am looking for if is there is a way to skip hooking up events both in the
presenter and in the form, I would prefer doing all work in the presenter.

My view looks like this:

public interface IApplicationView
{
    event EventHandler Find;
}

My presenter looks like this:

public class ApplicationPresenter
{
    private IApplicationView _view;
    private IApplicationDomain _domain;

    public ApplicationPresenter(IApplicationView view) : this(view, new ApplicationDomain()) {}
    public ApplicationPresenter(IApplicationView view, IApplicationDomain domain) {
        _view = view;
        _domain = domain;

        HookupEventHandlersTo(view);
    }

    private void HookupEventHandlersTo(IApplicationView view)
    {
        view.Find += delegate { FindAction(); };
    }

    public void FindAction()
    {
        // ...
    }
}

My WinForm looks like:

public partial class Form1 : Form, IApplicationView
{
    private ApplicationPresenter _presenter;
    public event EventHandler Find = delegate {};

    public Form1()
    {
        InitializeComponent();
        _presenter = new ApplicationPresenter(this);
        HookupEvents();
    }

    private void HookupEvents()
    {
        searchButton.Click += Find;
    }
}

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人心善变 2024-08-17 08:47:01

使用 WinForms 学习 MVP 的另一个重要资源是 Jeremy Millers 构建您自己的 CAB 系列。我在学习时发现这非常有用,

查看到演示者通信将对您有用;这里有一个关于使用事件与直接调用的很好的讨论。更好的是, 事件聚合器一文提出了一种“发布/订阅”机制,可以用来代替事件,同时保持代码可测试。这是我个人比较喜欢的方法,并且取得了很好的成功。

Another great resource for learning MVP with WinForms is Jeremy Millers Build Your Own CAB series. I found this incredibly useful when I was learning,

The article on View to Presenter Communication will be useful to you; there is a good discussion here on using events vs making direct calls. Even better, the Event Aggregator article presents a "publish/subscribe" mechanism that can be used instead of events, while keeping the code testable. This is the approach that I personally prefer, and have had good success with.

过去的过去 2024-08-17 08:47:01

查看 这个示例说明了如何实现被动视图。它有一种很好的方法来连接/取消连接视图和控制器之间的事件,将大部分工作放在控制器中。

Check out this example of how to implement the Passive View. It has a good way of wiring/unwiring for events between the view and controller that puts most of the work in the controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文