views:

926

answers:

1

I'm curious about the situation where you have a user control that you want to reuse throughout an application, but you also have a page or other control that also needs a presenter.

So say I have an upload view and control

public partial class UploadControlView : System.Web.UI.UserControl, IUploadView

but I also have a page view

public partial class ExcelProcessorView : System.Web.UI.Page, IExcelProcessorView

The upload control would be on the ExcelProcessor page, so that the user can upload a file which is handed off to some excel processing business logic.

So I have a presenter,

public class ExcelUploadAndProcessPresenter : IUploadPresenter

In the ExcelProcessorView I would wire up the presenter as follows:

protected void Page_Load(object sender, EventArgs e) 
    {
        ExcelUploadAndProcessorPresenter presenter = 
            new ExcelUploadAndProcessorPresenter(this, uploadControl);
        this.AttachPresenter(presenter);
        uploadControl.AttachPresenter(presenter);
        //init etc...
    }

Is this a correct use of the MVP pattern?

I'm trying to figure out what is the best practice for handling a many views to one presenter relationship. Can MVP have a many to one relationship in this fashion. As an alternate question I would also like to know if the MVP pattern can/should handle a situation where you have many presenters and many views (or should you only have one presenter) ?

If you see any problems with my interpretation of MVP in general, by all means go ahead and point that out.

Thanks for reading!

+2  A: 

Can you explain further how your view and control use the same logic? I can't imagine a scenario where I would have this issue. If the logic is separate, I would think you would use two presenters.

However, there's no reason one presenter cannot handle multiple views/controls, though I would only reuse a presenter for multiple views that display the same model (e.g. CRUD screens). If one view hosts another from the same presenter, I can't think of a reason why you shouldn't reuse your presenter (e.g. Create screen at the bottom of a List screen).

Ryan Riley
I don't follow. Would you mind posting an edit with some sample code?
Ryan Riley