chapter six
6 The refactor: the repository layer (Customer)
This chapter covers
- Refactoring a legacy code base using clean code and defensive coding practices.
- Using Entity Framework Core to query a database
- Using Test-Driven Development to test against an in-memory database
In chapters 3 and 4, we looked at the codebase we inherited and discussed potential improvements. To solve the issues found, we started a new version of the Flying Dutchman Airlines service in chapter 5. We implemented the database access layer with Entity Framework Core in chapter 5, and now we are ready for the fun part: implementing the business logic.
Figure 6.1 After having implemented the Database Access layer in chapter 5, we move on to implementing the CustomerRepository in this chapter.
The repository layer is the meat and potatoes of our service. In the repository layer we do two things:
- Query and manipulate the database by communicating with the database access layer
- Return the requested entities or information to the service layer
We want to create isolated, small, clean, and readable methods that follow the Single Responsibility Principle (SRP). Following the SRP makes it easier to test and maintain our code because we can quickly understand every method and fix any potential bugs.