I'm writing an application using the MVP pattern, and am curious if there is a "correct" way to pass user input from the View to the Presenter.
我正在使用MVP模式编写应用程序,如果有一种“正确”的方法将用户输入从View传递给Presenter,我很好奇。
For example, I have a simple search form with a text box and a "Search" button. In the Presenter, I have a function that performs the search and populates the View with the result. In the Click event for the search button, I call the search function in the Presenter.
例如,我有一个带有文本框和“搜索”按钮的简单搜索表单。在Presenter中,我有一个执行搜索的函数,并使用结果填充View。在搜索按钮的Click事件中,我在Presenter中调用搜索功能。
My question, is it preferred to pass the user input as a parameter to the search function in the Presenter, or is it better to create a Get accessor in the View for the Presenter to retrieve the user input?
我的问题是,首选将用户输入作为参数传递给Presenter中的搜索功能,还是最好在View for the Presenter中创建Get访问器以检索用户输入?
Example -
In the view:
在视图中:
private void btnSearch_Click(object sender, System.EventArgs e)
{
presenter.Search(txtUserInput.Text);
}
In the Presenter:
在演示者中:
public void Search(string userInput)
{
//perform search
}
OR
In the view:
在视图中:
public string UserInput
{
get { return txtUserInput.Text; }
}
private void btnSearch_Click(object sender, System.EventArgs e)
{
presenter.Search();
}
In the Presenter:
在演示者中:
public void Search()
{
string userInput = view.UserInput;
//perform search
}
1 个解决方案
#1
I usually implement it using your second approach.
我通常使用你的第二种方法实现它。
Presenter
methods that usually don't receive any arguments and collect data from the IView
.
Presenter方法通常不接收任何参数并从IView收集数据。
As I see it, the view exposes the state and the behavior is implemented in a "stateless" Presenter (the only state is a reference to the view and injected dependencies).
在我看来,视图公开了状态,行为在“无状态”Presenter中实现(唯一的状态是对视图和注入依赖项的引用)。
This provides a very easy way to unit test the presenter against a Mock view, as well as a very simple guideline when creating the IView
and the Presenter
:
这提供了一种非常简单的方法,可以根据Mock视图对演示者进行单元测试,以及创建IView和Presenter时非常简单的指导:
- All data that is exposed through controls in the UI should be properties (usually as ValueTypes to make it Winforms agnostic) in the
IView
interface. - All behavior, such as the logic for handling each button, and the initialization should be
Presenter
methods.
通过UI中的控件公开的所有数据都应该是IView接口中的属性(通常为ValueTypes,以使其与Winforms无关)。
所有行为,例如处理每个按钮的逻辑和初始化应该是Presenter方法。
#1
I usually implement it using your second approach.
我通常使用你的第二种方法实现它。
Presenter
methods that usually don't receive any arguments and collect data from the IView
.
Presenter方法通常不接收任何参数并从IView收集数据。
As I see it, the view exposes the state and the behavior is implemented in a "stateless" Presenter (the only state is a reference to the view and injected dependencies).
在我看来,视图公开了状态,行为在“无状态”Presenter中实现(唯一的状态是对视图和注入依赖项的引用)。
This provides a very easy way to unit test the presenter against a Mock view, as well as a very simple guideline when creating the IView
and the Presenter
:
这提供了一种非常简单的方法,可以根据Mock视图对演示者进行单元测试,以及创建IView和Presenter时非常简单的指导:
- All data that is exposed through controls in the UI should be properties (usually as ValueTypes to make it Winforms agnostic) in the
IView
interface. - All behavior, such as the logic for handling each button, and the initialization should be
Presenter
methods.
通过UI中的控件公开的所有数据都应该是IView接口中的属性(通常为ValueTypes,以使其与Winforms无关)。
所有行为,例如处理每个按钮的逻辑和初始化应该是Presenter方法。