9 Extension methods, streams, and abstract classes

 

This chapter covers

  • Using streams to redirect console output
  • Using abstract classes to provide common functionality across derived classes
  • Using the AddRange LINQ method to add many things to a collection at once
  • Using the SortedList collection
  • Using extension methods to extend existing types with new functionality
  • Refactoring “magic numbers”

In sections 3.1 and 3.2, the CEO of Flying Dutchman Airlines tasked us with creating a new version of the existing FlyingDutchmanAirlines codebase. The existing codebase is old and riddled with design flaws, and does not play nice with the new API requirements put in place by a newly signed business deal with a search aggregator. In chapters 3 and 4, we considered the existing codebase and earmarked potential improvements. In chapter 5, we started our refactor and implemented a database access layer with Entity Framework Core. Following that, in chapters 5 through 8 we implemented (and tested) two repositories out of the following four required classes:

  • CustomerRepository—We implemented this repository class in chapters 6 and 7.
  • BookingRepository—We implemented this repository class in chapter 8.
  • AirportRepository—We implement this repository class in this chapter.
  • FlightRepository—We implement this repository class in this chapter.

See figure 9.1 for where we are in the scheme of the book.

9.1 Implementing the Airport repository

9.2 Getting an Airport out of the database by its ID

9.3 Validating the AirportID input parameter

9.4 Output streams and being specifically abstract

9.5 Querying the database for an Airport object

9.6 Implementing the Flight repository

9.6.1 The IsPositive extension method and “magic numbers”

9.6.2 Getting a flight out of the database