C# Design Patterns: The Façade Pattern
- 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 Façade pattern is used to wrap a set of complex classes into a simpler enclosing interface. As your programs evolve and develop, they grow in complexity. In fact, for all the excitement about using design patterns, these patterns sometimes generate so many classes that it is difficult to understand the program's flow. Furthermore, there may be a number of complicated subsystems, each of which has its own complex interface.
The Façade pattern allows you to simplify this complexity by providing a simplified interface to these subsystems. This simplification may in some cases reduce the flexibility of the underlying classes, but it usually provides all the function needed for all but the most sophisticated users. These users can still, of course, access the underlying classes and methods.
Fortunately, we don't have to write a complex system to provide an example of where a Façade can be useful. C# provides a set of classes that connect to databases, using an interface called ADO.NET. You can connect to any database for which the manufacturer has provided a ODBC connection class almost every database on the market. Let's take a minute and review how databases are used and a little about how they work.
What Is a Database?
A database is a series of tables of information in some sort of file structure that allows you to access these tables, select columns from them, sort them, and select rows based on various criteria. Databases usually have indexes associated with many of the columns in these tables, so we can access them as rapidly as possible.
Databases are used more than any other kind of structure in computing. You'll find databases as central elements of employee records and payroll systems, in travel scheduling systems, and all through product manufacturing and marketing.
In the case of employee records, you could imagine a table of employee names and addresses and of salaries, tax withholding, and benefits. Let's consider how these might be organized. You can imagine one table of employee names, addresses, and phone numbers. Other information that you might want to store would include salary, salary range, last raise, next raise, employee performance ranking, and so forth.
Should this all be in one table? Almost certainly not. Salary ranges for various employee types are probably invariant between employees, and thus you would store only the employee type in the employee table and the salary ranges in another table that is pointed to by the type number. Consider the data in Table 181.
Table 18-1. Employee Names and Salary Type Tables
Key |
Lastname |
SalaryType |
1 |
Adams |
2 |
2 |
Johnson |
1 |
3 |
Smyth |
3 |
4 |
Tully |
1 |
5 |
Wolff |
2 |
SalaryType |
Min |
Max |
1 |
30000 |
45000 |
2 |
45000 |
60000 |
3 |
60000 |
75000 |
The data in the SalaryType column refers to the second table. We could imagine many such tables for things like state of residence and tax values for each state, health plan withholding, and so forth. Each table will have a primary key column like the ones at the left of each table and several more columns of data. Building tables in a database has evolved to both an art and a science. The structure of these tables is referred to by their normal form. Tables are said to be in first, second, or third normal form, abbreviated as 1NF, 2NF, or 3NF.
First. Each cell in a table should have only one value (never an array of values). (1NF)
Second. 1NF and every nonkey column is fully dependent on the key column. This means there is a one-to-one relationship between the primary key and the remaining cells in that row. (2NF)
Third. 2NF and all nonkey columns are mutually independent. This means that there are no data columns containing values that can be calculated from other columns' data. (3NF)
Today, nearly all databases are constructed so that all tables are in third normal form (3NF). This means that there are usually a fairly large number of tables, each with relatively few columns of information.