Saturday, November 8, 2025

Dotnet Core - DI(Dependency Injection) vs DIP(Dependency Inversion Principle)

 ðŸ§© The “D” in SOLID — Dependency Inversion Principle (DIP)

Dependency Inversion Principle (DIP) is the design principle behind the idea of Dependency Injection.




🧠 What DIP States

High-level modules should not depend on low-level modules. Both should depend on abstractions.
Abstractions should not depend on details. Details should depend on abstractions.

In simpler words:

  • Instead of classes depending on concrete implementations, they should depend on interfaces or abstract classes.
  • This makes the system more flexible and loosely coupled.


🧱 Example Without DIP (Bad Design)

public class NotificationService { private EmailService _emailService = new EmailService(); public void Notify(string message) { _emailService.SendEmail(message); } } public class EmailService { public void SendEmail(string message) { Console.WriteLine($"Email sent: {message}"); } }

Problem:

  • NotificationService depends directly on a concrete EmailService.
  • If you want to switch to SmsService, you must modify NotificationService.


✅ Example With DIP (Good Design)

public interface IMessageService { void Send(string message); } public class EmailService : IMessageService { public void Send(string message) => Console.WriteLine($"Email: {message}"); } public class SmsService : IMessageService { public void Send(string message) => Console.WriteLine($"SMS: {message}"); } public class NotificationService { private readonly IMessageService _messageService; public NotificationService(IMessageService messageService) { _messageService = messageService; } public void Notify(string message) { _messageService.Send(message); } }

Now:

  • NotificationService depends on the abstraction IMessageService.
  • You can easily switch between implementations (Email, SMS, Push).

✅ This is Dependency Inversion Principle in action.


🧩 How Dependency Injection (DI) Fits In

Now that we understand DIP as a principle, here’s where DI comes in:

ConceptWhat it is
DIP (Principle)A guideline that says “depend on abstractions, not concretions.”
DI (Pattern)A technique or mechanism to implement DIP in code.

🔄 Relationship Between DIP and DI

Let’s visualize it:

DIP → is the principle (what you should do) DI → is the method (how you do it)

So:

  • DIP is about how classes should relate to each other (architectural rule).
  • DI is about how to supply those dependencies (technical mechanism).


🧰 Example: Applying DI to Implement DIP

In .NET Core:

var builder = WebApplication.CreateBuilder(args); builder.Services.AddScoped<IMessageService, EmailService>(); builder.Services.AddScoped<NotificationService>(); var app = builder.Build();

  • Here, DI container ensures NotificationService receives an implementation of IMessageService.
  • That’s Dependency Injection in practice.
  • And since both classes depend on abstractions, that’s Dependency Inversion Principle applied.

🧠 Summary Comparison

AspectDependency Inversion Principle (DIP)Dependency Injection (DI)
TypePrinciple (part of SOLID)Design Pattern / Technique
PurposeTo reduce coupling between high-level and low-level modulesTo supply dependencies to classes at runtime
FocusArchitecture & relationships between classesImplementation & object creation
Enforced ByDeveloper’s designFramework or DI container
In .NET CoreInterfaces like IService, IRepositoryAddScoped, AddSingleton, AddTransient

💬 Analogy

ConceptAnalogy
DIP“Don’t depend on specific brands; depend on a plug standard.”
DI“Use a socket that provides power to any plug that fits.”

🧾 Quick Recap

✅ DIP (Dependency Inversion Principle) = design principle (the “D” in SOLID)
✅ DI (Dependency Injection) = pattern / implementation of that principle
💡 DIP gives the rule, DI provides the tool to achieve it

No comments:

Post a Comment

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...