<?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: lokesh</title>
    <description>The latest articles on DEV Community by lokesh (@lokeshraj).</description>
    <link>https://dev.to/lokeshraj</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3806419%2Fc29be064-bd43-4eb3-aff3-b212db0322e5.jpg</url>
      <title>DEV Community: lokesh</title>
      <link>https://dev.to/lokeshraj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lokeshraj"/>
    <language>en</language>
    <item>
      <title>Stop Hardcoding! Master Dependency Injection in .NET 8/9 like a Pro</title>
      <dc:creator>lokesh</dc:creator>
      <pubDate>Wed, 04 Mar 2026 19:09:42 +0000</pubDate>
      <link>https://dev.to/lokeshraj/stop-hardcoding-master-dependency-injection-in-net-89-like-a-pro-5e12</link>
      <guid>https://dev.to/lokeshraj/stop-hardcoding-master-dependency-injection-in-net-89-like-a-pro-5e12</guid>
      <description>&lt;p&gt;Introduction&lt;br&gt;
As a .NET Developer with 2 years of experience, I’ve seen many beginners struggle with "Tight Coupling." If you are hardcoding your classes using the new keyword everywhere, your code is a nightmare to test and maintain.&lt;/p&gt;

&lt;p&gt;Today, let’s break down Dependency Injection (DI) in the simplest way possible.&lt;/p&gt;

&lt;p&gt;What is Dependency Injection?&lt;br&gt;
In simple terms: Instead of a class "creating" its own tools, we "inject" the tools it needs from the outside.&lt;/p&gt;

&lt;p&gt;The Problem: Tight Coupling ❌&lt;br&gt;
Look at this code. Here, UserService is tightly coupled with EmailService. If I want to change the email provider, I have to change the UserService code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fasyvgma2mq1jmtd5ss9f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fasyvgma2mq1jmtd5ss9f.png" alt=" " width="800" height="229"&gt;&lt;/a&gt;&lt;br&gt;
The Solution: Dependency Injection&lt;br&gt;
We use an Interface to decouple the classes.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create an Interface&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv3pyhzp0lz21x3cbzlz3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv3pyhzp0lz21x3cbzlz3.png" alt=" " width="737" height="105"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Inject via Constructor
Now, UserService doesn't care which email service it uses, as long as it follows the IEmailService rule.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvfqrhgx4vrs6cfv2qlbb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvfqrhgx4vrs6cfv2qlbb.png" alt=" " width="778" height="335"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Understanding Service Lifetimes&lt;br&gt;
In .NET Core, we have three main ways to register these services in Program.cs. This is a top interview question!&lt;/p&gt;

&lt;p&gt;Transient: A new instance is created every time it is requested.&lt;/p&gt;

&lt;p&gt;Scoped: A new instance is created once per client request (perfect for Database Context).&lt;/p&gt;

&lt;p&gt;Singleton: Only one instance is created for the entire lifetime of the application.&lt;/p&gt;

&lt;p&gt;Registration in Program.cs&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkwer3hgmnqh7kqsjqxwj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkwer3hgmnqh7kqsjqxwj.png" alt=" " width="653" height="171"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Conclusion&lt;br&gt;
Dependency Injection makes your code:&lt;/p&gt;

&lt;p&gt;Testable: You can easily swap real services with "Mocks."&lt;/p&gt;

&lt;p&gt;Maintainable: Changes in one class don't break others.&lt;/p&gt;

&lt;p&gt;Scalable: Clean Architecture follows this as a core rule.&lt;/p&gt;

&lt;p&gt;What is your favorite DI lifetime? Let me know in the comments!&lt;/p&gt;

&lt;h1&gt;
  
  
  dotnet #csharp #webdev #programming #tutorial
&lt;/h1&gt;

</description>
      <category>beginners</category>
      <category>csharp</category>
      <category>dotnet</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
