skip to the main content area of this page
Patterns and Practices

ObjectContainerDataSource Using GridView and ASP.NET MVP ( Model View Presenter )


Over the weekend I was dogfooding a number of the components and controls that make up the Web Client Software Factory ( WCSF ) v2.0 on PnPGuidance. PnPGuidance uses ASP.NET Model-View-Presenter throughout and a GridView Control to display various kinds of information for administration.

Here is a snapshot of the very simple area to view and edit existing posts. It uses the RealTimeSearchMonitor Control for a bit of live filtering and a GridView Control in an UpdatePanel that uses the ObjectContainerDataSource Control as its DataSource.

 

ObjectContainerDataSource

 

Here is a snapshot of the page in design mode so you can see the controls:

 

ModelViewPresenter

 

Since I am using Model View Presenter, the ObjectContainerDataSource is absolutely key to this design. Without it, you wouldn't be able to use the GridView using an MVP approach.

The key is that the ObjectContainerDataSource fires off events for inserting, deleting, updating, and selecting, which allows the presenter class to subscribe to those events or the page class to pass those events to the Presenter Class. Here is an example of the selecting event and the page class delegating the event to the Presenter Class. You could choose to have the Presenter Class subscribed directly to the event just as easily:

 

void ObjectContainerDataSource1_Selecting(object sender,

                ObjectContainerDataSourceSelectingEventArgs e)

{

    _startRow = e.Arguments.StartRowIndex;

    _maxRows = e.Arguments.MaximumRows;

    _sortExpression = e.Arguments.SortExpression;

    _presenter.OnGetPosts();

}

 

The Presenter Class can then call the domain model to get the posts and send it back to the view. It gets the appropriate data from the view, requests the information from the BlogService, and then updates the Posts and Total Records retrieved.

 

public void OnGetPosts()

{

    int totalRows;

    View.Posts = _blogService.GetPosts(View.StartRow,

        View.MaxRows, View.Filter, View.SortExpression, out totalRows);

    View.TotalRows = totalRows;

}

 

The ObjectContainerDataSource helps you do server-side paging, which is what I am using in this case, by just having you set a TotalRows Property on it. So the View internally is providing the new page of records as well as the total number of records in the Posts and TotalRows Properties which look like this:

 

public IList Posts

{

    set

    {

        ObjectContainerDataSource1.DataSource = value;

    }

}

 

public int TotalRows

{

    set

    {

        ObjectContainerDataSource1.TotalRowCount = value;

    }

}

 

The View Interface Contract is as follows:

 

public interface IViewPostsView

{

    string Filter { get; }

    int StartRow { get; }

    int MaxRows { get; }

    string SortExpression { get; }

 

    IList Posts { set; }

    int TotalRows { set; }

}

 

The key to getting the GridView to work with Model View Presenter is the ObjectContainerDataSource. The SqlDataSource Control talks directly to the database, which obviously won't work, and the ObjectDataSource uses a business object somewhere in the middle-tier. The ObjectContainerDataSouce Control, however, uses events that can be subscribed to by the Presenter or delegated to the Presenter from the UI.

You can find the ObjectContainerDataSource in the Composite Web Client Library and Composite Web Client Automation Guidance Bundles that ship as a part of the WCSF v2.0. It is in an assembly, called Microsoft.Practices.Web.UI.WebControls.


Tags: CompositeWebApplicationBlock, ModelViewPresenter, ObjectContainerDataSource


Topics



Popular Tags



Recent Links