Every Angular developer loves the dependency injection system the ng core provides. DI is a intelligent way to manage all the applications classes and therefore it is definitly worth learning.In this article I want to demonstrate a way to implement easy-to-understand and high usability (developers point of view) object-oriented design with TypeScript in NodeJS. This is not a TypeScript basics article and expects at least a little TS programming experience.
What is dependency injection?
Depenency injection is a principle that intelligently manages your applications classes and prevents redundant instatiations. A DI system usually implements containers which store class definitions and instances. Requesting a class instance results in a lookup of these containers and, if not exist, it creates and returns the instance or otherwise returns the existing instance.In a hierarchical dependency injection the child containers are aware of the parents. If a class is requested to a child container, the DI system checks the container, and, if not recognized, it looks in the parents container. This is how you can share data and logic between multiple modules with just a single class instance.
Setting up a new project
Create a new directory, e.g. diproject, which will be the root ./ of the application, and run To get started create a new file called ./tsconfig.json, and enable the experimentalDecorators flag. It could look like
Building the application
At first we create a root ApplicationModule in a file thats located in ./src/application.module.ts. This is the module on top of the dependency injection hierarchy. Create an ./src/index.ts file with the following content. This will be the applications entrypoint. It simply imports the ApplictionModule and creates an instance.As you might have seen, we import the DatabaseModule and the Http2Module in our ApplicationModule. These modules are treated as children of the root module. Means that all provided and declared classes in the ApplicationModule are also available in the child modules but not the other way around.Let's create the children. Starting with the DatabaseModule in ./src/modules/database/database.module.ts. We declare a CreateDatabaseComponent and provide a DatabaseService. The DatabaseService will be available with a single instance in all declared classes of the DatabaseModule.And the Http2Module in ./src/modules/http2/http2.module.ts.Similar to the database module the PrepareRoutesComponent in ./src/modules/http2/components/prepare-routes.ts is capable of the RequestHandlerService. It can be injected as follows.But what if we need the databaseService when preparing routes? Usually one needs to create a DatabaseService instance in order to get its feature again. But using DI is the more intelligent approach. It is as easy as exporting the service so that it is stored in the ApplicationModule container.Now it is possible to use the instance in the DatabaseModule and in the Http2Module. Finally your projects structure should be similar to
Hopefully you have got an idea of how useful DI really is, even on server-side applications. It massively increases your codes reusability and prevents redundant class instances.