Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should Application.Run() be called for the main presenter of a MVP WinForms app?

Tags:

c#

mvp

winforms

I'm learning to apply MVP to a simple WinForms app (only one form) in C# and encountered an issue while creating the main presenter in static void Main(). Is it a good idea to expose a View from the Presenter in order to supply it as a parameter to Application.Run()?

Currently, I've implemented an approach which allows me to not expose the View as a property of Presenter:

    static void Main()
    {
        IView view = new View();
        Model model = new Model();
        Presenter presenter = new Presenter(view, model);
        presenter.Start();
        Application.Run();
    }

The Start and Stop methods in Presenter:

    public void Start()
    {
        view.Start();
    }

    public void Stop()
    {
        view.Stop();
    }

The Start and Stop methods in View (a Windows Form):

    public void Start()
    {
        this.Show();
    }

    public void Stop()
    {
        // only way to close a message loop called 
        // via Application.Run(); without a Form parameter
        Application.Exit();
    }

The Application.Exit() call seems like an inelegant way to close the Form (and the application). The other alternative would be to expose the View as a public property of the Presenter in order to call Application.Run() with a Form parameter.

    static void Main()
    {
        IView view = new View();
        Model model = new Model();
        Presenter presenter = new Presenter(view, model);
        Application.Run(presenter.View);
    }

The Start and Stop methods in Presenter remain the same. An additional property is added to return the View as a Form:

    public void Start()
    {
        view.Start();
    }

    public void Stop()
    {
        view.Stop();
    }

    // New property to return view as a Form for Application.Run(Form form);
    public System.Windows.Form View
    {
        get { return view as Form(); }
    }

The Start and Stop methods in View (a Windows Form) would then be written as below:

    public void Start()
    {
        this.Show();
    }

    public void Stop()
    {
        this.Close();
    }

Could anyone suggest which is the better approach and why? Or there even better ways to resolve this issue?

like image 393
anonymous Avatar asked Dec 07 '22 03:12

anonymous


1 Answers

What about the following:

// view
public void StartApplication() // implements IView.StartApplication
{ 
    Application.Run((Form)this);
}

// presenter
public void StartApplication()
{
    view.StartApplication();
}

// main
static void Main()     
{     
    IView view = new View();     
    Model model = new Model();     
    Presenter presenter = new Presenter(view, model);     
    presenter.StartApplication();     
}     

That way, you don't need to expose the view to the outside. In addition, the view and the presenter know that this view has been started as a "main form", which might be a useful piece of information.

like image 145
Heinzi Avatar answered Jan 18 '23 14:01

Heinzi