{"id":113,"date":"2026-06-29T10:34:20","date_gmt":"2026-06-29T10:34:20","guid":{"rendered":"https:\/\/blog.vigplanet.com\/?p=113"},"modified":"2026-06-29T10:35:41","modified_gmt":"2026-06-29T10:35:41","slug":"dependency-injection-in-net-explained-in-simple-practical-way-with-examples","status":"publish","type":"post","link":"https:\/\/blog.vigplanet.com\/?p=113","title":{"rendered":"Dependency Injection in .NET \u2013 Explained in Simple &amp; Practical Way (With Examples)"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Dependency Injection (DI) is one of the most important concepts in modern .NET development, especially when building scalable applications like ASP.NET Core APIs, Microservices, or Enterprise applications.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Dependency Injection?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Dependency Injection is a design pattern in which an object receives its dependencies from outside rather than creating them itself.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In simple words:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u274c Without DI \u2192 Class creates its own dependency<\/li>\n\n\n\n<li>\u2705 With DI \u2192 Dependency is provided to the class<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Why Do We Need Dependency Injection?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s understand with a real-life example.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Imagine:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A Car needs an Engine.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u274c Without DI<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Car\n{\n    private Engine _engine;\n\n    public Car()\n    {\n        _engine = new Engine();  \/\/ Tight coupling\n    }\n\n    public void Start()\n    {\n        _engine.Run();\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">C#<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Problems:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Tight coupling<\/li>\n\n\n\n<li>Hard to test<\/li>\n\n\n\n<li>Not flexible<\/li>\n\n\n\n<li>Difficult to replace Engine<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 With Dependency Injection<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Car\n{\n    private readonly IEngine _engine;\n\n    public Car(IEngine engine)\n    {\n        _engine = engine;\n    }\n\n    public void Start()\n    {\n        _engine.Run();\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">C#<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now the Engine is injected from outside.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This makes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Code loosely coupled<\/li>\n\n\n\n<li>Easy to test<\/li>\n\n\n\n<li>Easy to replace implementation<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Important Terms<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1\ufe0f\u20e3 Dependency<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A class that another class needs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<br>Car depends on Engine.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2\ufe0f\u20e3 Inversion of Control (IoC)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Instead of the class controlling dependencies, control is given to the container.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3\ufe0f\u20e3 IoC Container<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Framework component that manages dependencies.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In .NET \u2192 Built-in DI container<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Dependency Injection<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1\ufe0f\u20e3 Constructor Injection (Most Common)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Dependency is passed through the constructor.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class UserService\n{\n    private readonly IEmailService _emailService;\n\n    public UserService(IEmailService emailService)\n    {\n        _emailService = emailService;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">C#<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2714 Recommended approach<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2\ufe0f\u20e3 Property Injection<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public IEmailService EmailService { get; set; }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">C#<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Less used in .NET Core.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3\ufe0f\u20e3 Method Injection<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public void SendEmail(IEmailService emailService)\n{\n    emailService.Send();\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">C#<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Dependency Injection in ASP.NET Core (Real Example)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s create a simple example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Create Interface<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public interface IMessageService\n{\n    string GetMessage();\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">C#<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Create Implementation<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public class MessageService : IMessageService\n{\n    public string GetMessage()\n    {\n        return \"Hello from Dependency Injection!\";\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">C#<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Register Service in Program.cs<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>builder.Services.AddScoped&lt;IMessageService, MessageService&gt;();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">C#<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Inject in Controller<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public class HomeController : Controller\n{\n    private readonly IMessageService _messageService;\n\n    public HomeController(IMessageService messageService)\n    {\n        _messageService = messageService;\n    }\n\n    public IActionResult Index()\n    {\n        var message = _messageService.GetMessage();\n        return Content(message);\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">C#<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now .NET automatically injects MessageService into HomeController.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Service Lifetimes in .NET<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When registering services, you must define the lifetime.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1\ufe0f\u20e3 Transient<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>services.AddTransient&lt;IService, Service&gt;();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">C#<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>New instance every time<\/li>\n\n\n\n<li>Good for lightweight services<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2\ufe0f\u20e3 Scoped<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>services.AddScoped&lt;IService, Service&gt;();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">C#<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>One instance per HTTP request<\/li>\n\n\n\n<li>Most commonly used in web apps<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3\ufe0f\u20e3 Singleton<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>services.AddSingleton&lt;IService, Service&gt;();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">C#<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>One instance for the entire application lifetime<\/li>\n\n\n\n<li>Good for caching and configuration<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Why DI is Important for Testing?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Without DI: You cannot mock dependencies.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With DI: You can easily use Moq or fake services.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var mockService = new Mock&lt;IMessageService&gt;();\nmockService.Setup(x =&gt; x.GetMessage()).Returns(\"Test\");\n\nvar controller = new HomeController(mockService.Object);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">C#<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now testing becomes easy.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Real-World Usage in Microservices<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In microservices, DI is used for:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Repository pattern<\/li>\n\n\n\n<li>Database context<\/li>\n\n\n\n<li>Logging<\/li>\n\n\n\n<li>API services<\/li>\n\n\n\n<li>External services<\/li>\n\n\n\n<li>Authentication services<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Without DI \u2192 Microservices architecture becomes messy.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Common Mistakes<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u274c Injecting too many services in one class<\/li>\n\n\n\n<li>\u274c Using Singleton for DbContext<\/li>\n\n\n\n<li>\u274c Not using interfaces<\/li>\n\n\n\n<li>\u274c Creating services manually with the new keyword<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practices<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u2714 Use Constructor Injection<\/li>\n\n\n\n<li>\u2714 Use Interfaces<\/li>\n\n\n\n<li>\u2714 Keep services small<\/li>\n\n\n\n<li>\u2714 Use Scoped for DbContext<\/li>\n\n\n\n<li>\u2714 Avoid service locator pattern<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Real World Example Structure<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Controllers<\/li>\n\n\n\n<li>Services<\/li>\n\n\n\n<li>Repositories<\/li>\n\n\n\n<li>Interfaces<\/li>\n\n\n\n<li>Models<\/li>\n\n\n\n<li>Program.cs<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Register all services in one place.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Benefits of Dependency Injection<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Loose Coupling<\/li>\n\n\n\n<li>Easy Unit Testing<\/li>\n\n\n\n<li>Better Maintainability<\/li>\n\n\n\n<li>Flexible Architecture<\/li>\n\n\n\n<li>Scalable Applications<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Final Summary<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Dependency Injection is not just a feature\u2014it is a foundation of modern .NET architecture.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you are building:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>APIs<\/li>\n\n\n\n<li>Microservices<\/li>\n\n\n\n<li>Enterprise Applications<\/li>\n\n\n\n<li>Clean Architecture Projects<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Then DI is mandatory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Dependency Injection is a core design pattern in modern .NET development that promotes loose coupling, testability, maintainability, and scalability. By using constructor injection, proper service lifetimes, and the built-in .NET DI container, developers can build clean, modular, and enterprise-ready applications while avoiding tight coupling and architectural complexity.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">People also reading<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Mastering IoC Containers: Beyond the Built-in .NET Options<\/li>\n\n\n\n<li>Dependency Injection in gRPC Services: A Practical Guide<\/li>\n\n\n\n<li>Advanced Mocking Techniques with DI: Beyond the Basics<\/li>\n\n\n\n<li>Clean Architecture &amp; Dependency Injection: A Deep Dive<\/li>\n\n\n\n<li>DI Anti-Patterns: Identifying and Avoiding Common Mistakes<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Dependency Injection (DI) is one of the most important concepts in modern .NET development, especially when building scalable applications like ASP.NET Core APIs, Microservices, or Enterprise applications. What is Dependency Injection? Dependency Injection is a design pattern in which an object receives its dependencies from outside rather than creating them itself. In simple words:<\/p>\n","protected":false},"author":1,"featured_media":115,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-113","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.vigplanet.com\/index.php?rest_route=\/wp\/v2\/posts\/113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.vigplanet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.vigplanet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.vigplanet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.vigplanet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=113"}],"version-history":[{"count":1,"href":"https:\/\/blog.vigplanet.com\/index.php?rest_route=\/wp\/v2\/posts\/113\/revisions"}],"predecessor-version":[{"id":114,"href":"https:\/\/blog.vigplanet.com\/index.php?rest_route=\/wp\/v2\/posts\/113\/revisions\/114"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.vigplanet.com\/index.php?rest_route=\/wp\/v2\/media\/115"}],"wp:attachment":[{"href":"https:\/\/blog.vigplanet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.vigplanet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.vigplanet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}