使用MVP-如何正确使用事件进行测试
时间:2020-03-05 18:43:57 来源:igfitidea点击:
我刚刚开始在要构建的ASP.NET大型应用程序中使用MVP模式(实际上是在重新构建),并且我很难确定应该如何将事件应用到视图中。
假设我在一个用户控件中有2个下拉列表,其中一个依赖于另一个的值:
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="ucTestMVP.ascx.vb" Inherits=".ucTestMVP" %> <asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="True" /> <asp:DropDownList ID="ddlCity" runat="server" />
如何在界面中定义AutoPostBack事件?这应该是由用户控件处理的事件,如下所示:
Public Partial Class ucTestMVP Inherits System.Web.UI.UserControl Implements ITestMVPView Protected Sub PageLoad(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then Dim presenter As New TestMVPPresenter(Me) presenter.InitView() End If End Sub Private Sub ddlCountrySelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCountry.SelectedIndexChanged Dim presenter as New TestMVPPresenter(Me) presenter.CountryDDLIndexChanged() End Sub End Class
还是应该在接口上定义一个事件?如果这是首选模式,如何添加要处理和使用的事件?
解决方案
回答
我不知道是否存在普遍偏爱的模式。我倾向于将事件添加到视图界面,并让主持人响应视图。我在这里更详细地描述了这种模式。