C#Winforms MVP被动视图,在哪里处理模型?

C#Winforms MVP被动视图,在哪里处理模型?,c#,winforms,model,mvp,C#,Winforms,Model,Mvp,我不熟悉MVP-PV类型的模式,对如何处理模型有一些疑问。(我在应用程序中使用的是简单的CRUD语句,而不是EF)模型是否包含用于检索数据的CRUD,或者仅包含模型的属性。在哪里实例化模型?在与演示者一起传递的视图中?在演示者的顶端?在每种方法中 这里是一个简单的例子,我正在做什么与MVP UserControl的添加方式如下: ElementView uControl = new ElementView() uControl.Location = new Point(0, 0); uContr

我不熟悉MVP-PV类型的模式,对如何处理模型有一些疑问。(我在应用程序中使用的是简单的CRUD语句,而不是EF)模型是否包含用于检索数据的CRUD,或者仅包含模型的属性。在哪里实例化模型?在与演示者一起传递的视图中?在演示者的顶端?在每种方法中

这里是一个简单的例子,我正在做什么与MVP

UserControl的添加方式如下:

ElementView uControl = new ElementView()
uControl.Location = new Point(0, 0);
uControl.Dock = DockStyle.Fill;
rPanel.Controls.Add(uControl);
界面:

namespace MPVExample.View
{
    public interface IElementView
    {
        int IElementPKey { get; set; }
        string INumber { get; set; }
        string IDescription { get; set; }

        event EventHandler<EventArgs> OnEditElement;
    }
}
模型:

namespace MPVExample.Models
{
    public class ElementModel 
    {
        public int ElementPKey { get; set; }
        public byte Number { get; set; }
        public string Description { get; set; }

        public void Edit() //Does this belong in here?
        {
             //SQL to Edit record
        }

        public void Save() //Does this belong in here?
        {
             //SQL to Save record
        }

        public void Get() //Does this belong in here?
        {
             //SQL to Get record
        }

        public void Delete() //Does this belong in here?
        {
             //SQL to Delete record
        }
    }
}

模型应在演示者中初始化。最好在单个位置初始化它,并在演示者中的任何位置使用它。视图可以调用presenter方法以及所需的参数(主要来自GUI元素)。在presenter方法中,通过传递所有参数,可以使用它并与模型对象交互,执行获取、编辑、保存和删除操作。如果需要将模型中的更改更新到视图中,则需要在模型中具有调用presenter中的方法的方法,这些方法将在视图中调用方法,从而更新UI

对于初学者来说,这可能会让人困惑。简单地说,每个演示者都将有一个对其视图和模型的引用每个视图都将引用它的演示者每个模型将仅参考演示者,而不是视图。通过这种方式,可以实现应用程序各层之间的松散耦合


我已将链接附加到我的github存储库,其中包含一个使用MVP模式的示例应用程序。但我很少使用模型。您可以看到我们可以根据presenter方法调用更改实际UI事件方法的程度。祝你好运,伙计:)

谢谢你的例子。
namespace MPVExample.Presenter
{
    public class ElementPresenter
    {
        private readonly IElementViewView;
        //ElementModel Model = new ElementModel (); //Instantiate Here?

        public ElementPresenter(IElementView view)
        {
            try
            {
                if (view != null)
                {
                    View = view;
                    Initialize();
                }
                else
                {
                    throw new ArgumentNullException("IElementView");
                }
            }
            catch (Exception ex)
            {
                 //Log Error
            }
        }

        private void Initialize()
        {
            try
            {
                View.OnEditElement += Edit_Element;
            }
            catch (Exception ex)
            {
                 //Log Error
            }
        }

        private void Edit_Element(object sender, EventArgs e)
        {
            try
            {
             ElementModel model = new ElementModel(); //Instantiate Here?
             Model.ElementPKey = View.IElementPKey;
             Model.Number = Convert.ToByte(View.INumber);
             Model.Description = View.IDescription;
             Model.Edit();
            }
            catch (Exception ex)
            {
                 //Log Error
            }
        }
    }
}
namespace MPVExample.Models
{
    public class ElementModel 
    {
        public int ElementPKey { get; set; }
        public byte Number { get; set; }
        public string Description { get; set; }

        public void Edit() //Does this belong in here?
        {
             //SQL to Edit record
        }

        public void Save() //Does this belong in here?
        {
             //SQL to Save record
        }

        public void Get() //Does this belong in here?
        {
             //SQL to Get record
        }

        public void Delete() //Does this belong in here?
        {
             //SQL to Delete record
        }
    }
}