- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我研究 MVP 模式已有一段时间,并设法创建了一些简单的 MVP 兼容应用程序。
我现在正尝试将该模式应用到一个更复杂的应用程序中,但我对这样做的最佳方式存疑。
我的应用程序只有一个 WinForm,带有两个用于加载两种不同类型数据的按钮。我的 View 界面如下所示:
interface IView_MainForm
{
// Load input
//
event EventHandler<InputLoadEventArgs> LoadInput_01;
event EventHandler<InputLoadEventArgs> LoadInput_02;
bool Input01_Loaded { get; set; }
bool Input02_Loaded { get; set; }
}
IView 是通过构造函数注入(inject)在我的演示者中引用的:
public Presenter_MainForm(IView_MainForm view)
{
this.View = view;
this.View.LoadInput_01 += new EventHandler<InputLoadEventArgs>(OnLoadInput_01);
this.View.LoadInput_02 += new EventHandler<InputLoadEventArgs>(OnLoadInput_02);
}
到目前为止,还不错。当用户单击两个按钮中的任何一个以加载数据时,将引发 LoadInput_## 事件,Presenter 正在处理它,检查输入是否有错误并根据我的数据模型构建它。
我的下一步是在 View 中显示处理后的数据。
我努力让我的 View 尽可能保持被动和“愚蠢”,假设它对 Presenter 一无所知(它不订阅它的事件,Presenter 通过调用 IView 方法将数据发送到 View) ,更不用说模型了。
如果 View 不知道数据模型是什么样子,我应该如何填充像 TreeView 这样的控件?
此外,我是否正确处理了整个 MVP 问题,还是我遗漏了什么?
最佳答案
在您的View
中使用复杂类型
属性没有任何问题。假设您有一些 ComplexType
。
class ComplexType
{
public string ParentNode {get;set;}
public List<string> ChildNodes {get;set;}
// some other properties
}
我们还假设 ComplexType
是您的 TreeView
的数据模型。使用 MVP
模式在您的 View
上拥有将具有 ComplexType
的属性是完全没问题的。所以拥有这样的东西非常好
interface IView_MainForm
{
// Load input
//
event EventHandler<InputLoadEventArgs> LoadInput_01;
event EventHandler<InputLoadEventArgs> LoadInput_02;
bool Input01_Loaded { get; set; }
bool Input02_Loaded { get; set; }
ComplexType Input01Data {get;set;} // you might actually have some code in get/set setters
ComplexType Input02Data {get;set;} // you might actually have some code in get/set setters
public void SetInput01Data(ComplexType input01Data)
{
Input01Data = input01Data;
// some other stuff
}
}
并且由于您的 Model
适用于具有 2 个输入的 View
,因此您的 Model 可能看起来像这样
public interface IModel
{
public ComplexType Input01Data {get;set;}
public ComplexType Input02Data {get;set;}
}
现在在您的 Presenter
中,您只需处理从 View
触发的事件,填充 Model
并在 View
上设置属性>
class Presenter
{
private IModel _myModel...
private IRepository _repository;
public Presenter(IView_MainForm view, IRepository repository)
{
_repository = repository;
this.View = view;
this.View.LoadInput_01 += new EventHandler<InputLoadEventArgs>(OnLoadInput_01);
this.View.LoadInput_02 += new EventHandler<InputLoadEventArgs>(OnLoadInput_02);
}
public void OnLoadInput_01(object sender, InputLoadEventArgs e)
{
// get data based on passed arguments (e.SomeProperty)
// construct IModel
myModel = _repository.GetData(e.SomeProperty);
// pass data to IView_MainForm
View.SetInput01Data(myModel.Input01Data);
}
}
关于您的问题
I'm striving to keep my View as passive and "dumb" as possible, assuming it knows nothing of the Presenter (it doesn't subscribe to its events, the Presenter sends data to the View by calling IView methods instead), let alone of the Model.
您的 View
仍然对 Presenter
和 Model
一无所知。它只是触发事件,从 Presenter
获取数据并绑定(bind)其控件。并且您具有可测试性
(请注意此单元测试是伪代码,因为我不知道您如何检索数据、您在按钮单击事件中需要什么输入等...)。
[Test]
public void ShouldLoadInput01DataOnButtonClick()
{
// Arrange
IModel data = // create dummy data
Mock<IView_MainForm> clientsViewMock = new Mock<IView_MainForm>();
Mock<IRepository> clientsRepositoryMock = new Mock<IRepository>();
clientsRepositoryMock.Setup(repository => repository.GetData(something)).Returns(data.Input01Data);
var presenter = new Presenter(clientsViewMock.Object, clientsRepositoryMock .Object);
// Act
clientsViewMock.Raise(view => view.LoadInput01 += null, new InputLoadEventArgs());
// Assert
clientsViewMock.Verify(view => view.SetInput01Data(data.Input01Data), "Input01 data expected be set on button click.");
}
关于c# - 如何在 MVP 被动 View 中显示复杂数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25770413/
我相信 Presenter 是负责处理 MVP 应用程序导航方面的人。这是真的还是有异常(exception)? 最佳答案 我认为 View 是唯一负责屏幕导航的 View 。 Here我们对这个“A
我看到了两种实现“加载”反馈的基本方法(例如旋转加载图标): 在演示者中: void displayData() { display.startShowingLoadingIcon();
我有一个方法,用户可以搜索文章编号,如果它在数据库中可用,则文章编号绑定(bind)到 BindingList。现在我想让用户知道该文章是否在数据库中不可用。我该如何以正确的方式做到这一点? 只需将消
我们正在使用 gwt-presenter,但并不是一个特定于此的问题...... 我有一张包含用户的表格。当我在 View 中构建表格时(根据演示者提供的数据),我需要在行的末尾添加两个操作按钮(“编
请用一个简单的例子来描述MVP-Passive View和MVP-Supervising controller的区别。最好显示如何使用 mvp 技术(被动 View 和监督 Controller )绑
GWT 2.1 包含一个 MVP framework其中包括 Google 的 Ray Ryan 在 Google I/O 2009 上概述的 PlaceService 实现。 那个talk启发了一些
在Model-View-Presenter模式中,我们应该在哪里编写用户输入的验证。 最佳答案 Realm 特定的规则/验证应在模型中。您可以使用model.validate()来告知您是否违反规则。
当我们在面试的时候被问到谈谈你对MVC,MVP,MVVM的理解。我们该如何回答,下面我们将对这些模式进行总结。 一、web1.0时代 在web1.0时代,当时并没有前端这个概念,当时如果要开发一个项目
我正在寻找有关 GWT 架构的指南 - 何时使用自包含小部件与 MVP/事件/场所。 背景 阅读了 Google 文档并搜索了 Stackoverflow,gwt-examples 项目为这个问题提供
在地点更改时更新事件状态的最佳做法是什么?假设您有一个带有 View 的事件,该 View 显示类别列表和类别中的项目列表。如果选择了不同的类别,则应用程序会使用类别 ID 转到新位置。然后我只想刷新
在 GWT 中使用 MVP 时,您将如何使用表?例如,如果您有一个用户表,您的 View 看起来像这样吗? public interface MyDisplay{ HasValue users();
我有一个 GWT 应用程序,我正在将 GWT MVP 与地点/事件一起使用。 我的应用程序布局类似于 菜单 |内容 菜单和内容显示将动态变化,其中一个单独变化。我的意思是,当内容显示更改时,我不想更新
我正在使用 Windows 窗体实现 MVP 模式,我对当前的实现有疑问,因为我正试图将其融入更复杂的架构中。 现在我有一个带有属性的完全不可知的 View ,一个在构造函数中注入(inject) V
对所有 GWT 大师的问题。 我是 GWT 的新手,正在尝试了解编写 GWT 应用程序的最佳实践。我经历过"Large scale application development and MVP"基于
我最近一直试图从架构的角度来理解 MVP 架构中交互器和用例之间的关系。我的疑问是关于部件之间的通信以及符合 MVP 标准的内容。 问题是我见过很多相互矛盾的流程图。其中一些似乎每个演示者都有一个交互
我有一个具有 MVP 架构模式的应用程序。 现在,我正在尝试在我的应用程序中实现 Livedata。 我搜索了很多,但我找不到任何教程或示例。 所有教程都说实时数据适用于 MVVM 模式。 在 MVP
我有一个关于在使用 MVP 时如何处理演示者之间的通信的问题。假设我有两个 MVP-triads。一个是产品列表(Triad A),另一个是有关当前所选产品的一般信息(Triad B)。 我如何告诉
我主要具有 ASP.Net 背景,并具有一些 MVC 知识。我也做了一些 Silverlight 和 MVVM,但是我现在即将转向 Winforms,我对它的经验很少,所以我想知道如何处理 MVP。
我读了鲍勃叔叔的《整洁架构书》。我尝试实现简单的例子。我有这个图: 我不明白如何实现红线部分。例如,我有一个简单的网页: 我有 2 个按钮、一个已编辑的文本和一个标签。如果我按下发送到服务器按钮 -
我正在查看 MVP 的 google 示例,我看到 this作为 Activity 的 onCreate 中的最后一个语句: new TaskDetailPresenter(
我是一名优秀的程序员,十分优秀!