Can I create View and Presenter in different projects in the MVP template

I am studying the MVP pattern for my current project (Windows application). I have good experience in MVVM, using it in Silverlight and WPF. In MVVM, my view and ViewModel are used for a separate project and use the strong WPF bindings that they use to communicate with each other. But in MVP, in most cases I see on the Internet where View and moderator are in the same project.

So my questions are: - Is there a way to create View and Presenter in different projects? I mean View as Windows Application and Presenter as a class library project.

If so, how do both presentations and the speaker relate to each other.

+4
source share
1 answer

- .

, Windows. , Windows, .

, .

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

All Articles