concept CustomerRepository in category c#

appears as: CustomerRepository, CustomerRepository
Code like a Pro in C#

This is an excerpt from Manning's book Code like a Pro in C#.

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. In this chapter, we start implementing the business logic by moving into the repository layer and creating the CustomerRepository class.

Figure 6.1 After having implemented the database access layer in chapter 5, we move on to implementing the CustomerRepository in this chapter.

Throughout this chapter, we use the “UnitTest1.cs” file and adapt it to suit our needs for the first repository: CustomerRepository. Why start with CustomerRepository as opposed to FlightRepository or BookingRepository? (To refresh our memories, when we finish implementing them, we have one repository per database model (Customer, Flight, Airport, Booking. Within these repositories we perform atomic operations that query or manipulate the database) The Customer entity does not have any outgoing foreign key constraints, so we are less likely to get into the rabbit hole of needing to create a repository for a different entity before we can finish the one we really want to work on. In my experience, it is easier working from the lowest (most nested / least foreign key constraints) entity up to the highest. By the time you reach the entity with the most constraints, you have all the dependencies already done. This is the same argument as to why we started (in chapter 5) with the database access layer and not the controller level for the overall architecture implementation of our next-gen service.

Before we write our first unit test, let’s create the repository class and the skeleton of our first method: CreateCustomer. The CreateCustomer method accepts an input of type string representing a customer’s name, validates that input, and inserts a new entity into the database. CustomerRepository lives in a new folder called “RepositoryLayer” in the FlyingDutchmanAirlines project.

namespace FlyingDutchmanAirlines.RepositoryLayer
{
    public class CustomerRepository
    {
        public void CreateCustomer(string name) 
        {
        }
    }
}

The CustomerRepository method doesn’t look like much at this point: just a class declaration and one method, both with a public access modifier, but it is enough to get us started with our first unit test. In keeping with TDD tradition, we follow a binary strategy akin to a red-green traffic light pattern.