我正在尝试使用 WebformsMVP 框架将 Webforms 应用程序重构为 Model-View-Presenter (MVP)。在创建演示者和调整视图代码时,我遇到了一个动态填充其某些子控件数据的视图。这是我正在处理的视图的非重构版本背后的代码:
[PresenterBinding(typeof(OrderDetailsPresenter))]
public partial class OrderDetails : AuthorizedPageBase<OrderDetailsPage>, IOrderDetails
{
private PurchaseOrder _orderDetail;
private int _orderGroupId;
protected new void Page_Load(object sender, EventArgs e)
{
base.Page_Load(sender, e);
_orderGroupId = string.IsNullOrEmpty(Request.QueryString["po"])
? _orderGroupId = 0
: _orderGroupId = Int32.Parse(Request.QueryString["po"].ToString(CultureInfo.InvariantCulture));
_orderDetail = OrderContext.Current.GetPurchaseOrder(SecurityContext.Current.CurrentContactId, _orderGroupId);
if (_orderDetail == null)
return;
if (_orderDetail.OrderForms[0].Shipments.Count > 1)
{
var control = (OrderDetailsMultiShipment)LoadControl("~/Templates/Sample/Units/AccountManagement/OrderDetailsMultiShipment.ascx");
control.OrderDetail = _orderDetail;
modulePlaceHolder.Controls.Add(control);
}
else
{
var control = (OrderDetailsSingleShipment)LoadControl("~/Templates/Sample/Units/AccountManagement/OrderDetailsSingleShipment.ascx");
control.OrderDetail = _orderDetail;
modulePlaceHolder.Controls.Add(control);
}
}
}
如您所见,根据值,一个控件或另一个控件加载了多个信息。我的想法是:
- 将 PurchaseOrder 属性移动到演示者
- 尝试从 Page_Load 中移动尽可能多的逻辑
- 将控件重构为普通且不填充任何 OrderDetail 属性(对我来说最令人困惑的问题)
我尝试了以下方法来解决这些问题(创建演示者填充数据的事件,然后由视图检查):
[PresenterBinding(typeof(OrderDetailsPresenter))]
public partial class OrderDetails : AuthorizedPageBase<OrderDetailsPage>, IOrderDetails
{
public event EventHandler<GetShipmentsArgs> GetShipments;
protected new void Page_Load(object sender, EventArgs e)
{
base.Page_Load(sender, e);
var args = new GetShipmentsArgs();
GetShipments(this, args);
if (args.Shipments.Count > 1)
{
var control = (OrderDetailsMultiShipment)LoadControl("~/Templates/Sample/Units/AccountManagement/OrderDetailsMultiShipment.ascx");
control.OrderDetail = _orderDetail; //Here I duplicate the problem I tried to solve...
modulePlaceHolder.Controls.Add(control);
}
else
{
var control = (OrderDetailsSingleShipment)LoadControl("~/Templates/Sample/Units/AccountManagement/OrderDetailsSingleShipment.ascx");
control.OrderDetail = _orderDetail; //Here I duplicate the problem I tried to solve...
modulePlaceHolder.Controls.Add(control);
}
}
是的,我知道我的代码无法编译,但我希望它能阐明我的想法。我的方法仍然有几个问题: - View 仍然有决定加载哪个控件的逻辑,并且必须加载要加载的控件的信息。
我考虑过一个演示者间通信系统,但我不确定它是否是最佳解决方案。在 MVP 范式上重构我的初始代码的最佳方式是什么?
谢谢