Make Your Life Easy With Dependency Injection in Your TypeScript Projects
By: İbrahim Gündüz
Tags: Design Patterns, Typescript, Software Engineering, JavaScript

Because libraries solve some problems in a specific area and allow making something easier, we use tons of libraries/frameworks and internal project components written by us… in order to make an application easier. However, this situation brings other complexities that we need to take care of, like relations between the components. In this article, I would like to mention how to use the technique as known as Dependency Injection in a TypeScript project.

What Is Dependency Injection?

In an application, If we define all the other classes used by a class as its dependencies; Dependency Injection is a technique in which a class receives its dependencies.

Real-Life Example:

As you could see in the code below, we have two classes called PaymentService and OrderService . Because these classes are strongly coupled, and it’s not easily possible to mock PaymentService class, you can’t test OrderService class’s behavior properly.

If we look at OrderService class once again, we can easily see the code that blocks testing the class by creating a new instance of PaymentService inside createOrder method. So, let’s start by that part in order to inject the PaymentService instance from the OrderService constructor method.

Because we move the PaymentService initialization process outside of the method, we can test OrderService easily by mocking PaymentService class, and we can inject the same instance to the other classes that it depends on also.

How To Use It In Real Life

Of course, we don’t always work with components that have only one single dependency in real-life. You may have components with a chain of dependencies.

So we should add a library to the project that responsible for managing dependencies in order to get rid of those complex object creation processes.

TypeDI

TypeDI is a dependency injection tool that allows you to build well-structured projects with dependency injection technique. In this article, I’ll try to explain some use cases as simple as possible but you can check also the library documentation for more details.

How To Setup

Install typedi and reflect-metadata packages.

$ npm install typedi reflect-metadata

Because TypeDI performs the injections through decorators, you must make the following changes in your tsconfig.json file under CompilerOptions .

// file: tsconfig.json {

//...

"compilerOptions": {

//...

"emitDecoratorMetadata": true,

"experimentalDecorators": true,

//...

}

//...

}

And finally, import reflect-metadata library at the first line of your application. For example, the file where you start the server process in a web project works on Express framework.

How To Use

Let’s take a look at the following code as a simple example.

As you can see, TypeDI injects an instance of PaymentService to the OrderService class with its dependencies automatically.

In another example, you can also set and inject dependencies by a key. It’s quite a useful feature if you want to inject some settings.

TypeDI makes your job easy with other unique features also. I strongly recommend you to check the library document.

Enjoy!

Credits