Chapter 5. Make It Simple Again — Inversion of Control

In Chapter 3, you read about the principles of Dependency Inversion and Dependency Injection, and learned how objects collaborating together in modules can be built in a loosely coupled fashion by inverting the relationship of a dependent to its dependencies. However after moving the responsibility of creating the dependencies out of the service modules, you have been left with the problem of where and when the concrete implementations are created.

Creating Dependencies

To demonstrate the problems of constructing objects with dependencies, let's consider a simple example of a service that requires two low-level detail objects.

The TouristInformationService is a service that pinpoints places of interest on a map. The service requires a map and a dictionary of places of interest. The service has a single method, FindInterestingPlacesNear, which takes a location object and returns a map filled with places of interest. The service has been designed in a loosely coupled fashion, via a Test Driven Design methodology and you have been very strict about not to tying yourself down to any concrete implementation. Figure 5-1 shows the class diagram of the TouristInformationService.

Figure 5-1

Figure 5-1. Figure 5-1

You will be using this sample code to demonstrate the concepts of Inversion of Control, so let's first build the sample.

  1. Create a new blank solution ...

Get Professional Enterprise .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.