C# 模型视图演示者界面问题?

C# 模型视图演示者界面问题?,c#,.net,asp.net,asp.net-mvc,C#,.net,Asp.net,Asp.net Mvc,我需要你的帮助来理解MVP。我使用了一个接口(IProductEditorView)。如果您看下面,您可以看到表示层: using System; using System.Collections.Generic; using System.Text; using MVPProject.BusinessLayer; namespace MVPProject.PresentationLayer { public interface IProductEditorView {

我需要你的帮助来理解MVP。我使用了一个接口(
IProductEditorView
)。如果您看下面,您可以看到表示层:

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;
            }
        }
    }

}

假设第二块代码是asp.net表单代码的一部分,那么我可以想象asp.net表单实现了IPProductEditorView接口,因此您将视图(此)传递给演示者。

假设第二部分代码来自视图,MVP模式的实现存在问题

在您的设计中,presenter和view都相互了解(presenter在其构造函数中接受视图,而view在OnInit上设置其presenter)

这是一个问题,因为您使用MVP来解耦视图和演示者,但这种设计使它们紧密耦合。演示者不需要了解视图,因此可以从演示者构造函数中删除IPProductEditorView参数

然后,您需要将保存方法更改为:

private void Save(Product product)
{
    try
    {
        product.Save();
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine(ex.Message);
        throw;
    }
}
在您看来,当您需要保存产品时,请准备产品并将其传递给演示者:

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

OnInit视图保持不变。

在ASP.NET页面的上下文中,“this”视图就是“page”。当我开始学习MVP时,我发现它非常有用。

我做MVP的方法是让演示者有一个名为Initialize的方法,该方法将视图作为参数。考虑MVP的一种方式是演示者通过视图界面操纵视图。让我们举一个例子,当单击save按钮的OnClick事件时,您希望presneter保存产品,而不是将此代码直接放在页面的代码后面。您可以编写如下代码:

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

其中productBuilder为您处理产品的创建。您确实希望尝试进行基于接口/契约的编程,而不是直接在类中创建具体对象。Jeremy Miller还创建了一个关于表示模式的wiki,它可能有助于更详细地描述这种模式。可以看到

可能会删除asp.net-mvc标记。谢谢您,一切正常!泰克库勒·多斯塔姆。Benim meesengerım:yusufkaratoprak@yahoo.com埃克勒森·坦鲁兹。。。
public class ProductEditorPresenter
{
    public void Initialize(IProductEditorView view, IProductBuilder productBuilder)
    {
       view.SaveProduct += delegate
                           {
                              var product = productBuilder.Create(view.ProductId, view.ProductDescription);
                              product.Save();
                           }
    }
}