concept Customer in category c#

appears as: Customer
Code like a Pro in C#

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

Looking at figure 3.9, the Flight and Booking tables are the only entities with foreign key relationships (two each). In the Flight table, Flight.Origin ties to Airport.AirportID, and so does Flight.Destination. In the Booking table, Booking.FlightNumber ties to Flight.FlightNumber, and Booking.CustomerID ties to Customer.CustomerID. Then, finally, there are the Airport and Customer tables, who have no outgoing foreign key relationships (they both have one incoming foreign key relationship). All four tables in the schema have a Primary Key, as denoted by the upright key icon.

In the previous chapter, we implemented the CustomerRepository where we can add a customer to the database. We also learned how to use dependency injection to write testable code. This is a splendid start, but we are not done yet. We can add a Customer instance to the database, but how about retrieving one?

Figure 7.1 In this chapter, we continue the implementation of the CustomerRepository we started in chapter 6. This is the first step in implementing all the repositories for the Flying Dutchman Airlines service.

In this chapter, we create the GetCustomerByName method that returns an appropriate Customer object when given a string containing the customer’s name. As before, we use Test-Driven Development to ensure our code quality is adequate.

  • The returned Customer instance is not null.
  • The Customer instance has valid data for the CustomerId, Name, and Booking fields.
  • Those should be fairly simple assertions to write. Let’s assert that the returned Customer instance is not null:

    [TestMethod]
    public void GetCustomerByName_Success()
    {
        Customer customer = 
    ➥await repository.GetCustomerByName("Linus Torvalds");
        Assert.IsNotNull(customer);
    }

    Since C# is an explicitly typed language, when we request a Customer instance from GetCustomerByName, the method likely throws an exception if customer is null because null is not the expected type. Nevertheless, we should be on the safe side and check for it.