1. PVwith theSCThe best way to solve a view that is hard to test is to keep him from testing. If the view does not require testing, the prerequisite is to make it as much as possible without involving the UI processing logic, which is the purpose of the PV mode.
If we design the view purely using the PV mode, we need to expose the UI elements in the view through attributes. Specifically, when we define interfaces for a view, we need to define UI element-based attributes so that presenter can manipulate the view fine-grained, but that doesn't mean we expose the controls directly on the view.
For example, if we develop an HR system with such an interface, there is a DropDownList with a list of all the departments, there is a GridView that displays the list of employees, and a query button that we can display by the staff of the selected department through the query button.
If you define an interface for the view Iemployeeview we cannot display the above two controls directly as attributes, as shown in the following code. Data binding for a specific control type belongs to the internal details of the view and cannot be reflected in the interface that represents the abstract view.
public interface Iemployeeview{<span style= "White-space:pre" ></span> DropDownList departments{get;} <span style= "White-space:pre" ></span>gridview<span style= "White-space:pre" ></span> Employees {get;}}
The correct interface and the View that implements the interface should be defined as follows:Presenter by the properties departments and Employees assign values to the corresponding DropDownList and the GridView data binding, while passing properties selecteddepartment Gets the selection department selected by the user. In order to let the interface expose only the necessary information, we also deliberately control the reading / Writing of the property .
public interface Iemployeeview{<span style= "White-space:pre" ></span>IEnumerable<String> departments{set;} <span style= "White-space:pre" ></span>stringselecteddepartment{get;} <span style= "White-space:pre" ></span>ienumerable<employee><span style= "White-space:pre" > </span>employees{set;}} Public partial class Employeeview:page, Iemployeeview{<span style= "White-space:pre" ></span>public ienumerable<string> departments<span style= "White-space:pre" ></span>{<span style= " White-space:pre "></span>set<span style=" White-space:pre "></span>{<span style=" White-space:pre "></span>this. Dropdownlistdepartments.datasource = Value;<span style= "White-space:pre" ></span>this. Dropdownlistdepartments.databind (); <span style= "White-space:pre" ></span>}<span style= "White-space :p re "></SPAN>} <span style=" White-space:pre "></span>public StrinG Selecteddepartment<span style= "White-space:pre" ></span>{<span style= "White-space:pre" ></ Span>get{<span style= "White-space:pre" ></span>return this. Dropdownlistdepartments.selectedvalue;<span style= "White-space:pre" ></span>}<span style= " White-space:pre "></span>}<span style=" White-space:pre "></span>public IEnumerable< employee> employees<span style= "White-space:pre" ></span>{<span style= "White-space:pre" ></ Span>set<span style= "White-space:pre" ></span>{<span style= "White-space:pre" ></span> This. Gridviewemployees.datasource = Value;<span style= "White-space:pre" ></span>this. Gridviewemployees.databind (); <span style= "White-space:pre" ></span>}<span style= "White-space:pre" ></span>}}
PVmode will all theUIThe processing logic is all defined inPresenteron, means that all theUIprocessing logic can be tested, which is a good choice from a testability standpoint. But it requires that theViewis available for operation in theUIelement is defined in the corresponding interface, for some complex rich clients (Rich Client) Application ofView, the number of interface members can become many. In addition, becausePresenterneeds to be at the control levelViewfine-grained control, which often complicates the original simple logic. In this case we tend to useSCmode.
InSCmode, in order to reducePresentercomplexity, we tend to have a simple way to format data binding and display dataUIThe processing logic is transferred toView, these processing logic is reflected in theViewimplemented in the interface, althoughViewfromPresentertook over the partUIprocessing logic, butPresenteris still the driver of the entire triangular relationship,Viewthe passive status remains unchanged. For user role inViewthe interactive operation on theViewitself does not respond, it only forwards the interaction request toPresenter, which completes the corresponding processing process independently (which may involveModelcall) will then drive theViewresponds to user interaction requests.
Learn the ASP. NET MVC framework Secret notes-PV and SC