如何将数据绑定和操纵它在MVP的GridView [英] How to Bind Data and manipulate it in a GridView with MVP

查看:106
本文介绍了如何将数据绑定和操纵它在MVP的GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的整个MVP的事情,慢慢变我的脑袋周围所有。在我有一个问题是如何填充GridView的时候留在MVP的方法是一致的(和的DDL,但我们会稍后处理)。

什么情况下可以把它直接连接到一个ObjectDataSourceID?对我来说,因为它绕过所有的顾虑MVP是为了做分离,这似乎是错误的。

因此​​,随着中说,我怎么办呢?如何处理排序(我送过来处理事件到presentation层,如果是的话请问是怎么看在$ C $三)?
现在我有一个没有排序的GridView。低于code。

ListCustomers.aspx.cs:

 公共部分类ListCustomers:System.Web.UI.Page,IlistCustomer
{
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    //在每个页面加载,创建一个新的presenter对象
    //构造recieving的    //页面的IlistCustomer看法
    LISTUSER presenter ListUser_P =新LISTUSER presenter(本);    //调用presenter的PopulateList将数据绑定到GridView控件
    ListUser_P.PopulateList();}GridView控件IlistCustomer.UserGridView
{
    {返回gvUsers; }
    集合{gvUsers =价值; }
}}

接口(IlistCustomer.cs):是这样的坏整个GridView控件发送

 公共接口IlistCustomer
{
GridView控件UserGridView {集;得到; }
}

在presenter(LISTUSER presenter.cs):

 公共类LISTUSER presenter
{
私人IlistCustomer view_listCustomer;
私人的GridView gvListCustomers;
私人数据表objDT;公共LISTUSER presenter(IlistCustomer视图)
{
    //处理一个错误,如果一个Ilistcustomer未发送的)
    如果(查看== NULL)
        抛出新的ArgumentNullException(ListCustomer查看不能为空);    //设置本地IlistCustomer接口视图
    this.view_listCustomer =视图。
}公共无效PopulateList()
{
    //填写当地的GridView与当地IlistCustomer
    gvListCustomers = view_listCustomer.UserGridView;    //实例化一个新CustomerBusiness对象联系数据库
    CustomerBusiness CustomerBiz =新CustomerBusiness();    //调用CustomerBusiness的GetListCustomers填补DataTable对象
    objDT = CustomerBiz.GetListCustomers();    //绑定到数据表的GridView;
    gvListCustomers.DataSource = objDT;
    gvListCustomers.DataBind();
}
}


解决方案

使用数据库感知控件,GridView的一样,是一个巨大的诱惑的便利。在理论上人们可以只推出自己的GridView和忠于MVP的设计。但是你会被重复工作,并给予企业的资源有限不是经常最明智的选择。由于节省时间可能会相当有令人信服的理由使用数据库感知控件。

妥协是通过code,其中控制连接到数据库的路径,清楚地记录。这样,如果当你迁移UI,后端或两者兼而有之,你可以看到什么是依赖于识别数据库的控制和后端。另外,也要看看在你的框架所提供的数据库API。你可能有一个接近通用的选择与改变后端减少的问题。

在规划设计中的关键问题要问的是会发生什么,如果我更改UI中,presenter,视图,模型或数据库后端。答案希望将带领您设计,允许变化。

I am new to the whole MVP thing and slowly getting my head around it all. The a problem I am having is how to stay consistent with the MVP methodology when populating GridViews (and ddls, but we will tackle that later).

Is it okay to have it connected straight to an ObjectDataSourceID? To me this seems wrong because it bypasses all the separation of concerns MVP was made to do.

So, with that said, how do I do it? How do I handle sorting (do I send over handler events to the presentation layer, if so how does that look in code)? Right now I have a GridView that has no sorting. Code below.

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): is this bad sending in an entire Gridview control?

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

The Presenter (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();
}
}

解决方案

The convenience of using a database aware control, like Gridview, is a huge temptation. In theory one could just roll their own gridview and remain true to the MVP design. But you will be duplicating work and giving the finite resources of businesses not often the wisest choice. Since the time saving can be considerable there are compelling reason to use database aware controls.

The compromise is to clearly document via code the path in which the control connects to the database. That way if and when you migrate the UI, backend or both, you can see what is dependent on the database aware control and the backend. Also look over the database apis offered by your framework. You may have a close to generic choice that minimize problem with changing backends.

When planning your design the key question to ask is "What happens if I change the UI, the presenter, the view, the model, or database backend. The answer hopefully will lead you to a design that allows for changes.

这篇关于如何将数据绑定和操纵它在MVP的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆