πŸ› ️ Understanding Dependency Injection in C#: A Simple Guide for Beginners

 

πŸ€” What is Dependency Injection?

Let’s break it down with an everyday example.

🎯 Real-World Scenario: Ordering Pizza πŸ•

Imagine you love pizza, and every time you want one, you go to a specific pizza shop (hardcoded dependency).


public class Person { private PizzaShop _pizzaShop = new PizzaShop(); // Direct dependency public void OrderPizza() { _pizzaShop.MakePizza(); } }

πŸ›‘ Problem:

  • You are tied to one pizza shop (class PizzaShop).
  • What if you want to order from a different pizza shop?
  • What if the pizza shop closes?

Solution: Dependency Injection (DI)

Instead of hardcoding the pizza shop, let's allow any pizza provider to be used.

public interface IPizzaProvider
{ void MakePizza(); } public class DominoPizza : IPizzaProvider { public void MakePizza() => Console.WriteLine("Making Pizza from Domino's..."); } public class Person { private readonly IPizzaProvider _pizzaProvider; public Person(IPizzaProvider pizzaProvider) { _pizzaProvider = pizzaProvider; // Dependency is injected } public void OrderPizza() { _pizzaProvider.MakePizza(); } }

Now, we can easily switch pizza providers:

Person person = new Person(new DominoPizza());
person.OrderPizza();

πŸŽ‰ Now, the person can order from any pizza provider without changing their code!


πŸ€” Why Use Dependency Injection?

Flexibility – Easily switch between different implementations.
Testability – Makes unit testing easy (mock dependencies).
Maintainability – No need to modify code when adding new services.


πŸ”§ Using Dependency Injection in C# with .NET Core

C# provides built-in dependency injection with the IServiceCollection.

Step 1: Define Interfaces & Implementations


public interface IPizzaProvider { void MakePizza(); } public class DominoPizza : IPizzaProvider { public void MakePizza() => Console.WriteLine("Making Pizza from Domino's..."); } public class PizzaHut : IPizzaProvider { public void MakePizza() => Console.WriteLine("Making Pizza from Pizza Hut..."); }

Step 2: Register Dependencies in DI Container


var services = new ServiceCollection(); services.AddTransient<IPizzaProvider, DominoPizza>(); var serviceProvider = services.BuildServiceProvider();

Step 3: Inject Dependency and Use the Service


var pizzaProvider = serviceProvider.GetService<IPizzaProvider>(); pizzaProvider.MakePizza(); // Outputs: "Making Pizza from Domino's..."

🎯 Final Thoughts

Dependency Injection is like ordering food from any restaurant instead of being stuck with one. πŸ•πŸ”

  • It makes code more modular, testable, and maintainable.
  • It is widely used in ASP.NET Core, Web APIs, and microservices.

πŸ’‘ What do you think? Have you used DI in your projects? Let me know in the comments! 😊

Comments

Popular posts from this blog

πŸ€– Copilot vs Microsoft Copilot vs Copilot Studio: What’s the Difference?

Understanding Auto-Numbering in a Multi-Transaction System

Integrating Dynamics 365 CRM with MuleSoft Using a Synchronous C# Plugin