Event Bubbling and MVP: ASP.NET

百般思念 提交于 2019-11-26 16:41:43

TLDR the code.

Here's how I would do it. You say there are 2 controls on the same page. So that can be served by a ContainerVM with references (members) of TimeVM and MonthVM.

  1. TimeVM updates a backing property ResultantDate whenever you do your thing.
  2. ContainerVM has subscribed to property-changed notifications for TimeVM.ResultantDate. Whenever it receives a change notification, it calls MonthVM.SetMonth()

This can now be tested without using any views - purely at the presenter level.

Thanks for the inputs. I referred MVP Quickstarts http://msdn.microsoft.com/en-us/library/ff650240.aspx. Model can raise events. I think, I should go with that approach. Any thoughts are welcome.

Also, I have posted http://forums.asp.net/t/1760921.aspx/1?Model+View+Presenter+Guidelines to collect general rules on MVP.

Quote

Develop Presenter which can communicate with both View and Model. Presenter may only have knowledge of view interfaces. Even if the concrete view changes, it does not affect presenter.

In the concrete view, control’s event handlers will simply call presenter methods or raise events to which presenter would have subscribed. There should be no presentation rule/logic written in concrete view.

Presenter should have only interface object of model; not concrete model. This is for the ease of Unit Testing

View can refer business entities. However there should no logic written associated with the entity objects. It may just pass the entity object to presenter.

View interface should be an abstraction. It should NOT have any control or System.Web reference. In concrete view, there should be no method other than interface defined methods.

The "Model" never knows about the concrete view as well as the interface view

"Model" can define and raise events. Presenter can subscribe these events raised by model.

Public methods in presenter should be parameterless. View object should access only parameterless methods of presenter. Another option is view can define events to which the presenter can subscribe. Either way, there should be no parameter passing.

Since the model has all the required values (to be stored back in database), there is no need to pass any value to model from view (most of the time). E.g. when an item is selected in dropdown list only the controls’ current index need to be passed to model. Then model knows how to get the corresponding domain values. In this case, the view need not pass anything to presenter. Presenter knows how to get value from view.

View may make use of model directly (without using presenter). E.g. ObjectDataSource's SelectMethod. But controller never knows about the concrete view as well as the interface view.

The presenter references the view interface instead of the view's concrete implementation. This allows you to replace the actual view with a mock view when running unit tests.

I am not experienced with ASP.net but I think I follow the gist of what you are trying to do.

It appears that you are going down to fine a level with your presenter by making presenters for the individual UI elements. In this case the Month and the Time. I would think of it more as ShowTime period. ShowTime has the capability of showing the Month and Time.

To use this with MVP. Then you will need a IShowTimeView that the page will implement. (Not the controls). And then write a ShowTimePresenter that uses IShowTimeView to send and retrieve values.

You will have ShowTime implement the IShowTimeView interface. It will route items like the Time, AddDay events, and the Month to and from the actual controls on the page.

So if I understand your writeup. The sequence of event would be something like this.

The user types in the days to add. The user clicks add days Add Days fires the event which calls a method on the Present to add days. The method in the presenter that add days will make it's calculation and other needed steps. The add days method will then use the View pointer in the Presenter to tell the view to update the Month with the calculated value. The View will then take the calculated value set the correct property on the control.

To do unit testing you need to make a mock object implementing IShowTimeView and use that in place of the actual page object.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!