views:

261

answers:

4

I need your help to understand MVP. I used a interface (IProductEditorView). If you look below you can see presentation layer:

using System;
using System.Collections.Generic;
using System.Text;
using MVPProject.BusinessLayer;

namespace MVPProject.PresentationLayer
{
    public interface IProductEditorView
    {
        int ProductID { get;}
        string ProductDescription { get; }

        event EventHandler<EventArgs> Save;
    }

    public class ProductEditorPresenter
    {
        private IProductEditorView mView;

        public ProductEditorPresenter(IProductEditorView view)
        {
            this.mView = view;
            this.Initialize();
        }

        private void Initialize()
        {
            this.mView.Save += new EventHandler<EventArgs>(mView_Save);
        }

        private void mView_Save(object sender, EventArgs e)
        {
            Product product;
            try
            {
                product = new Product();
                product.Description = mView.ProductDescription;
                product.ID = mView.ProductID;
                product.Save();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                throw;
            }
        }
    }

}

If you look below you can see; this is mixing my head because ProductListPresenter(IProductEditorView view) but they want me add (this). I don't understand why "this.mPresenter = new ProductListPresenter(this);" ?

    private ProductListPresenter mPresenter;
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        this.mPresenter = new ProductListPresenter(this);
    }
A: 

Assuming the the second block of code is part of an asp.net form code behind, then I can imagine that the asp.net form implements the IProductEditorView interface and thus you are passing the view (this) to the presenter.

Pedro Santos
A: 

Assuming that the second part of the code is from the view, there is a problem with your implementation of MVP pattern.

In your design, both presenter and view knows about each other (Presenter accepts the view in its constructor and view sets its presenter on OnInit).

This is a problem because you use MVP for decoupling view and presenter but this design makes them tightly coupled. Your presenter does not need to know about your view, so you can remove IProductEditorView parameter from the presenters constructor.

Then, you need to change your save method to this:

private void Save(Product product)
{
    try
    {
        product.Save();
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine(ex.Message);
        throw;
    }
}

And in your view, when you need to save the product, prepare it and pass to the presenter:

private btnSaveProduct_Click(object sender, EventArgs e)
{
    Product product;
    product.Description = txtDescription.Text;
    // Set other properties or the product
    this.mPresenter.Save(product);
}

OnInit of your view stays the same.

Serhat Özgel
Thanks you are ok!Teşekkürler dostum. Benim meesengerım: [email protected] eklersen tanışırız...
Phsika
+1  A: 

The view, "this", in the context of an ASP.NET page is the "Page". When I was getting to grips with MVP I found the Polymorphic Podcast series on MV Patterns really useful.

Junto
A: 

The way I have done MVP is to have the presenter have a single method called Initialize which takes the view as an argument. One way to think of MVP is that the presenter is manipulating the view through the views interface. Lets take the example that when the OnClick event of the save button is clicked that you want the presneter to save the product and not have this code in the code behind of the page directly. You could code something like this:

public class ProductEditorPresenter
{
    public void Initialize(IProductEditorView view, IProductBuilder productBuilder)
    {
       view.SaveProduct += delegate
                           {
                              var product = productBuilder.Create(view.ProductId, view.ProductDescription);
                              product.Save();
                           }
    }
}

where the productBuilder handles the creation of the product for you. You really do want to attempt to do interface/contract based programming and not create concrete objects directly in your classes. Also Jeremy Miller has created a wiki on presentation patterns that may be useful in describing this pattern even in more detail. This can be seen here

Michael Mann