gpt4 book ai didi

asp.net - 如何使用 MVP 在 GridView 中绑定(bind)数据并对其进行操作

转载 作者:行者123 更新时间:2023-12-02 00:15:00 25 4
gpt4 key购买 nike

我对 MVP 的事情还很陌生,并且正在慢慢地了解这一切。我遇到的问题是如何在填充 GridView(和 ddls,但我们稍后会解决这个问题)时与 MVP 方法保持一致。

可以将其直接连接到 ObjectDataSourceID 吗?对我来说,这似乎是错误的,因为它绕过了 MVP 所做的所有关注点分离。

话虽如此,我该怎么做呢?如何处理排序(是否将处理程序事件发送到表示层,如果是的话,在代码中看起来如何)?现在我有一个没有排序的 GridView。代码如下。

ListCustomers.aspx.cs:

public partial class ListCustomers : System.Web.UI.Page, IlistCustomer
{
protected void Page_Load(object sender, EventArgs e)
{
//On every page load, create a new presenter object with
//constructor recieving the

// page's IlistCustomer view
ListUserPresenter ListUser_P = new ListUserPresenter(this);

//Call the presenter's PopulateList to bind data to gridview
ListUser_P.PopulateList();

}

GridView IlistCustomer.UserGridView
{
get { return gvUsers; }
set { gvUsers = value; }
}

}

接口(interface)(IlistCustomer.cs):在整个 Gridview 控件中发送是否错误?

public interface IlistCustomer
{
GridView UserGridView { set; get; }
}

演示者(ListUserPresenter.cs):

public class ListUserPresenter
{
private IlistCustomer view_listCustomer;
private GridView gvListCustomers;
private DataTable objDT;

public ListUserPresenter( IlistCustomer view)
{
//Handle an error if an Ilistcustomer was not sent in)
if (view == null)
throw new ArgumentNullException("ListCustomer View cannot be blank");

//Set local IlistCustomer interface view
this.view_listCustomer = view;
}

public void PopulateList()
{
//Fill local Gridview with local IlistCustomer
gvListCustomers = view_listCustomer.UserGridView;

// Instantiate a new CustomerBusiness object to contact database
CustomerBusiness CustomerBiz = new CustomerBusiness();

//Call CustomerBusiness's GetListCustomers to fill DataTable object
objDT = CustomerBiz.GetListCustomers();

//Bind DataTable to gridview;
gvListCustomers.DataSource = objDT;
gvListCustomers.DataBind();
}
}

最佳答案

我将首先强调 MVP 作为模式解决的主要问题。首先是通过关注点分离来实现模块化。这意味着理想情况下可以将 View 层从 Web 窗体更改为 Windows 窗体,而无需更改 Presenter 层和模型层中的任何内容。其次,使用 MVP 设计的应用程序很容易进行单元测试,因为测试表单非常困难。考虑到这一点,您就会意识到不应在 Presenter 层中操作以 View 为中心的对象(例如网格),因为当 View 更改时,控件也可能会更改。为了回答您的问题,我可以通过以下方式解决您的问题:

// View
public interface IListCustomersView
{
public void BindGrid(IList<Customer> customers);
}

// Form e.g. Web Form, Windows Form
public class ListCustomers : IListCustomersView
{
private ListCustomersPresenter listCustomerPresenter = null;

public ListCustomers()
{
// You can use a Dependency Injector here
this.listCustomersPresenter = new ListCustomerPresenter(
new CustomerRepository(),
this);
}

public void BindGrid(IList<Customer> customers)
{
grid.DataSource = customers;
grid.Databind();
}
}

// Presenter
public class ListCustomersPresenter
{
private readonly IListCustomersView view = null;
private readonly ICustomerRespository repository = null;

public ListCustomersPresenter(
ICustomerRespository customerRepository, IListCustomersView view)
{
Guard.AgainstNull(view,"View");
Guard.AgainstNull(customerRepository,"CustomerRepository");

this.view = view;
this.customerRepository = customerRepository;
}

public void BindGrid()
{
// Fetch customers from repository
IList<Customer> customers = this.customerRepository.GetCustomers();
view.BindGrid(customers);
}
}

关于asp.net - 如何使用 MVP 在 GridView 中绑定(bind)数据并对其进行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2381661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com