MVP:应该了解模型的是View还是Presenter? [英] MVP: Is it the View or the Presenter that should know of the Model?
问题描述
模式的相对较新,让我直接在WinForms上下文中显示一个示例.
我有一个基本的MVP Passive View结构,我应该继续该结构:
公共局部类UserView:Form,IUserView{公共事件EventHandler保存;公共UserView(){InitializeComponent();new UserPresenter(new UserModel(),this);}}公共类UserPresenter{public UserPresenter(IUser模型,IUserView视图){保存+ =(发送方,e)=>model.Save();}}
或
公共局部类UserView:Form,IUserView{公共事件EventHandler保存;公共UserView(){InitializeComponent();新的UserPresenter(this);}}公共类UserPresenter{public UserPresenter(IUserView视图){var model = new UserModel();//假设我有将属性值从View绑定到Model的逻辑保存+ =(发送方,e)=>model.Save();}}
我的问题是:
1)谁应该知道模型 User
,View或Presenter的具体实例?
2)在这种情况下有什么好处?
3)假设我的模型从不依赖于View.在这种情况下,如果View知道模型怎么办?制作完所有 UserView
后,是不是 UserModel
吗?
4)如果Presenter仅应与Model和View的接口交互,则在 Save
事件处理程序中调用 model.Save
,在哪里可以获取型号
来自?
在此处有两个重复的问题,并且此处,但是我猜他们并不能完全解决我的情况.
如果视图知道模型怎么办?制作完所有UserView之后专门针对UserModel吗?
什么都没有.MVP模式的 Supervising Controller
变体已接受这种做法.该视图直接与模型交互以实现简单的操作,而更复杂的操作则通过演示者进行编组.在 Passive View
中,所有内容都通过演示者进行.
另外,请参阅Jeremy Miller的 构建您自己的CAB系列 ,以更好地了解这两种方法之间的区别:被动视图.>
Relatively new to patterns, let me straight away show an example in the context of WinForms.
I have a basic MVP Passive View structure, which one should I go ahead with:
public partial class UserView : Form, IUserView
{
public event EventHandler Save;
public UserView()
{
InitializeComponent();
new UserPresenter(new UserModel(), this);
}
}
public class UserPresenter
{
public UserPresenter(IUser model, IUserView view)
{
view.Save += (sender, e) => model.Save();
}
}
or
public partial class UserView : Form, IUserView
{
public event EventHandler Save;
public UserView()
{
InitializeComponent();
new UserPresenter(this);
}
}
public class UserPresenter
{
public UserPresenter(IUserView view)
{
var model = new UserModel();
//assuming I have the logic to bind property values from View to Model
view.Save += (sender, e) => model.Save();
}
}
My questions are:
1) Who should know of the concrete instance of model User
, View or Presenter?
2) What will be the benefit in that case?
3) Suppose my Model is never dependent on the View. In that case what's wrong if View knows Model? After all UserView
is made to present UserModel
isn't it?
4) If Presenter should interact with only interfaces of Model and View, then to call model.Save
in Save
eventhandler, where do I get the concrete instance of Model
from?
There are two duplicate questions here and here, but they aren't exactly dealing with my scenario I guess..
What's wrong if view knows model? After all UserView is made specifically for UserModel isnt it?
Nothing. It's accepted practice in the Supervising Controller
variant of the MVP pattern. The view interacts directly with the model for simple operations while more complex operations are marshalled throught the presenter. While in Passive View
, everything goes through the presenter.
Additionally, see Jeremy Miller's Build your own CAB series to get a better idea on the differences between the two approaches: Supervising Controller and Passive View.
这篇关于MVP:应该了解模型的是View还是Presenter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!