Dependency injection is a design pattern that allows you to decouple your classes from their dependencies, such as other classes, interfaces, or frameworks. Dependency injection makes your code more modular, testable, and maintainable, as you can easily swap or mock the dependencies without changing the dependent class.
The basic idea of dependency injection is to pass the dependencies to the dependent class through a constructor, a property, or a method, rather than creating them inside the class. This way, the dependent class does not need to know how to create or access the dependencies, but only how to use them.
Dependency Injection in C#: A Simple Example
For example, suppose you have a class called EmailSender that sends emails using an external service called EmailService. Without dependency injection, you would create an instance of EmailService inside the EmailSender class, like this:
public class EmailSender
{
// Create an instance of EmailService
private EmailService emailService = new EmailService();
// Send an email using EmailService
public void SendEmail(string to, string subject, string body)
{
emailService.Send(to, subject, body);
}
}
However, this approach has some drawbacks. For instance, if you want to change the email service provider, you would have to modify the EmailSender class. Also, if you want to test the EmailSender class, you would have to deal with the real EmailService, which may be slow or unreliable.
With dependency injection, you would pass an instance of EmailService to the EmailSender class through a constructor parameter, like this:
public class EmailSender
{
// Declare a private field for EmailService
private EmailService emailService;
// Inject an instance of EmailService through the constructor
public EmailSender(EmailService emailService)
{
this.emailService = emailService;
}
// Send an email using EmailService
public void SendEmail(string to, string subject, string body)
{
emailService.Send(to, subject, body);
}
}
Now, the EmailSender class does not create or access the EmailService directly, but receives it as a dependency. This way, you can easily change or mock the EmailService without affecting the EmailSender class.
In conclusion, dependency injection is a useful technique that helps you write cleaner and more flexible code in C#. You can use various frameworks or libraries that support dependency injection in C#, such as Autofac, Ninject, or Unity.
Another example:
The dependency injection pattern one of the most popular design paradigms today. It is process of removing dependency of object which creates the independent business objects. It is very useful for Test Driven Development.
Take a look this example. First we need to create one employee class create a constructor objects, next we need to create ILogger interface with writeToLog method. Finally create two LoggerOne and LoggerTwo implement ILogger to modified writeToLog method.
Explore My Other Channel for More Cool and Valuable Insights
👉 Youtube Learn Tech Tips👉 Tiktok
👉 Facebook:Code demo:
We have one Interface and three classes:- Interface: ILogger.cs
- Three Classes: Employee, LoggerOne.cs, LoggerTwo.cs
Interface: ILogger.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DependencyInjection
{
public interface ILogger
{
void writeToLog(string text);
}
}
Three Classes:
Employee.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DependencyInjection
{
public class Employee
{
public Employee(ILogger logger)
{
logger.writeToLog("New employee created");
}
}
}
LoggerOne.cs implement from ILogger.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DependencyInjection
{
class LoggerOne : ILogger
{
public void writeToLog(string text)
{
Console.WriteLine(text);
}
}
}
LoggerTwo.cs implement from ILogger.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DependencyInjection
{
class LoggerTwo : ILogger
{
public void writeToLog(string text)
{
Console.WriteLine("*************** \n {0} \n ***************" + text);
}
}
}
Main Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DependencyInjection
{
class Program
{
static void Main(string[] args)
{
Employee employee1 = new Employee(new LoggerOne());
Employee employee2 = new Employee(new LoggerTwo());
Console.ReadLine();
}
}
}
And the output:
New employee created
*******************
New employee created
*******************
#CSharp #DependencyInjection #DesignPattern #Decoupling #Modularity #Testability #Maintainability #ConstructorInjection #Autofac #Ninject #Unity