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
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
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?
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
Simple Code Snippet Using Javascript
class BlogService {
constructor() {}
}
module.exports = new UserService();
import BlogService from './blogService';
const userService = new UserService(BlogService);
Top comments (0)