🏛️ OOP trong NestJS (Phần 5): SOLID Principles
🎯 Mục tiêu: Hiểu 5 nguyên tắc SOLID và áp dụng vào code NestJS thực tế.
📌 Recap từ Series
| Phần | Đã học | UserService evolution |
|---|---|---|
| 1 | Class, this, Constructor | Functional → Class |
| 2 | Encapsulation, Decorators | @Injectable, private |
| 3 | Interface, Abstract Class | IUserRepository |
| 4 | DI, IoC Container | Inject IUserRepository |
Bây giờ: Học các nguyên tắc thiết kế để code NestJS bền vững, dễ maintain.
1. SOLID LÀ GÌ?
S - Single Responsibility Principle (SRP)
O - Open/Closed Principle (OCP)
L - Liskov Substitution Principle (LSP)
I - Interface Segregation Principle (ISP)
D - Dependency Inversion Principle (DIP)
💡 Đây là 5 nguyên tắc giúp code flexible, maintainable, testable.
2. S - SINGLE RESPONSIBILITY PRINCIPLE
2.1 Định nghĩa
Một class chỉ nên có MỘT lý do để thay đổi.