Control modification in presentation layer

痞子三分冷 提交于 2020-01-04 05:48:05

问题


I am using GridView and the data binding occurs in Presenter layer but the cell 1 for example has to be modified and converted to a HyperLink control then I have to call the RowDataBound event in Presenter layer and do the modification inside that event.Is this a OK with MVP?


回答1:


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, i.e. 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.



来源:https://stackoverflow.com/questions/8339303/control-modification-in-presentation-layer

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