简体   繁体   English

表示层中的控件修改

[英]Control modification in presentation layer

我正在使用GridView并且数据绑定发生在Presenter层中,但是例如必须将单元格1修改并转换为HyperLink控件,然后我必须在Presenter层中调用RowDataBound事件并在该事件内进行修改。 MVP可以吗?

I would typically do the data binding and event handling at the View level. 我通常会在视图级别执行数据绑定和事件处理。 By doing it in the Presenter, you are creating a dependency between the Presenter and the View that you want to avoid. 通过在Presenter中执行此操作,可以在Presenter和要避免的View之间创建依赖关系。 I'm not sure how you would unit test a Presenter method that's calling .DataBind() on a GridView. 我不确定如何对在GridView上调用.DataBind()的Presenter方法进行单元测试。

What I would do (and what I believe is standard) is add a property to the code-behind of your view class that represents the data for the GridView. 我要做的(也是我认为是标准的)是在视图类的代码背后添加一个属性,该属性表示GridView的数据。 So say your GridView shows employees, the property might be something like 因此,假设您的GridView显示了员工,则该属性可能类似于

public List<Employee> Employees 
{ 
    get { return (List<Employee>)GridView1.DataSource; }
    set // The Presenter calls this
    {
        GridView1.DataSource = value;
        GridView1.DataBind();
    }
}

The presenter would simply set this property and then you would do the data binding and event handling as you typically would with webforms. 演示者只需设置此属性,然后就可以像使用Webforms一样进行数据绑定和事件处理。

This will also allow you to unit test your Presenter if you wish to. 如果需要,这还可以使您对Presenter进行单元测试。 Assuming that your view implements an interface, you can use a different implementation for your unit test, ie the setter wouldn't call .DataBind() , it might simply be an automatic property. 假设您的视图实现了一个接口,则可以对单元测试使用其他实现,即setter不会调用.DataBind() ,它可能只是一个自动属性。 You could create a mock view, pass it to the Presenter, and then test that your property is not null, or something along those lines. 您可以创建一个模拟视图,将其传递给Presenter,然后测试您的属性不为null或类似的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM