tags:

views:

389

answers:

2

I totally understand the MVP pattern now, but I still struggle to see where views and presenters are instantiated. I have seen some examples where the presenter is newed up in the view, but is this correct. After reading a blog post of Jeremy Miller about communicating between View and Presenter he had a function on the Presenter to attach the presenter to the view.

My question is then this: Where should views and presenters be created? Also where in winforms and webforms.

+2  A: 

In webforms, the page gets instantiated by the request. Since the page is the view and you cannot control the order of execution, it is the view that has to register itself with the presenter

Matt Briggs
what about winforms?
adriaanp
+2  A: 

In Winforms, I instantiate the view as required (eg: in the main method, or in a method on another presenter, but wherever makes sense really). The view then creates and registers itself with a new instance of a presenter.

This makes having several views use the same presenter logic easy, and shields users of my view from my particular architectural decision to use MVP.

Dan Vinton
So, you mean your presenter will know about concrete view?
Samnang
No. The presenter has a reference to the view only through an interface that the view implements. This decouples the presenter from the view implementation, and makes unit testing presenter logic very easy since you can use a mock view for testing.
Dan Vinton
If you instantiate the view as required in a method on another presenter, then surely that presenter must know about the concrete view?
Kevin Thiart
@kevint - there's ways around that too; either have the presenter ask it's own view to create the new sub-view, or having some sort of view lookup, so the implementation returned isn't explicit. Admittedly the the answer doesn't make that too clear...
Dan Vinton
Ok, I see what you mean. I guess something similar to the IMessageBoxCreator in [this post](http://jeremydmiller.blogspot.com/2005/06/simple-example-of-humble-dialog-box.html) could work, only it creates screens with presenters rather than just simple dialog boxes. So you implement the IDialogCreator (which returns an IDialogView) in your presentation layer and your controllers/presenters are still blissfully unaware of the details. Thanks for the response!
Kevin Thiart