Using the ObjectDataSource Control

Most of the applications we’ll look at in this book are two-tier, separating the user interface from the backend data. Many larger commercial applications, however, are n-tier, with at least one middle business-logic layer to separate the retrieval of data from a database from the manipulation (and validation) of that data before presentation. This separation means that business-layer logic can be more readily tested and refactored so code is not duplicated between pages when it does not need to be.

Take, for example, the Customer class in Example 7-1. It’s necessarily simple for the purposes of this example and generates its own data rather than querying any external source of data, but the key idea is that it contains several methods—such as GetCustomers—returning different views of the customer data in the database as a generic DataSet container for the ObjectDataSource to then access and present onscreen.

Example 7-1. A simple business layer object class

using System.Data; /// <summary> /// An example Customer class /// </summary> public class Customer { public int CustomerID { get; set; } public string Name { get; set; } public string City { get; set; } public Customer( ) { } /// <summary> /// Gets Customers and returns them in DataSet /// </summary> public DataSet GetCustomers( ) { // Add logic here for actual retrieval of data from DB DataSet ds = new DataSet( ); DataTable dt = new DataTable("Customers"); dt.Columns.Add("CustomerId", typeof(System.Int32)); ...

Get Programming ASP.NET 3.5, 4th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.