views:

49

answers:

2

I try to apply MVP pattern for win.forms application. I have 2 forms: main & child. Main has a button and when you click it - child form should appear.

There are 2 views interfaces that forms implement

IMainView  {
  event OnClick;
  ...
}

IChildView { 
  ...
}

There are two presenters

MainPresenter(IMainView) & ChildPresenter(IChildView)

MainPresenter listens to OnClick event and then should create IChildView implementation.

MainPresenter {
  ...
  MainClicked() {
    // it's required to create IChildView instance here
  }
}

How would you implement such creation typically?

Shall IMainView has factory method for IChildView or may be it should be separate Views factory. What would you advise? Or maybe there is some misunderstanding of MVP here?

Thank you in advance!

+1  A: 

You can check my sample (isn't THE solution, but A solution) in my blog www.danieleteti.it /?p=221

the sample is in Delphi language

Daniele Teti
Thanks, it was useful. But it's not clear who creates views on request?
Andrew Florko
In the sample in the blog, the presenter create the view. But in a real-world application you can have a factory, a builder, a factory method, a DI container and so on.
Daniele Teti
I lurked Internet and it looks like that factory (often called service in examples) is usual practice. Especially if DI is used. Is it true? (otherwise we have manual dependency injection)
Andrew Florko
Yes, factory is often used for create views. DI is a must when you need to 'link' all the MVP (passive view or sup. Controller) parts.
Daniele Teti
A: 

Yes, factory is often used for create views. DI is a must when you need to 'link' all the MVP (passive view or sup. Controller) parts.

Daniele Teti