简体   繁体   中英

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. I'm not sure how you would unit test a Presenter method that's calling .DataBind() on a GridView.

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. So say your GridView shows employees, the property might be something like

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.

This will also allow you to unit test your Presenter if you wish to. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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