Wiring Services in Frameworks And How it Works Under the Hood
Tags: architecture, oop, design
Published: 2021-06-26 14:44:57

The Most Important Question i have asked myself when i have started learning Clean Architecture like DDD (Domain Driven Development) and Separating Business Logic in Services What is the Best Practices Should i follow To Make the Communication Between Services Better?
Today we will discuss alot of Things Related to The Communication between Classes And How it Works in Most Of Frameworks Like Nestjs, Angular and Laravel,....
 
First Of All You Must Read this Quote Twice
>Every App is The Result of Aggregation of Several Components and App Grows, the Way we connect these Components Becomses Win Or Lose Factor For the Maintainability and Success Of The Project
[This Great Quote From Nodejs Design Patterns Book And Most of Code Snippets Also](https://www.packtpub.com/product/node-js-design-patterns-third-edition/9781839214110)

### Now Let's Start
we Have a 1-Blog Service and 2-User Service
we want To Use Blog Service in User Service because User Service Depends on Blog Service 

![Two Services Depends On Each Other](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6579jkr2azisek5rab4z.jpg)

But if We Have More Service Like Friends Service and User Service Depends on Both So We Going to Create a new Object in every New One?

![Three Services Connected With Each Other](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ogar003bwgb6ltwmz6kw.jpg)

The First Thing Comes in your Mind is to Make It as **Singleton** 
Singleton Object And Uses it Every where it's an ideal Solution for Wiring Modules But This Solution Still Have a bottlenecks That Can Easily Solved By 
### Dependency Injection
* Mock Our Services 
* Improved Decoupling Between Modules
* Let's Say i will change the whole Object Also
* Understand the Relationship Between Components

### Dependency Injection + Singleton 
it's The Ideal Solution For Wiring Modules Together and how we can easily Control the flow And Dependency Tree With These To That Exactly The Same what IOC(Inversion of Control) Containers Do For Wiring The Modules Together Uses 

**Dependency Injection + Singleton** 
Makes the Communication between these Modules Easily And helps us To Solve Many Problems Related to Wiring Services Together

that's why most of Frameworks Already Using IOC Containers To Shift Responsibility Of Wiring Modules To Third Party Package and how it Actually Works Under the hood and Injects Services Easily With Each other

#### Nestjs Framework
Nestjs Framework Also Using Dependency Injection + Singleton
For The Communication Between Services They Already Mentioned 
and Describe how exactly it Works In The Documentation
[Nestjs Dependency Injection and Singleton](https://docs.nestjs.com/providers#dependency-injection)

#### Simple Code Snippet Using Javascript
```javascript
class BlogService {
  constructor() {}
}
module.exports = new UserService();
```
```javascript
import BlogService from './blogService';
const userService = new UserService(BlogService);
```

#### References
[Nodejs Design Pattern Book](https://www.packtpub.com/product/node-js-design-patterns-third-edition/9781839214110)

[Refactoring Guru Design Patterns](https://refactoring.guru/design-patterns)