In the previous chapter, we extracted all our SQL statements to a layer of Gateway objects. This encapsulates the interactions between the application and the database.
However, we usually need to apply some amount of business or domain logic to the data coming from and going back to the database. The logic can include things like data validation, adding or modifying values for presentation or calculation purposes, collecting simpler records into more complex ones, using the data to perform related actions, and so on. This domain logic is often embedded into a page script, making that logic difficult to reuse and test.
This chapter describes one way to extract domain behaviors into a separate layer. In many ways, this chapter forms the very core of the book: everything before now has led us to this central concern of the legacy application, and everything that comes after will lead us into layers above and around this core functionality.