Dependency Injection via Constructor Injection

The most common way of implementing Dependency Injection (DI) is to use Constructor Injection with classes. Let’s suppose we have a Customer class defined as follows:

We can create a service to fetch customer records and define an interface for this service:

An actual implementation of the DataService class could look like this:

Obviously the real thing would fetch the real records from somewhere, e.g. a database. When we fetch these records we’d like to put them in a Customer repository. Whenever we create an instance of the CustomerRepository class we’ll pass in the dependency to the…
Read more →