- What Is a Database?
- Getting Data Out of Databases
- Kinds of Databases
- ODBC
- Database Structure
- Using ADO.NET
- Adding Rows to Database Tables Using ADO.NET
- Building the Façade Classes
- Making the ADO.NET Façade
- Creating Classes for Each Table
- Building the Price Table
- Loading the Database Tables
- The Final Application
The Final Application
The program loads a list of food prices into a list box on startup.
private void loadFoodTable() { Foods fods =new Foods(db); fods.openTable(); while (fods.hasMoreElements()){ lsFoods.Items.Add(fods.getValue()); } }
And it displays the prices of the selected food when you click on it.
private void lsFoods_SelectedIndexChanged(object sender, System.EventArgs e) { string food = lsFoods.Text; DataTable dtable = prc.getPrices(food); lsPrices.Items.Clear(); foreach (DataRow rw in dtable.Rows) { lsPrices.Items.Add(rw["StoreName"].ToString().Trim() + "\t" + rw["Price"].ToString()); } }
The final program was shown in Figure 18-1.
What Constitutes the Façade?
The Façade in this case wraps the classes as follows.
DBase
Contains OleDbConnection, Database, DataTable, OleDbAdapter, DataSets
DBTable
Contains DataSet, DataRow, DataTable, OleDbConnection, OleDbCommandBuilder
You can quickly see the advantage of the Façade approach when dealing with such complicated data objects. This is illustrated in Figure 18-2.
Figure 18-2. Façades wrapping classes make up the DBase and DBTable classes.
Consequences of the Façade
The Façade pattern shields clients from complex subsystem components and provides a simpler programming interface for the general user. However, it does not prevent the advanced user from going to the deeper, more complex classes when necessary.
In addition, the Façade allows you to make changes in the underlying subsystems without requiring changes in the client code and reduces compilation dependencies.
Thought Question
Suppose you had written a program with a File | Open menu, a text field, and some buttons controlling font (bold and italic). Now suppose that you need to have this program run from a line command with arguments. Suggest how to use a Façade pattern.
Program on the CD-ROM
C# Database Façade Classes |
\Façade\ |