I've been exploring the MVP pattern with ASP.NET but I have had trouble keeping all of the presentation logic in the presenter when using a databound control on the page.
This following scenario and classes are just for example and the real case I'm working with is more complex. Any ideas or direction would be greatly appreciated.
Say that I have a page which displays info about a Customer including Name and Address. It also renders a list of Orders using a Repeater control.
public class CustomerDto {
public string Name { get; set; }
public string Address { get; set; }
public List<OrderDto> OrderList { get; set; }
}
public class OrderDto {
public int Id { get; set; }
public decimal Amount { get; set; }
public bool IsRush { get; set; }
}
At first I had the presenter set the Name, Address and OrdrerList on the view. At this point there was still some presentation logic that was occurring in the ItemDataBound event of the Repeater depending on the value of IsRush on the order. In my opinion this logic doesn't belong in the code-behind but in a testable presenter class.
public interface IOrderView {
void SetName(string name);
void SetAddress(string address);
void SetOrderList(List<OrderDto> orderList);
}
public partial class OrderPage : Page, IOrderView
{
public void SetName(string name) {
labelName.Text = name;
}
public void SetAddress(string address) {
labelAddress.Text = address;
}
public void SetOrderList(List<OrderDto> orderList) {
repeaterOrders.DataSource = orderList;
repeaterOrders.DataBind();
}
protected void repeaterOrders_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
OrderDto orderDto = e.Item.DataItem as OrderDto;
if (orderDto.IsRush) {
Label labelOrderId = (Label)e.Item.FindControl("labelOrderId");
labelOrderId.ForeColor = System.Drawing.Color.Red;
}
}
}
}
It almost seems like each item in the repeater needs its own Presenter and View but I haven't found any similar example anywhere. I have come up with a couple of ways to keep all the presentation logic in the presenter but they all feel like a hack and I was wondering hope people generally handle this situation.
Thanks!