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

Can I create View and Presenter in different Project in MVP Pattern

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

所以我的问题是:- 有什么方法可以在不同的项目中创建 View 和 Presenter?我的意思是查看为 Windows 应用程序和演示者为 Class 库项目。

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

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

您的演示器和视图界面可以包含在 class 库项目中,windows 应用程序可以引用该项目。您在 windows 应用程序项目中创建的任何具体视图都可以实现适当的视图接口。

下面的简单示例显示了 classes 可能如何交互。

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...
    }
}