views:

1389

answers:

2

I’ve started using the MVP pattern in a Windows forms app for a couple of my forms. These forms create a presenter and pass themselves back to the presenter.

I’m not using any particular framework and forms are still able to open other forms.

Should I change this setup so that presenters can open other forms (via views)? I believe would have to use an IOC framework to do this?

This might save me putting logic to open related forms in the UI and allow me to remove references to the presenter from within the form.

A: 

I'm doing a similar thing with a couple of winforms projects, one for work and one for home.

My solution for both is to use a factory for views that will allow the caller to access and pass messages to the new presenter. In some cases I'm abstracting them further by placing a service interface either on the presenter itself or on an intermediary class that can be injected.

Andrew Kennan
+2  A: 

You should have a separate class that is responsible for creating / managing forms. I would recommend using the Command pattern to invoke the code that opens a new form. It will end up scaling a lot better and you can change the Command implementation to do different things. In my projects, I just do the data changes in the form itself, but anything that interacts with the "application" is delegated to a Command object.

You don't NEED an IOC framework to do this, but it helps. If you just have simple needs, you can keep it simple.

I have a WinForms MVP example on my site here: ModelViewPresenter. I don't use any IOC containers, but structure the code such that using one would be a quick hop.

Garo Yeriazarian
Thanks for the example code, Garo, but I don't think it is a very simple or clear example of MVP. Instead, it looks a bit more like MVC - you even call your type `ApplicationController` rather than `ApplicationPresenter`.
Pat
In my example, the ApplicationController is a Controller, not a Presenter. The Presenters inherit from AbstractPresenter and are in the Modules folder. The Presenters bind data from the model to the view and the Controller will pull the application together and provide some environmental functionality for doing things like loading presenters and hooking up things (these things may normally be handled by a container).
Garo Yeriazarian