Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, CodeProject.

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

I have the following View interface declaration:

C#
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:

C#
public interface IPresenter
{
}


Presenter base class is the following:

C#
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:

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

    event EventHandler<SomeEntity> EntitySelected;
    event EventHandler ListRequested;
}


And writing new Presenter:

C#
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:

C#
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
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:

C#
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
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:

C#
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?

Thanks!
Posted
Comments
AnkitGoel.com 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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900