Dependency Injection

Ahmed Youssef
5 min readMar 14, 2020

--

Before you learn about Dependency Injection (DI), let me show the problem that we have.

let’s create an effortless project where a client needs to save a car in our database and because this is a simple project we decided to use JDBC strategy

to connect with the DB,

UML diagram, we are following the best practice regarding the application structure using the separation of concern tiers.

The client communicates to a CarService(intermediate object ) which delivers to the CarRepository which talk to the DB

so the client does not interact directly with the DB

Application Tier

Domain Tier

Car.java
GitHub Gist

@Data
@AllArgsConstructor
@NoArgsConstructor

I’m using project Lombok to generate all the boilerplate

Client Tier

Client.java
GitHub Gist

Service Tier

CarService.java
GitHub Gist

Repository Tier

CarRepositoryJdbc.java
GitHub Gist

so far our application run and we could save the car Object to DB

We notice that the JDBC strategy to connect to DB is inferior, so we decided to use this new strategy called Hibernate. and because of Hibernate is a new strategy we decided to test it first

Challenge

“We decided to use a Different DAO strategy such as Hibernate”

HibernateUtil.java
GitHub Gist

CarRepositoryJpa.java
GitHub Gist

Let’s use the new strategy so our implementation will be

GitHub Gist

The problem.
When we updated our strategy, we must update the CarService class, which indicates that we have coupling
between Two different Tiers.

“you may think it’s fine but think about how many lines of code you need to change in a real application”

Coupling is when a class depend on another class unnecessary, change to one class resulted in changes to the other

We need coupling because, without coupling, applications are going to be disconnected, and if all our classes are connected, they are not that effective, plus there is a coupling that we can not control. It is in almost every domain class.

for example:

“imagen that Java Decided to rename the BigDecimal class or the String class to something else can you imagen how many lines of code we have to rewrite”

the coupling in the car class is out of our control, but let’s say we decided to create an Engine for each Car; this is coupling as well.

— BUT THIS IS NOT THE COUPLING WE NEED TO REDUC —

“the coupling that we need to reduce as much as possible is the coupling between tiers”

As you can see, CarService is tied to CarRepository. This is the kind of couple that causes the problem when we tried to change the strategy or even if we try to test.

Reduce coupling in 3 steps.

to reduce coupling we need to follow a 3 step. Those 3 steps can not be implemented separately the must be implemented together.

Step 1: Programming to Interface

creating interface

implement interface

or:

using programming to interface principle

notice the new is always a sign of coupling which lead us to our second step

Step 2: Dependency Injection

Dependency Injection is nothing but passing the collaborators as a parameter instead of hard coding them

Pass the collaborators as a parameter

updating client

Now if we decided to change our strategy it’s going to be a minor change

on the line

so you see Dependency Injection is just a simple Java

let’s assume that we forget to update our client so our client turn to be like that

now if we run our application we get

in a true application, this is a bad, not reliable way. to improve it we need to go to our next step

Step 3: Centriliezed Configuration

In this step what we are going to do is to capture all our dependencies in one place. thin we are going to use a “Container” to create and configure our Object. this is where the Spring framework will help us later. BUT NOT NOW

for now, let’s just create a simple container using pure Java this container will be our centralized config file

in this class, we are going to do the following

creating the Repository

creating the services

Connecting the Repository and Service together

The final look of our container is:

updating the client

all we have to do now is to use the container in the client

as you, the client now is smaller and does not have to construct the service he just ask the container for it.

In the end, you will not have to create a container by your self you will use a framework like Spring framework

Project Link

--

--