{"id":926,"date":"2025-04-09T19:18:19","date_gmt":"2025-04-09T18:18:19","guid":{"rendered":"https:\/\/debuggersspace.com\/?p=926"},"modified":"2025-04-09T19:27:15","modified_gmt":"2025-04-09T18:27:15","slug":"understanding-dependency-injection-and-ioc-in-net-core-a-beginner-friendly-guide-with-real-world-examples","status":"publish","type":"post","link":"https:\/\/debuggersspace.com\/index.php\/2025\/04\/09\/understanding-dependency-injection-and-ioc-in-net-core-a-beginner-friendly-guide-with-real-world-examples\/","title":{"rendered":"Understanding Dependency Injection and IoC in .NET Core \u2013 A Beginner-Friendly Guide with Real-World Examples"},"content":{"rendered":"<div class='booster-block booster-read-block'>\n                <div class=\"twp-read-time\">\n                \t<i class=\"booster-icon twp-clock\"><\/i> <span>Read Time:<\/span>4 Minute, 14 Second                <\/div>\n\n            <\/div><h2 class=\"\" data-start=\"253\" data-end=\"271\">\ud83d\udd0d Introduction<\/h2>\n<p class=\"\" data-start=\"273\" data-end=\"553\">As a new developer, you\u2019ve probably come across a situation where one class creates and controls the behavior of another class, making it tightly coupled and hard to test or extend. This is where <strong data-start=\"469\" data-end=\"498\">Dependency Injection (DI)<\/strong> and <strong data-start=\"503\" data-end=\"533\">Inversion of Control (IoC)<\/strong> come to the rescue.<\/p>\n<p class=\"\" data-start=\"555\" data-end=\"824\">In this blog, we\u2019ll break down these concepts in simple terms, walk through real-world examples, and implement them using <strong data-start=\"677\" data-end=\"690\">.NET Core<\/strong>, including Entity Framework Core registration in the DI container. We&#8217;ll also tackle common follow-up interview questions at the end.<\/p>\n<hr class=\"\" data-start=\"826\" data-end=\"829\" \/>\n<h2 class=\"\" data-start=\"831\" data-end=\"871\">\ud83e\udde0 What is Dependency Injection (DI)?<\/h2>\n<p class=\"\" data-start=\"873\" data-end=\"1109\"><strong data-start=\"873\" data-end=\"897\">Dependency Injection<\/strong> is a software design pattern that helps you achieve <strong data-start=\"950\" data-end=\"968\">loose coupling<\/strong> between classes. Instead of a class creating its own dependencies, they are provided from the outside, typically via constructor parameters.<\/p>\n<blockquote data-start=\"1111\" data-end=\"1217\">\n<p class=\"\" data-start=\"1113\" data-end=\"1217\">\ud83d\udd0c Think of DI as plugging in different components into a machine instead of soldering them permanently.<\/p>\n<\/blockquote>\n<hr class=\"\" data-start=\"1219\" data-end=\"1222\" \/>\n<h2 class=\"\" data-start=\"1224\" data-end=\"1265\">\ud83d\udd04 What is Inversion of Control (IoC)?<\/h2>\n<p class=\"\" data-start=\"1267\" data-end=\"1539\"><strong data-start=\"1267\" data-end=\"1291\">Inversion of Control<\/strong> is the broader concept behind Dependency Injection. It means handing over the control of object creation to a framework or container. In .NET Core, the <strong data-start=\"1444\" data-end=\"1470\">built-in IoC container<\/strong> handles object creation, lifetime management, and injection for you.<\/p>\n<blockquote data-start=\"1541\" data-end=\"1583\">\n<p class=\"\" data-start=\"1543\" data-end=\"1583\">\ud83e\udde0 IoC = \u201cDon\u2019t call me, I\u2019ll call you.\u201d<\/p>\n<\/blockquote>\n<hr class=\"\" data-start=\"1585\" data-end=\"1588\" \/>\n<h2 class=\"\" data-start=\"1590\" data-end=\"1638\">\u2699\ufe0f Types of Dependency Injection in .NET Core<\/h2>\n<ol data-start=\"1640\" data-end=\"1734\">\n<li class=\"\" data-start=\"1640\" data-end=\"1684\">\n<p class=\"\" data-start=\"1643\" data-end=\"1684\"><strong data-start=\"1643\" data-end=\"1668\">Constructor Injection<\/strong> \u2705 (Most common)<\/p>\n<\/li>\n<li class=\"\" data-start=\"1685\" data-end=\"1710\">\n<p class=\"\" data-start=\"1688\" data-end=\"1710\"><strong data-start=\"1688\" data-end=\"1710\">Property Injection<\/strong><\/p>\n<\/li>\n<li class=\"\" data-start=\"1711\" data-end=\"1734\">\n<p class=\"\" data-start=\"1714\" data-end=\"1734\"><strong data-start=\"1714\" data-end=\"1734\">Method Injection<\/strong><\/p>\n<\/li>\n<\/ol>\n<p class=\"\" data-start=\"1736\" data-end=\"1782\">We&#8217;ll focus on <strong data-start=\"1751\" data-end=\"1776\">Constructor Injection<\/strong> here.<\/p>\n<hr class=\"\" data-start=\"1784\" data-end=\"1787\" \/>\n<h2 class=\"\" data-start=\"1789\" data-end=\"1833\">\ud83d\udce6 Real-Time Example: Notification System<\/h2>\n<h3 class=\"\" data-start=\"1835\" data-end=\"1847\">Scenario<\/h3>\n<p class=\"\" data-start=\"1849\" data-end=\"1987\">We need a system to send email notifications. We\u2019ll inject an <code data-start=\"1911\" data-end=\"1937\">EmailNotificationService<\/code> into a controller rather than creating it inside.<\/p>\n<h3 class=\"\" data-start=\"1989\" data-end=\"2021\">Step 1: Define the Interface<\/h3>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre! language-csharp\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">interface<\/span> <span class=\"hljs-title\">INotificationService<\/span><br \/>\n{<br \/>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">void<\/span><\/span> <span class=\"hljs-title\">Send<\/span>(<span class=\"hljs-params\"><span class=\"hljs-built_in\">string<\/span><\/span> to, <span class=\"hljs-built_in\">string<\/span> message);<br \/>\n}<br \/>\n<\/code><\/div>\n<\/div>\n<hr class=\"\" data-start=\"2122\" data-end=\"2125\" \/>\n<h3 class=\"\" data-start=\"2127\" data-end=\"2160\">Step 2: Implement the Service<\/h3>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre! language-csharp\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">EmailNotificationService<\/span> : <span class=\"hljs-title\">INotificationService<\/span><br \/>\n{<br \/>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span><\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Send<\/span>(<span class=\"hljs-params\"><span class=\"hljs-built_in\">string<\/span><\/span> to, <span class=\"hljs-built_in\">string<\/span> message)<br \/>\n{<br \/>\nConsole.WriteLine(<span class=\"hljs-string\">$\"Email sent to <span class=\"hljs-subst\">{to}<\/span><\/span>: <span class=\"hljs-subst\">{message}<\/span>\");<br \/>\n}<br \/>\n}<br \/>\n<\/code><\/div>\n<\/div>\n<hr class=\"\" data-start=\"2363\" data-end=\"2366\" \/>\n<h3 class=\"\" data-start=\"2368\" data-end=\"2402\">Step 3: Inject into Controller<\/h3>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre! language-csharp\"><code class=\"whitespace-pre! language-csharp\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">NotificationController<\/span> : <span class=\"hljs-title\">ControllerBase<\/span><br \/>\n{<br \/>\n<span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">readonly<\/span> INotificationService _notificationService;<\/code><\/code><span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span><\/span> <span class=\"hljs-title\">NotificationController<\/span>(<span class=\"hljs-params\">INotificationService notificationService<\/span>)<br \/>\n{<br \/>\n_notificationService = notificationService;<br \/>\n}<code class=\"whitespace-pre! language-csharp\"><code class=\"whitespace-pre! language-csharp\"><\/code><\/code>[<span class=\"hljs-meta\">HttpPost<\/span>]\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span><\/span> IActionResult <span class=\"hljs-title\">NotifyUser<\/span>(<span class=\"hljs-params\"><span class=\"hljs-built_in\">string<\/span><\/span> email, <span class=\"hljs-built_in\">string<\/span> message)<br \/>\n{<br \/>\n_notificationService.Send(email, message);<br \/>\n<span class=\"hljs-keyword\">return<\/span> Ok(<span class=\"hljs-string\">&#8220;Notification sent!&#8221;<\/span>);<br \/>\n}<br \/>\n}<\/p>\n<\/div>\n<\/div>\n<hr class=\"\" data-start=\"2867\" data-end=\"2870\" \/>\n<h3 class=\"\" data-start=\"2872\" data-end=\"2908\">Step 4: Register in <code data-start=\"2896\" data-end=\"2908\">Program.cs<\/code><\/h3>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\">\n<p><code class=\"whitespace-pre! language-csharp\"><span class=\"hljs-keyword\">var<\/span> builder = WebApplication.CreateBuilder(args);<\/code><\/p>\n<p>builder.Services.AddScoped&lt;INotificationService, EmailNotificationService&gt;();<br \/>\n<span class=\"hljs-keyword\">var<\/span> app = builder.Build();<\/p>\n<\/div>\n<\/div>\n<hr class=\"\" data-start=\"3081\" data-end=\"3084\" \/>\n<h2 class=\"\" data-start=\"3086\" data-end=\"3141\">\ud83d\uddc3\ufe0f How Do You Register EF Core in the DI Container?<\/h2>\n<p class=\"\" data-start=\"3143\" data-end=\"3243\">Entity Framework Core\u2019s <code data-start=\"3167\" data-end=\"3178\">DbContext<\/code> is also a dependency and must be registered in the DI container:<\/p>\n<h3 class=\"\" data-start=\"3245\" data-end=\"3278\">Sample <code data-start=\"3256\" data-end=\"3278\">ApplicationDbContext<\/code><\/h3>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre! language-csharp\"><code class=\"whitespace-pre! language-csharp\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">ApplicationDbContext<\/span> : <span class=\"hljs-title\">DbContext<\/span><br \/>\n{<br \/>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span><\/span> <span class=\"hljs-title\">ApplicationDbContext<\/span>(<span class=\"hljs-params\">DbContextOptions&lt;ApplicationDbContext&gt; options<\/span>)<br \/>\n: <span class=\"hljs-title\">base<\/span>(<span class=\"hljs-params\">options<\/span>) { }<\/code><\/code><span class=\"hljs-keyword\">public<\/span> DbSet&lt;User&gt; Users { <span class=\"hljs-keyword\">get<\/span>; <span class=\"hljs-keyword\">set<\/span>; }<br \/>\n}<\/div>\n<\/div>\n<h3 class=\"\" data-start=\"3497\" data-end=\"3536\">Registering EF Core in <code data-start=\"3524\" data-end=\"3536\">Program.cs<\/code><\/h3>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre! language-csharp\">builder.Services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;<br \/>\noptions.UseSqlServer(builder.Configuration.GetConnectionString(<span class=\"hljs-string\">\"DefaultConnection\"<\/span>)));<br \/>\n<\/code><\/div>\n<\/div>\n<hr class=\"\" data-start=\"3707\" data-end=\"3710\" \/>\n<h2 class=\"\" data-start=\"3712\" data-end=\"3744\">\ud83e\uddea Unit Test Example with Moq<\/h2>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre! language-csharp\"><code class=\"whitespace-pre! language-csharp\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">NotificationControllerTests<\/span><br \/>\n{<br \/>\n[<span class=\"hljs-meta\">Fact<\/span>]\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span><\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">NotifyUser_SendsEmail_ReturnsOk<\/span>()<br \/>\n{<br \/>\n<span class=\"hljs-keyword\">var<\/span> mockService = <span class=\"hljs-keyword\">new<\/span> Mock&lt;INotificationService&gt;();<br \/>\n<span class=\"hljs-keyword\">var<\/span> controller = <span class=\"hljs-keyword\">new<\/span> NotificationController(mockService.Object);<\/code><\/code><span class=\"hljs-keyword\">var<\/span> result = controller.NotifyUser(<span class=\"hljs-string\">&#8220;test@example.com&#8221;<\/span>, <span class=\"hljs-string\">&#8220;Welcome!&#8221;<\/span>);<code class=\"whitespace-pre! language-csharp\"><code class=\"whitespace-pre! language-csharp\"><\/code><\/code>Assert.IsType&lt;OkObjectResult&gt;(result);<br \/>\n}<br \/>\n}<\/p>\n<\/div>\n<\/div>\n<hr class=\"\" data-start=\"4137\" data-end=\"4140\" \/>\n<h2 class=\"\" data-start=\"4142\" data-end=\"4187\">\ud83d\uddbc\ufe0f Visual Diagram \u2013 DI with IoC Container<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-927 alignnone\" src=\"https:\/\/debuggersspace.com\/wp-content\/uploads\/2025\/04\/ChatGPT-Image-Apr-9-2025-09_05_09-PM-e1744222811442-300x151.png\" alt=\"\" width=\"350\" height=\"176\" srcset=\"https:\/\/debuggersspace.com\/wp-content\/uploads\/2025\/04\/ChatGPT-Image-Apr-9-2025-09_05_09-PM-e1744222811442-300x151.png 300w, https:\/\/debuggersspace.com\/wp-content\/uploads\/2025\/04\/ChatGPT-Image-Apr-9-2025-09_05_09-PM-e1744222811442-768x388.png 768w, https:\/\/debuggersspace.com\/wp-content\/uploads\/2025\/04\/ChatGPT-Image-Apr-9-2025-09_05_09-PM-e1744222811442.png 1024w\" sizes=\"(max-width: 350px) 100vw, 350px\" \/><\/p>\n<hr class=\"\" data-start=\"4316\" data-end=\"4319\" \/>\n<h2 class=\"\" data-start=\"4321\" data-end=\"4366\">\u2705 Benefits of Dependency Injection and IoC<\/h2>\n<div class=\"pointer-events-none relative left-[50%] flex w-[100cqw] translate-x-[-50%] justify-center *:pointer-events-auto\">\n<div class=\"tableContainer horzScrollShadows\">\n<table class=\"min-w-full\" data-start=\"4368\" data-end=\"4776\">\n<thead data-start=\"4368\" data-end=\"4397\">\n<tr data-start=\"4368\" data-end=\"4397\">\n<th data-start=\"4368\" data-end=\"4386\">Feature<\/th>\n<th data-start=\"4386\" data-end=\"4397\">Benefit<\/th>\n<\/tr>\n<\/thead>\n<tbody data-start=\"4427\" data-end=\"4776\">\n<tr data-start=\"4427\" data-end=\"4507\">\n<td class=\"\" data-start=\"4427\" data-end=\"4447\">\ud83e\udde9 Loose Coupling<\/td>\n<td class=\"min-w-[calc(var(--thread-content-max-width)\/3)]\" data-start=\"4447\" data-end=\"4507\">Components depend on abstractions, not concrete classes.<\/td>\n<\/tr>\n<tr data-start=\"4508\" data-end=\"4575\">\n<td class=\"\" data-start=\"4508\" data-end=\"4527\">\ud83e\uddea Testability<\/td>\n<td class=\"min-w-[calc(var(--thread-content-max-width)\/3)]\" data-start=\"4527\" data-end=\"4575\">Easier to write unit tests with mocks\/fakes.<\/td>\n<\/tr>\n<tr data-start=\"4576\" data-end=\"4655\">\n<td class=\"\" data-start=\"4576\" data-end=\"4595\">\ud83d\udd01 Flexibility<\/td>\n<td class=\"min-w-[calc(var(--thread-content-max-width)\/3)]\" data-start=\"4595\" data-end=\"4655\">Swap implementations without changing dependent classes.<\/td>\n<\/tr>\n<tr data-start=\"4656\" data-end=\"4714\">\n<td class=\"\" data-start=\"4656\" data-end=\"4675\">\ud83e\uddfc Clean Code<\/td>\n<td class=\"\" data-start=\"4675\" data-end=\"4714\">Keeps classes focused and readable.<\/td>\n<\/tr>\n<tr data-start=\"4715\" data-end=\"4776\">\n<td class=\"\" data-start=\"4715\" data-end=\"4734\">\ud83d\udd0d Debuggability<\/td>\n<td class=\"\" data-start=\"4734\" data-end=\"4776\">Isolated testing simplifies debugging.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<hr class=\"\" data-start=\"4778\" data-end=\"4781\" \/>\n<h2 class=\"\" data-start=\"4783\" data-end=\"4827\">\u2753 Interview Follow-Up Questions &amp; Answers<\/h2>\n<h3 class=\"\" data-start=\"4829\" data-end=\"4856\">\ud83d\udd39 <strong data-start=\"4836\" data-end=\"4856\">Normal Questions<\/strong><\/h3>\n<p class=\"\" data-start=\"4858\" data-end=\"5055\"><strong data-start=\"4858\" data-end=\"4908\">Q1: What is the difference between IoC and DI?<\/strong><br data-start=\"4908\" data-end=\"4911\" \/>\ud83d\udc49 IoC is the principle of delegating control to a container. DI is one implementation of IoC, where dependencies are provided from the outside.<\/p>\n<p class=\"\" data-start=\"5057\" data-end=\"5239\"><strong data-start=\"5057\" data-end=\"5104\">Q2: Why is constructor injection preferred?<\/strong><br data-start=\"5104\" data-end=\"5107\" \/>\ud83d\udc49 It makes dependencies mandatory and explicit. It\u2019s also easier for testing and ensures the object is always initialized properly.<\/p>\n<p class=\"\" data-start=\"5241\" data-end=\"5299\"><strong data-start=\"5241\" data-end=\"5297\">Q3: What are the lifetimes of services in .NET Core?<\/strong><\/p>\n<ul data-start=\"5300\" data-end=\"5429\">\n<li class=\"\" data-start=\"5300\" data-end=\"5339\">\n<p class=\"\" data-start=\"5302\" data-end=\"5339\"><code data-start=\"5302\" data-end=\"5313\">Transient<\/code>: New instance every time.<\/p>\n<\/li>\n<li class=\"\" data-start=\"5340\" data-end=\"5377\">\n<p class=\"\" data-start=\"5342\" data-end=\"5377\"><code data-start=\"5342\" data-end=\"5350\">Scoped<\/code>: One instance per request.<\/p>\n<\/li>\n<li class=\"\" data-start=\"5378\" data-end=\"5429\">\n<p class=\"\" data-start=\"5380\" data-end=\"5429\"><code data-start=\"5380\" data-end=\"5391\">Singleton<\/code>: One instance for the app&#8217;s lifetime.<\/p>\n<\/li>\n<\/ul>\n<p class=\"\" data-start=\"5431\" data-end=\"5508\"><strong data-start=\"5431\" data-end=\"5506\">Q4: How do you register multiple implementations of the same interface?<\/strong><\/p>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre! language-csharp\">builder.Services.AddTransient&lt;INotificationService, EmailNotificationService&gt;();<br \/>\nbuilder.Services.AddTransient&lt;INotificationService, SmsNotificationService&gt;();<br \/>\n<\/code><\/div>\n<\/div>\n<p class=\"\" data-start=\"5683\" data-end=\"5750\">You can inject <code data-start=\"5698\" data-end=\"5733\">IEnumerable&lt;INotificationService&gt;<\/code> to resolve both.<\/p>\n<p class=\"\" data-start=\"5752\" data-end=\"5892\"><strong data-start=\"5752\" data-end=\"5807\">Q5: What is the default IoC container in .NET Core?<\/strong><br data-start=\"5807\" data-end=\"5810\" \/>\ud83d\udc49 Microsoft.Extensions.DependencyInjection is the built-in default IoC container.<\/p>\n<hr class=\"\" data-start=\"5894\" data-end=\"5897\" \/>\n<h3 class=\"\" data-start=\"5899\" data-end=\"5926\">\ud83d\udd39 <strong data-start=\"5906\" data-end=\"5926\">Tricky Questions<\/strong><\/h3>\n<p class=\"\" data-start=\"5928\" data-end=\"6092\"><strong data-start=\"5928\" data-end=\"5993\">Q6: Can you inject a service inside another injected service?<\/strong><br data-start=\"5993\" data-end=\"5996\" \/>\ud83d\udc49 Yes. The DI container supports nested dependencies and will resolve the full dependency tree.<\/p>\n<p class=\"\" data-start=\"6094\" data-end=\"6322\"><strong data-start=\"6094\" data-end=\"6163\">Q7: What happens if you inject a scoped service into a singleton?<\/strong><br data-start=\"6163\" data-end=\"6166\" \/>\ud83d\udc49 It causes a runtime exception. Scoped services cannot be safely injected into singletons. You should use <code data-start=\"6274\" data-end=\"6292\">IServiceProvider<\/code> or a factory pattern instead.<\/p>\n<p class=\"\" data-start=\"6324\" data-end=\"6467\"><strong data-start=\"6324\" data-end=\"6390\">Q8: How would you resolve dependencies dynamically at runtime?<\/strong><br data-start=\"6390\" data-end=\"6393\" \/>\ud83d\udc49 You can use <code data-start=\"6408\" data-end=\"6442\">IServiceProvider.GetService&lt;T&gt;()<\/code> or <code data-start=\"6446\" data-end=\"6466\">ActivatorUtilities<\/code>.<\/p>\n<p class=\"\" data-start=\"6469\" data-end=\"6711\"><strong data-start=\"6469\" data-end=\"6547\">Q9: How can you replace a service at runtime (e.g., in integration tests)?<\/strong><br data-start=\"6547\" data-end=\"6550\" \/>\ud83d\udc49 Use the <code data-start=\"6561\" data-end=\"6586\">ConfigureTestServices()<\/code> method in <code data-start=\"6597\" data-end=\"6620\">WebApplicationFactory<\/code> or use <code data-start=\"6628\" data-end=\"6639\">Replace()<\/code> extension from <code data-start=\"6655\" data-end=\"6710\">Microsoft.Extensions.DependencyInjection.Abstractions<\/code>.<\/p>\n<p class=\"\" data-start=\"6713\" data-end=\"6851\"><strong data-start=\"6713\" data-end=\"6768\">Q10: What if a dependency has its own dependencies?<\/strong><br data-start=\"6768\" data-end=\"6771\" \/>\ud83d\udc49 The IoC container recursively resolves all nested dependencies automatically.<\/p>\n<hr class=\"\" data-start=\"6853\" data-end=\"6856\" \/>\n<h2 class=\"\" data-start=\"6858\" data-end=\"6871\">\ud83c\udfaf Summary<\/h2>\n<ul data-start=\"6873\" data-end=\"7146\">\n<li class=\"\" data-start=\"6873\" data-end=\"6939\">\n<p class=\"\" data-start=\"6875\" data-end=\"6939\"><strong data-start=\"6875\" data-end=\"6899\">Dependency Injection<\/strong> enables decoupled and testable designs.<\/p>\n<\/li>\n<li class=\"\" data-start=\"6940\" data-end=\"6991\">\n<p class=\"\" data-start=\"6942\" data-end=\"6991\"><strong data-start=\"6942\" data-end=\"6949\">IoC<\/strong> delegates object creation to a container.<\/p>\n<\/li>\n<li class=\"\" data-start=\"6992\" data-end=\"7048\">\n<p class=\"\" data-start=\"6994\" data-end=\"7048\"><strong data-start=\"6994\" data-end=\"7007\">.NET Core<\/strong> makes it easy with a built-in DI system.<\/p>\n<\/li>\n<li class=\"\" data-start=\"7049\" data-end=\"7146\">\n<p class=\"\" data-start=\"7051\" data-end=\"7146\"><strong data-start=\"7051\" data-end=\"7062\">EF Core<\/strong> registration and service lifetimes are crucial for robust application architecture.<\/p>\n<\/li>\n<\/ul>\n<hr class=\"\" data-start=\"7148\" data-end=\"7151\" \/>\n<h2 class=\"\" data-start=\"7153\" data-end=\"7173\">\ud83d\udce2 Final Thoughts<\/h2>\n<p class=\"\" data-start=\"7175\" data-end=\"7358\">Mastering Dependency Injection and IoC is essential for modern .NET developers. It\u2019s not just about patterns \u2014 it\u2019s about building clean, flexible, testable, and maintainable systems.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udd0d Introduction As a new developer, you\u2019ve probably come across a situation where one class creates and controls the behavior of another class, making it tightly coupled and hard to test or extend. This is where Dependency Injection (DI) and Inversion of Control (IoC) come to the rescue. In this blog, we\u2019ll break down these [&hellip;]<\/p>\n","protected":false},"author":43,"featured_media":927,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[15,159],"tags":[218,152,219],"class_list":["post-926","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-net-core-3-1","category-net-core-top-50-qa","tag-dependency-injection","tag-designpattern","tag-ioc"],"uagb_featured_image_src":{"full":["https:\/\/debuggersspace.com\/wp-content\/uploads\/2025\/04\/ChatGPT-Image-Apr-9-2025-09_05_09-PM-e1744222811442.png",1024,517,false],"thumbnail":["https:\/\/debuggersspace.com\/wp-content\/uploads\/2025\/04\/ChatGPT-Image-Apr-9-2025-09_05_09-PM-e1744222811442-150x150.png",150,150,true],"medium":["https:\/\/debuggersspace.com\/wp-content\/uploads\/2025\/04\/ChatGPT-Image-Apr-9-2025-09_05_09-PM-e1744222811442-300x151.png",300,151,true],"medium_large":["https:\/\/debuggersspace.com\/wp-content\/uploads\/2025\/04\/ChatGPT-Image-Apr-9-2025-09_05_09-PM-e1744222811442-768x388.png",750,379,true],"large":["https:\/\/debuggersspace.com\/wp-content\/uploads\/2025\/04\/ChatGPT-Image-Apr-9-2025-09_05_09-PM-683x1024.png",683,1024,true],"1536x1536":["https:\/\/debuggersspace.com\/wp-content\/uploads\/2025\/04\/ChatGPT-Image-Apr-9-2025-09_05_09-PM-e1744222811442.png",1024,517,false],"2048x2048":["https:\/\/debuggersspace.com\/wp-content\/uploads\/2025\/04\/ChatGPT-Image-Apr-9-2025-09_05_09-PM-e1744222811442.png",1024,517,false]},"uagb_author_info":{"display_name":"Himanshu Namdeo","author_link":"https:\/\/debuggersspace.com\/author\/admin\/"},"uagb_comment_info":1,"uagb_excerpt":"\ud83d\udd0d Introduction As a new developer, you\u2019ve probably come across a situation where one class creates and controls the behavior of another class, making it tightly coupled and hard to test or extend. This is where Dependency Injection (DI) and Inversion of Control (IoC) come to the rescue. In this blog, we\u2019ll break down these&hellip;","_links":{"self":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/926"}],"collection":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/users\/43"}],"replies":[{"embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/comments?post=926"}],"version-history":[{"count":4,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/926\/revisions"}],"predecessor-version":[{"id":932,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/926\/revisions\/932"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/media\/927"}],"wp:attachment":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/media?parent=926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/categories?post=926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/tags?post=926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}