I'm working with the Passive View pattern. The user clicks a new account button. The view delegates responsibility to the presenter using parameterless method calls.
我正在使用被动视图模式。用户单击新帐户按钮。视图使用无参数方法调用将责任委派给演示者。
The problem is there are multiple account types so the user needs to pick which one they want to create. How do I resolve this?
问题是有多种帐户类型,因此用户需要选择他们想要创建的帐户类型。我该如何解决这个问题?
- Create a new form from the view, get the needed information and expose it as a property so the presenter can retrieve it. (This ignores the notion that the view shouldn't have any logic in it)
- Create and use the new form from the presenter. (This ties the presenter directly to a form, ignoring the entire point of MVP)
- Create the new form somewhere else and pass it in as a constructor argument to the presenter... or view.
- Forget it and add a new button for each account type. (There are a number of account types and this will clutter the UI but so be it.)
- I'm going about this the wrong way and need to rethink my design. (If this is the case, an nudge in the the right direction would be appreciated.)
从视图创建新表单,获取所需信息并将其作为属性公开,以便演示者可以检索它。 (这忽略了视图中不应该有任何逻辑的概念)
从演示者创建并使用新表单。 (这会将演示者直接绑定到表单,忽略MVP的整个要点)
在其他地方创建新表单,并将其作为构造函数参数传递给演示者...或视图。
忘记它并为每种帐户类型添加一个新按钮。 (有许多帐户类型,这会使UI混乱,但也就是这样。)
我正在以错误的方式解决这个问题,需要重新考虑我的设计。 (如果是这种情况,那么在正确方向上的推动将会受到赞赏。)
4 个解决方案
#1
I'd probably create another presenter-view pair for getting the account type. Then either
我可能会创建另一个演示者 - 视图对来获取帐户类型。然后
- your presenter calls the other presenter directly to display the new form or
- your presenter asks its model for the right account type. The model knows that it should ask somewhere else and invokes the "account type presenter" or even the "account type model".
您的演示者直接调用另一个演示者以显示新表单或
您的演示者会询问其模型的正确帐户类型。该模型知道它应该询问其他地方并调用“帐户类型演示者”甚至“帐户类型模型”。
I think I'd go with the first option unless your presenter grows unwieldy.
我想我会选择第一个选项,除非你的主持人变得笨拙。
#2
I'm not an MVP expert but I would handle this by using a delegate to get the account type from the view. The presenter invokes the delegate on the view which opens the "select account type" form and returns the selected account type when the user has selected an account type and closed the form.
我不是MVP专家,但我会通过使用委托从视图中获取帐户类型来处理此问题。演示者在视图上调用委托,该委托打开“选择帐户类型”表单,并在用户选择帐户类型并关闭表单时返回所选帐户类型。
#3
If you are talking about a simple interface for selection an account type, IMO it depends on the number of account types. I would just add new buttons for each account. However, if you have a lot of account types, I would have a combobox with the list of all possible accounts and the first (the one the user sees first) be an invalid or unselected type. I would also add some label saying "Select account type to create", then have one single button press that sends the value in the combobox to the model. This way if the user just clicks the button without select an account type, the model will valid the type, and return the problem to the view (and the view can highlight the box or red the text or whatever). That would prevent the user from missing the account type selection. This approach would also make unit testing easier.
如果您正在谈论选择帐户类型的简单界面,IMO则取决于帐户类型的数量。我只想为每个帐户添加新按钮。但是,如果您有很多帐户类型,我会有一个组合框,其中包含所有可能帐户的列表,第一个(用户首先看到的帐户)是无效或未选择的类型。我还会添加一些标签,上面写着“选择要创建的帐户类型”,然后按下一个按钮,将组合框中的值发送给模型。这样,如果用户只是单击按钮而不选择帐户类型,则模型将使类型有效,并将问题返回到视图(并且视图可以突出显示框或红色文本或其他内容)。这样可以防止用户错过帐户类型选择。这种方法也可以使单元测试更容易。
If you are talking about each account type having different information that needs to be filled out, then you would have to have a different view and presenter per each account. (This would be what you need after the user selects the account type)
如果您在谈论每种帐户类型都需要填写不同的信息,那么您必须为每个帐户设置不同的视图和演示者。 (这将是用户选择帐户类型后您需要的)
#4
My solution for this was different than I expected. I changed the button the user clicked to a DropDownMenuButton. Then I passed a string list of account types to the view which populates the drop down menu. I also created an event handler for the drop down menu item click event, which updates a public property with the name of the menu item then delegates everything else to the presenter.
我对此的解决方案与我预期的不同。我将用户点击的按钮更改为DropDownMenuButton。然后我将一个帐户类型的字符串列表传递给填充下拉菜单的视图。我还为下拉菜单项click事件创建了一个事件处理程序,它使用菜单项的名称更新公共属性,然后将其他所有内容委托给演示者。
The presenter just has to get the menu item name from the exposed property and then lookup the account type in a private dictionary of account types using the account type name as the key.
演示者只需从公开的属性中获取菜单项名称,然后使用帐户类型名称作为键在帐户类型的私有字典中查找帐户类型。
#1
I'd probably create another presenter-view pair for getting the account type. Then either
我可能会创建另一个演示者 - 视图对来获取帐户类型。然后
- your presenter calls the other presenter directly to display the new form or
- your presenter asks its model for the right account type. The model knows that it should ask somewhere else and invokes the "account type presenter" or even the "account type model".
您的演示者直接调用另一个演示者以显示新表单或
您的演示者会询问其模型的正确帐户类型。该模型知道它应该询问其他地方并调用“帐户类型演示者”甚至“帐户类型模型”。
I think I'd go with the first option unless your presenter grows unwieldy.
我想我会选择第一个选项,除非你的主持人变得笨拙。
#2
I'm not an MVP expert but I would handle this by using a delegate to get the account type from the view. The presenter invokes the delegate on the view which opens the "select account type" form and returns the selected account type when the user has selected an account type and closed the form.
我不是MVP专家,但我会通过使用委托从视图中获取帐户类型来处理此问题。演示者在视图上调用委托,该委托打开“选择帐户类型”表单,并在用户选择帐户类型并关闭表单时返回所选帐户类型。
#3
If you are talking about a simple interface for selection an account type, IMO it depends on the number of account types. I would just add new buttons for each account. However, if you have a lot of account types, I would have a combobox with the list of all possible accounts and the first (the one the user sees first) be an invalid or unselected type. I would also add some label saying "Select account type to create", then have one single button press that sends the value in the combobox to the model. This way if the user just clicks the button without select an account type, the model will valid the type, and return the problem to the view (and the view can highlight the box or red the text or whatever). That would prevent the user from missing the account type selection. This approach would also make unit testing easier.
如果您正在谈论选择帐户类型的简单界面,IMO则取决于帐户类型的数量。我只想为每个帐户添加新按钮。但是,如果您有很多帐户类型,我会有一个组合框,其中包含所有可能帐户的列表,第一个(用户首先看到的帐户)是无效或未选择的类型。我还会添加一些标签,上面写着“选择要创建的帐户类型”,然后按下一个按钮,将组合框中的值发送给模型。这样,如果用户只是单击按钮而不选择帐户类型,则模型将使类型有效,并将问题返回到视图(并且视图可以突出显示框或红色文本或其他内容)。这样可以防止用户错过帐户类型选择。这种方法也可以使单元测试更容易。
If you are talking about each account type having different information that needs to be filled out, then you would have to have a different view and presenter per each account. (This would be what you need after the user selects the account type)
如果您在谈论每种帐户类型都需要填写不同的信息,那么您必须为每个帐户设置不同的视图和演示者。 (这将是用户选择帐户类型后您需要的)
#4
My solution for this was different than I expected. I changed the button the user clicked to a DropDownMenuButton. Then I passed a string list of account types to the view which populates the drop down menu. I also created an event handler for the drop down menu item click event, which updates a public property with the name of the menu item then delegates everything else to the presenter.
我对此的解决方案与我预期的不同。我将用户点击的按钮更改为DropDownMenuButton。然后我将一个帐户类型的字符串列表传递给填充下拉菜单的视图。我还为下拉菜单项click事件创建了一个事件处理程序,它使用菜单项的名称更新公共属性,然后将其他所有内容委托给演示者。
The presenter just has to get the menu item name from the exposed property and then lookup the account type in a private dictionary of account types using the account type name as the key.
演示者只需从公开的属性中获取菜单项名称,然后使用帐户类型名称作为键在帐户类型的私有字典中查找帐户类型。