Model-View-Presenter in ASP.NET with ListView and Repeater

I studied the MVP pattern using ASP.NET, but I had problems saving all the presentation logic in the presenter when using the data binding control on the page.

This script and classes are just an example, and the real case I'm working with is more complex. Any ideas or guidance would be greatly appreciated.

Say that I have a page with customer information, including name and address. It also displays a list of orders using the 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; }
}

, . - , ItemDataBound IsRush . -, , .

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;
            }
        }
    }
}

, Presenter View, . , , , , , .

!

+3
2

. , .

- . Foreach order , .

OrderDto orderDto = e.Item.DataItem as OrderDto;
controller.Visit(this, orderDto);

//- /

void Visit(ISomeView view, OrderDto dto) {
   if (dto.IsRush) {
        view.RenderRushOrder(dto);
   } else {
        view.RenderNornamlOrder(dto);
   }
}

, . IMO -, ASP MVC

, .

+4

ForeColor , .

CssClass ForeColor DTO. , ​​ OnItemDataBound.

+1

Source: https://habr.com/ru/post/1722190/


All Articles