11 Runtime type checking revisited and error handling

 

This chapter covers

  • Using the Assert.IsInstanceOfType test assertion
  • Calling multiple repositories from a service class
  • Using the discard operator
  • Using multiple catch blocks
  • Checking types at runtime with the is and as operators

After having implemented the database access layer in chapter 5 and the repository layer in chapters 6 through 9, we started to implement the BookingService in chapter 10. I also introduced you to using mocks in unit testing, and we discussed the repository/service pattern. In this chapter, we’ll use those concepts and draw on our knowledge of service layers to wrap up the BookingService implementation. Figure 11.1 shows where we are in the scheme of the book.

Figure 11.1 In this chapter we finish implementing the BookingService class. In the next chapter, we wrap up the Services layer by implementing the AirportService and FlightService classes.

While we finish implementing the BookingService, this chapter also discusses using the Assert.IsInstanceOfType test assertion to verify an object is of a certain type (or derived of a certain type), the discard (_) operator and its effect on Intermediate Language, and using multiple catch blocks in a try-catch code block.

To wrap up the BookingService implementation, we need to do the following:

  • Validate the input parameters of the BookingService.CreateBooking method (section 11.1).
  • Verify that the flight we want to book exists in the database (section 11.3).

11.1 Validating input parameters of a service layer method

11.1.1 Runtime type checks with the is and as operators

11.1.2 Type checking with the is operator

11.1.3 Type checking with the as operator

11.1.4 What did we do in section 11.1?

11.2 Cleaning up the BookingServiceTests class

11.3 Foreign key constraints in service classes

Exercises