Click here to Skip to main content
14,336,321 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello, CodeProject.

I am working on implementation of MVP patter for WinForms application.

I have the following View interface declaration:

public interface IView
    event EventHandler<EventArgs> ViewShown;
    event EventHandler<EventArgs> ViewClosed;

    void ShowNormalView();
    ModalViewResult ShowModalView();
    void CloseView();

Lets say IPresenter is just base marking interface for every presenter:

public interface IPresenter

Presenter base class is the following:

public class PresenterBase<T> : IPresenter where T : class, IView
    public PresenterBase(T view)
        View = view;

    protecter T View { get; private set; }

Lets say we need to implement simple WinForm with list of some items and user can select any item in this list so we declaring new View interface:

public interface ISomeListView : IView
    IEnumerable<SomeEntity> SomeList { set; }

    event EventHandler<SomeEntity> EntitySelected;
    event EventHandler ListRequested;

And writing new Presenter:

public class SomeListPresenter : PresenterBase<ISomeListView>
    private readonly ISomeListManager mListManager;

    public BuildListPresenter(IBuildListView view, ISomeListManager listManager)
        : base(view)
        mListManager = listManager;

        view.EntitySelected += OnEntitySelected;
        view.ListRequested += OnListRequested;

    public void OnListRequested()
        View.SomeList = mListManager.GetSomeList();

    void OnEntitySelected(object sender, SomeEntity selectedItem)
        // Do something

So, lets think that we implemented ISomeListViewin win forms.

I have the following questions:

1. With what class our WinForms application context should work? View or Presenter?

We know that WinForm application entry point is Program.cs:

Application.Run(new SomeListForm());

As you can see, here we have to instantiate new SomeListForm object. That measn, that application context work directly with View. Is it OK? Or we should work with View only throw Presenter?

In that meaning we will have to declare public property View in presenter to be able to do something like this:

Application.Run(new SomeListPresenter(new SomeListForm()).View as Form);

But this looks ugly. What the main approach here?

2. How should i organize closing of some current View and open another View?

For example, in SomeListPresenter we have OnEntitySelected event handler. Lets say we have a buisness request to open SomeOtherView with selected SomeEntity. I can implement this inside SomeListPresenter by sending all related IView implementations to this presenter and call Show() method. But i think it made Presenter to complex. I want some external code to make a decision of what form should be next and how to pass parameters. So i have the following 2 possibilities:

A. My View generate EntitySelected event and this event should be handled in some other code like ApplicationContext. But that means that some other code controls of my View without Presenter. It can destroy any kind of incapsulation.

B. My Presenter generate EntitySelected event and this event should be handled in some other code like ApplicationContext. But this follows with the problem of instantiating of new View as a result of EntitySelected event handling. I mean, on EntitySelected event i want to Close current View and open some other View. And i need to somhow tell to application that main form is changed:

ApplicationContext.MainForm = some code

So, how should i orginize code to be able to implement such scenario withoud loosing of incapsulation or increasing complexity of Presenter or View?

Comments 11-Dec-12 4:21am
are asking a question or writing an article? :)
_yaRus_ 11-Dec-12 6:24am
Questions are in the end :). I just thought that this kind of explanation will help in undersutnding of my current situation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100