Dependency Inversion Principle (DIP)
Definition High-level modules should not depend on low-level modules. Both should depend on abstractions. 👉 Depend on interfaces/abstractions, not concrete classes. Purpose Reduce tight coupling Improve flexibility Enable dependency injection Make unit testing easier Build scalable architecture Bad Example ❌ Low-Level Class public class SqlRepository { public void Save() { Console.WriteLine("Saved to SQL"); } } High-Level Class public class InvoiceService { private readonly SqlRepository _repository = new SqlRepository(); public void CreateInvoice() { _repository.Save(); } } Problem InvoiceService directly depends on: SqlRepository If database changes: MongoDB PostgreSQL MySQL 👉 High-level code must change. This creates: Tight coupling Difficult testing Poor scalability Good Example ✅ Step 1: Abstraction public interface IReposi...