简体   繁体   English

模型视图演示者界面有疑问吗?

[英]Model View Presenter Interface question?

I need your help to understand MVP. 我需要您的帮助才能了解MVP。 I used a interface ( IProductEditorView ). 我使用了一个界面( 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) . 这让我很ProductListPresenter(IProductEditorView view)因为ProductListPresenter(IProductEditorView view)但他们要我添加(this) I don't understand why " this.mPresenter = new ProductListPresenter(this); " ? 我不明白为什么this.mPresenter = new ProductListPresenter(this); ”?

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

The view, "this", in the context of an ASP.NET page is the "Page". 在ASP.NET页面的上下文中,视图“ this”是“页面”。 When I was getting to grips with MVP I found the Polymorphic Podcast series on MV Patterns really useful. 当我开始接触MVP时,我发现MV模式上多态播客系列确实很有用。

The way I have done MVP is to have the presenter have a single method called Initialize which takes the view as an argument. 我做MVP的方法是让演示者拥有一个名为Initialize的方法,该方法将视图作为参数。 One way to think of MVP is that the presenter is manipulating the view through the views interface. 想到MVP的一种方法是演示者正在通过视图界面操纵视图。 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. 让我们举个例子,当单击“保存”按钮的OnClick事件时,您希望主持人保存产品并且该代码不直接放在页面后面的代码中。 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. productBuilder在其中为您处理产品的创建。 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. 另外,杰里米·米勒(Jeremy Miller)还创建了一个有关演示模式的Wiki,这可能对甚至更详细地描述这种模式很有用。 This can be seen here 可以在这里看到

假定第二个代码块是asp.net表单代码的一部分,那么我可以想象asp.net表单实现了IProductEditorView接口,因此您将视图(this)传递给了主持人。

Assuming that the second part of the code is from the view, there is a problem with your implementation of MVP pattern. 假设代码的第二部分是从视图来看的,则您的MVP模式实现存在问题。

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). 在您的设计中,演示者和视图都彼此了解(Presenter在其构造函数中接受视图,并且视图在OnInit上设置其演示者)。

This is a problem because you use MVP for decoupling view and presenter but this design makes them tightly coupled. 这是一个问题,因为您使用MVP解耦视图和演示者,但是这种设计使它们紧密耦合。 Your presenter does not need to know about your view, so you can remove IProductEditorView parameter from the presenters constructor. 演示者不需要了解您的视图,因此可以从演示者构造函数中删除IProductEditorView参数。

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. 您的视图的OnInit保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM