This project is read-only.

ObjectContainerDataSource

The ObjectContainerDataSource QuickStart illustrates the key usage scenarios of ObjectContainerDataSource control.

Building and Running the QuickStart

The QuickStart ships as source code, which means you must compile it before running it. This QuickStart does not require any additional setup.

To build and run the QuickStart:
  • Open the solution file ObjectContainerDataSourceQuickstart.sln.
  • On the Build menu, click Rebuild Solution.
  • Press F5 to run the QuickStart.

Implementation Notes

The QuickStart contains the views named CustomersSimpleView and CustomersAdvancedView. These views demonstrate common usage scenarios of the ObjectContainerDataSource control in the context of the View-Presenter pattern. Both views perform select operations, update operations, delete operations, and insert operations and use the sorting and paging capabilities of the control.
CustomersSimpleView uses the default paging and sorting functionality of the ObjectContainerDataSource control and CustomersAdvancedView uses custom paging and sorting. For more information about paging and sorting with the ObjectContainerDataSource control, see Paging and Sorting. The two views both use an ASP.NET GridView control to display the data from the ObjectContainerDataSource control, as shown in Figure 1.

wcsf-ocds.jpg
ASP.NET GridView bound to an ObjectContainerDataSource control

Both views contain a property named Customers in the code-behind file. This property is set by the presenter with items that the view displays.

C#
public IList<Customer> Customers
{
    set { CustomersDataSource.DataSource = value; }
}


The presenter for the CustomersSimpleView sets the Customers property of the view when the view loads. This means that after the view loads it will display all available customers.

C#
public override void OnViewLoaded()
{
    View.Customers = _controller.GetCustomers();
}


In contrast, the presenter for the CustomersAdvancedView sets the Customers property of the view in the OnSelecting event. It does this to display a page of Customer objects when the control requests the data.

C#
public void OnSelecting(int startRowIndex, int maximumRows, string sortExpression)
{
    View.Customers = _controller.GetCustomers(startRowIndex, maximumRows, sortExpression);
    View.TotalCustomersCount = _controller.CustomersTotalCount;
}


To support update operations, delete operations, and insert operations, both views handle the OnUpdated, OnDeleted and OnInserted events. The views forward the events to their presenters, as shown in the following code.

C#
protected void CustomersDataSource_Inserted(object sender, ObjectContainerDataSourceStatusEventArgs e)
{
    _presenter.OnCustomerInserted((Customer)e.Instance);
}

protected void CustomersDataSource_Deleted(object sender, ObjectContainerDataSourceStatusEventArgs e)
{
    _presenter.OnCustomerDeleted((Customer)e.Instance);
}

protected void CustomersDataSource_Updated(object sender, ObjectContainerDataSourceStatusEventArgs e)
{
    _presenter.OnCustomerUpdated((Customer)e.Instance);
}


To support custom paging and sorting, the CustomersAdvancedView contains the following additional members in the code-behind file:
  • TotalCustomersCount property. This presenter sets this property to specify the total amount of Customer objects contained in the data store. This value is forwarded to the TotalRowCount property of the ObjectContainerDataSource control. Data-bound controls use this value to render paging elements, such as page numbers.

C#
public int TotalCustomersCount
{
    set { CustomersDataSource.TotalRowCount = value; }
}

  • Event handler for the Selecting event. This ObjectContainerDataSource control raises this event before it performs a select operation. The view handles this event and forwards it to the presenter. In turn, the presenter sets the Customers property of the view with the Customer objects for the current page.

C#
protected void CustomersDataSource_Selecting(object sender, ObjectContainerDataSourceSelectingEventArgs e)
{
    _presenter.OnSelecting(e.Arguments.StartRowIndex, e.Arguments.MaximumRows, e.Arguments.SortExpression);
}

Related Software Factory Assets

The following assets are related to this QuickStart:
  • ObjectContainerDataSource control
  • How to: Use the ObjectContainerDataSource Control
  • View-Presenter pattern
  • Add View (with presenter) Recipe
  • How to: Add a View with a Presenter
  • Reference implementation example:
    • NewTransferView.aspx and code-behind file NewTransferView.aspx.cs

Last edited Jan 8, 2007 at 10:47 PM by eugeniop, version 2

Comments

JohnnyG Jun 27, 2008 at 5:47 AM 
I think it is strange that the 'ed' events of the ObjectContainerDataSource (e.g. objectContainerDataSource_ItemUpdated) are the places where you are supposed to do the actual even. For example, i fire the update event on a DetailsView. The pre event fires. That is cool. Then the post event fires and this is where I am supposed to execute the actual event. That is strange. Confusing.

rkralston Dec 11, 2007 at 8:39 PM 
This looks pretty nifty. My worry is how much work it takes to work around features this architecture does not consider. All you samples and the RI are trivial. If I commit to this, when I am 70% finished and need that certain capability not supported in this package (which I never know about until I get there). How difficult is this to customize and get down to the nuts and bolts of data and HTML?

Off the top of my head, difficult to handle items can be calculated fields, normalized data represented by IDs, and hierarchical data. I know there must be others, but they aren't on the top of my head right now. :-)

rkralston Dec 11, 2007 at 8:11 PM 
In the sample, you guys loaded the status dropdown with items. What would your preference be for a data driven dropdown? Would you have the BE contain the status ID, then translate in the UI, or would you do all that in the BE?

SiTox Jul 23, 2007 at 12:23 PM 
How to use ObjectContainerDataSource with Profiles ?
if i want to create User Class with Profile Fields ?