How to link and manage data in a GridView using MVP

I am new to all MVP stuff and slowly hugging it all. The problem I am facing is how to stay in accordance with the MVP methodology when populating GridViews (and ddls, but we will solve this later).

Is it possible to connect it directly to ObjectDataSourceID? To me, this seems wrong, because it circumvents all the separation of problems that MVP has made.

So, with that said, how do I do this? How to handle sorting (whether to send messages to handlers at the presentation level, if so, how does it look in the code)? Right now I have a GridView that does not sort. The code is 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 IlistCustomer view 
    ListUserPresenter ListUser_P = new ListUserPresenter(this);

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

}

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

}

Interface (IlistCustomer.cs): is it a bad submission of the entire Gridview control?

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

Host (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 GetListCustomers to fill DataTable object
    objDT = CustomerBiz.GetListCustomers();

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

, , Gridview, . gridview MVP. . , .

, , . , , , , , . apis, . , , .

: " , , , , . , , , .

+1

, MVP . -, . , "", , " -" Windows, - . -, , MVP, , . , , View-centric, Grid, Presenter, 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);          
    }
}
+3

;

public interface IlistCustomer
{
   PopulateCustomers(IEnumerable<Customer> customers);
}

public class ListUserPresenter
{
private IlistCustomer _view;

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

    _view = view;
}

public void PopulateList()
{
   //Injecting your DAL seems like a good choice here
    CustomerBusiness CustomerBiz = new CustomerBusiness();    

    IEnumerable<Customer> customers = CustomerBiz.GetListCustomers();

    _view.PopulateCustomers(customers);
}
}
+1

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


All Articles