MVP查看知道型号 [英] Mvp View knows Model
问题描述
我试图用MVP和我注意到,我的观点必须知道模型,不应该发生在我的MVP presume。
I'm trying to use MVP and I notice that my View must know Model that should not happen in MVP I presume.
下面是例子:
public partial class TestForm : Form, ITestView
{
public void LoadList(IEnumerable<AppSignature> data)
{
testPresenterBindingSource.DataSource = data;
}
}
public interface ITestView
{
event EventHandler<EventArgs> Load;
void LoadList(IEnumerable<AppSignature> data);
}
public class TestPresenter
{
private ITestView view;
public TestPresenter(ITestView view)
{
this.view = view;
view.Load += View_Load;
}
private void View_Load(object sender, EventArgs e)
{
var data = // get from model
view.LoadList(data);
}
}
和的问题是,在TESTFORM我需要参照AppSignature。
在我看到的教程,有喜欢一些简单的例子
公共无效LoadList(IEnumerable的&LT;字符串&GT;数据)
那里是没有必要的参考模型。但是,如何即DataGridView的可以发布当前行的数据?
and the problem is that in TestForm I need reference to AppSignature.
In all tutorials I saw, there are some simple examples like
public void LoadList(IEnumerable<String> data)
where there is no need reference to model. But how i.e DataGridView can publish current row data?
推荐答案
您的形式是一个视图,它是不是一个presenter。因此,它应该实现接口 ITestView
:
Your form is a View, it is not a Presenter. Thus it should implement interface ITestView
:
public interface ITestView
{
event EventHandler Load;
void LoadList(IEnumerable<AppSignatureDto> data);
}
和你的presenter是一个人,谁订阅查看的事件,并使用视图属性读取和更新视图:
And your Presenter is someone, who subscribes to view's events and uses view properties to read and update view:
public class TestPresenter
{
private ITestView view;
public TestPresenter(ITestView view)
{
this.view = view;
view.Load += View_Load;
}
private void View_Load(object sender, EventArgs e)
{
List<AppSignature> signatures = // get from model
List<AppSignatureDto> signatureDtos = // map domain class to dto
view.LoadList(signatureDtos);
}
}
和你形成,正如我已经说过,是一个视图,它不知道presenter和型号什么:
And you form, as I already said, is a view, it does not know anything about presenter and model:
public partial class TestForm : Form, ITestView
{
public event EventHandler Load;
private void ButtonLoad_Click(object sender, EventArgs e)
{
if (Load != null)
Load(this, EventArgs.Empty);
}
public void LoadList(IEnumerable<AppSignatureDto> data)
{
// populate grid view here
}
}
如何处理参照域类?通常我提供,仅查看简单的数据(字符串,整数,日期等),或创建数据传输对象,它传递给视图(您可以命名他们FooView,FooDto等)。您可以轻松地将它们映射的东西,如 AtoMapper :
List<AppSignatureDto> signatureDtos =
Mapper.Map<List<AppSignature>, List<AppSignatureDto>>(signatures);
这篇关于MVP查看知道型号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!