Now that we have moved all our class-oriented functionality to a central directory location (and have a reasonable test suite for those classes) we can begin extracting more logic from our page scripts and place that logic into classes. This will have two benefits: first, we will be able to keep the various concerns of the application separated; second, we will be able to test the extracted logic so that any breaks will be easy to notice before we deploy into production.
The first of these extractions will be to move all SQL-oriented code to its own set of classes. For our purposes, SQL is a stand-in for any system of reading from and writing to a data store. This may be a no-SQL system, a CSV file, a remote resource, or anything else. We will concentrate on SQL-oriented data stores in this chapter because they are so common throughout legacy applications, but the principles apply to any form of data storage.