c# - 我可以在 MVP 模式的不同项目中创建 View 和 Presenter

标签 c# mvp webformsmvp

我正在为我当前的项目(Windows 应用程序)学习 MVP 模式。 我在 Silverlight 和 WPF 中使用 MVVM 方面有很好的工作经验。 在 MVVM 中,我的 View 和 ViewModel 曾经位于单独的项目中,并使用 WPF 的强绑定(bind)来相互通信。 但是在 MVP 中,我在 Internet 上看到的大多数示例中,View 和 Presenter 在同一个项目中。

所以我的问题是:- 有什么方法可以在不同的项目中创建 View 和 Presenter?我的意思是将 View 作为 Windows 应用程序,将 Presenter 作为类库项目。

如果是,那么我的 View 和 Presenter 如何相互引用。

最佳答案

您的演示者应该只通过界面与 View 通信。

您的presenter 和view 接口(interface)可以包含在windows 应用程序可以引用的类库项目中。您在 Windows 应用程序项目中创建的任何具体 View 都可以实现适当的 View 界面。

下面的简单示例显示了类如何交互。

ClassLibrary.dll

public class Presenter {

    // Repository class used to retrieve data
    private IRepository<Item> repository = ...;

    public void View { get; set; }

    public void LoadData() {
        // Retrieve your data from a repository or service
        IEnumerable<Item> items = repository.find(...);
        this.View.DisplayItems(items);
    }
}

public interface IView {
    void DisplayItems(IEnumerable<Item> items);
}

WindowsApplication.dll

public class ConcreteView : IView {

    private Button btn
    private Grid grid;
    private Presenter presenter = new Presenter();        

    public ConcreteView() {
        presenter.View = this;
        btn.Click += (s, a) => presenter.LoadData();
    }

    public void DisplayItems(IEnumerable<Item> items) {
        // enumerate the items and add them to your grid...
    }
}

关于c# - 我可以在 MVP 模式的不同项目中创建 View 和 Presenter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30930837/

相关文章:

c# - VAPIX将音频发送到Axis IP摄像机

c# - 在 Unity 中实现 Span<T>

c# - 如何将 Ninject 和 MVC 与单个接口(interface)的多个实现一起使用?

c# - 将参数传递给 CosmosDB 存储过程

java - 如何将模型与 View 分离?

c# - WinForms 中的模型 View 展示器

java - 如何在 View/Display 中模拟点击事件以进行测试?

asp.net - 在 WebFormsMVP 中管理 ViewState

asp.net - 有人深入了解 ASP.NET WebFormsMVP 吗?

c# - 使用简单注入(inject)器和 WebForms MVP 将运行时值传递给构造函数