问题
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