O'Reilly logo

Programming ASP.NET 3.5, 4th Edition by Jesse Liberty, Dan Hurwitz, Dan Maharry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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)); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required