Exploring NestJS's dependency injection system sparked a deeper dive into Dependency Inversion, Inversion of Control, and Dependency Injection. These concepts, while seemingly similar, offer distinct solutions to different problems. This explanation serves as a personal refresher, and hopefully, a helpful guide for others grappling with these terms.
Definition: High-level modules shouldn't depend on low-level modules; both should depend on abstractions. Abstractions shouldn't depend on details; details should depend on abstractions.
In software, high-level modules encapsulate core business logic, while low-level modules handle specific implementations (databases, APIs, etc.). Without DIP, high-level modules directly rely on low-level ones, creating tight coupling that hinders flexibility, complicates testing and maintenance, and makes replacing or extending low-level details difficult.
DIP reverses this relationship. Instead of direct control, both high-level and low-level modules depend on a shared abstraction (interface or abstract class).
<code class="language-python">class EmailService: def send_email(self, message): print(f"Sending email: {message}") class Notification: def __init__(self): self.email_service = EmailService() def notify(self, message): self.email_service.send_email(message)</code>
<code class="language-typescript">class EmailService { sendEmail(message: string): void { console.log(`Sending email: ${message}`); } } class Notification { private emailService: EmailService; constructor() { this.emailService = new EmailService(); } notify(message: string): void { this.emailService.sendEmail(message); } }</code>
Problems:
Notification
directly depends on EmailService
.SMSService
requires modifying Notification
.<code class="language-python">from abc import ABC, abstractmethod class MessageService(ABC): @abstractmethod def send_message(self, message): pass class EmailService(MessageService): def send_message(self, message): print(f"Sending email: {message}") class Notification: def __init__(self, message_service: MessageService): self.message_service = message_service def notify(self, message): self.message_service.send_message(message) # Usage email_service = EmailService() notification = Notification(email_service) notification.notify("Hello, Dependency Inversion!")</code>
<code class="language-typescript">interface MessageService { sendMessage(message: string): void; } class EmailService implements MessageService { sendMessage(message: string): void { console.log(`Sending email: ${message}`); } } class Notification { private messageService: MessageService; constructor(messageService: MessageService) { this.messageService = messageService; } notify(message: string): void { this.messageService.sendMessage(message); } } // Usage const emailService = new EmailService(); const notification = new Notification(emailService); notification.notify("Hello, Dependency Inversion!");</code>
IoC is a design principle where dependency control shifts to an external system (framework) instead of being managed within the class. Traditionally, a class creates and manages its dependencies. IoC reverses this—an external entity injects dependencies.
<code class="language-python">class SMSService: def send_message(self, message): print(f"Sending SMS: {message}") class Notification: def __init__(self): self.sms_service = SMSService() # Dependency created internally def notify(self, message): self.sms_service.send_message(message)</code>
<code class="language-typescript">class SMSService { sendMessage(message: string): void { console.log(`Sending SMS: ${message}`); } } class Notification { private smsService: SMSService; constructor() { this.smsService = new SMSService(); // Dependency created internally } notify(message: string): void { this.smsService.sendMessage(message); } }</code>
Problems Without IoC:
<code class="language-python">class EmailService: def send_email(self, message): print(f"Sending email: {message}") class Notification: def __init__(self): self.email_service = EmailService() def notify(self, message): self.email_service.send_email(message)</code>
<code class="language-typescript">class EmailService { sendEmail(message: string): void { console.log(`Sending email: ${message}`); } } class Notification { private emailService: EmailService; constructor() { this.emailService = new EmailService(); } notify(message: string): void { this.emailService.sendEmail(message); } }</code>
DI is a technique where an object receives its dependencies from an external source. It's a practical implementation of IoC, injecting dependencies via:
injector
library)<code class="language-python">from abc import ABC, abstractmethod class MessageService(ABC): @abstractmethod def send_message(self, message): pass class EmailService(MessageService): def send_message(self, message): print(f"Sending email: {message}") class Notification: def __init__(self, message_service: MessageService): self.message_service = message_service def notify(self, message): self.message_service.send_message(message) # Usage email_service = EmailService() notification = Notification(email_service) notification.notify("Hello, Dependency Inversion!")</code>
tsyringe
library)<code class="language-typescript">interface MessageService { sendMessage(message: string): void; } class EmailService implements MessageService { sendMessage(message: string): void { console.log(`Sending email: ${message}`); } } class Notification { private messageService: MessageService; constructor(messageService: MessageService) { this.messageService = messageService; } notify(message: string): void { this.messageService.sendMessage(message); } } // Usage const emailService = new EmailService(); const notification = new Notification(emailService); notification.notify("Hello, Dependency Inversion!");</code>
This detailed explanation clarifies the relationships and distinctions between DIP, IoC, and DI, emphasizing their individual contributions to building robust and maintainable software.
The above is the detailed content of Breaking Down Dependency Inversion, IoC, and DI. For more information, please follow other related articles on the PHP Chinese website!