Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 26, 2021 02:44 pm GMT

Wiring Services in Frameworks And How it Works Under the Hood

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 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

Two Services Depends On Each Other

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

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);

References

Nodejs Design Pattern Book

Refactoring Guru Design Patterns


Original Link: https://dev.to/eslamelkholy/wiring-services-how-it-works-in-frameworks-and-look-under-the-hood-54nj

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To