<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Subasri Viswanathan</title>
    <description>The latest articles on DEV Community by Subasri Viswanathan (@subasriviswanathan).</description>
    <link>https://dev.to/subasriviswanathan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3657105%2F0c1c23c4-c3d7-41b0-8579-3182664c9834.jpg</url>
      <title>DEV Community: Subasri Viswanathan</title>
      <link>https://dev.to/subasriviswanathan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/subasriviswanathan"/>
    <language>en</language>
    <item>
      <title>Dependency Injection in .NET</title>
      <dc:creator>Subasri Viswanathan</dc:creator>
      <pubDate>Thu, 11 Dec 2025 12:37:15 +0000</pubDate>
      <link>https://dev.to/subasriviswanathan/dependency-injection-in-net-22j0</link>
      <guid>https://dev.to/subasriviswanathan/dependency-injection-in-net-22j0</guid>
      <description>&lt;p&gt;Dependency Injection (DI) is one of the most commonly used design patterns in .NET.&lt;br&gt;
If you’re new to DI, this guide explains it in the simplest way possible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What Is Dependency Injection?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Dependency Injection means:&lt;/p&gt;

&lt;p&gt;A class should not create its own dependencies.&lt;br&gt;
Instead, dependencies should be provided (injected) from the outside.&lt;/p&gt;

&lt;p&gt;This is usually done through &lt;strong&gt;Program.cs or DependencyInjection.cs&lt;/strong&gt; in ASP.NET Core.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Do We Use DI?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here are the main advantages:&lt;/p&gt;

&lt;p&gt;✔ Loose Coupling&lt;br&gt;
Your classes do not depend on specific implementations.&lt;/p&gt;

&lt;p&gt;✔ No Need to Create Instances Manually&lt;br&gt;
You don’t use new ClassName() everywhere. The DI container manages it.&lt;/p&gt;

&lt;p&gt;✔ Easier to Unit Test&lt;br&gt;
You can replace real services with mocks.&lt;/p&gt;

&lt;p&gt;✔ Cleaner, Maintainable Code&lt;br&gt;
Follows the SOLID “Dependency Inversion Principle”.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How DI Works in C#&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Using DI in .NET involves three simple steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Register the service in Program.cs&lt;/li&gt;
&lt;li&gt;Inject the service into the constructor&lt;/li&gt;
&lt;li&gt;Use the service in the class&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Service Lifetimes in DI&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;.NET provides three lifetimes. Choosing the correct one is important.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Singleton&lt;/strong&gt;&lt;br&gt;
One instance for the entire application.&lt;br&gt;
Created once and reused everywhere.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Scoped&lt;/strong&gt;&lt;br&gt;
One instance per HTTP request.&lt;br&gt;
Perfect for services using DbContext.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Transient&lt;/strong&gt;&lt;br&gt;
A new instance every time it’s requested.&lt;br&gt;
Best for lightweight, stateless services.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Types of Dependency Injection&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;✔ Constructor Injection (Most Recommended)&lt;br&gt;
Dependencies are passed through the class constructor.&lt;/p&gt;

&lt;p&gt;✔ Method Injection&lt;br&gt;
Dependencies are passed directly into a method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1 — Create an interface and class&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public interface IAmazonS3
{
    Task&amp;lt;string&amp;gt; UploadFromStream();
}

public class AmazonS3 : IAmazonS3
{
    public async Task&amp;lt;string&amp;gt; UploadFromStream()
    {
        // Upload logic goes here
        return "Uploaded";
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2 — Inject the service into your class&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class ReportService
{
    private readonly IAmazonS3 _amazonS3;
    public ReportService(IAmazonS3 amazonS3)
    {
        _amazonS3 = amazonS3;
    }

    public async Task SaveReport()
    {
        await _amazonS3.UploadFromStream();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3 — Register the service in DI container&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Program.cs or DependencyInjection.cs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;builder.Services.AddScoped&amp;lt;IAmazonS3, AmazonS3&amp;gt;();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, whenever IAmazonS3 is needed, AmazonS3 will be automatically injected.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Dependency Injection reduces coupling and improves maintainability.&lt;br&gt;
Instead of creating objects manually, .NET injects them for you.&lt;br&gt;
Three lifetimes exist: Singleton, Scoped, Transient.&lt;br&gt;
Constructor injection is the most commonly used pattern.&lt;br&gt;
DI keeps your application clean, testable, and easy to extend.&lt;/p&gt;

</description>
      <category>dotnet</category>
    </item>
  </channel>
</rss>
