Home  >  Article  >  Backend Development  >  Dependency injection in C# explained

Dependency injection in C# explained

王林
王林forward
2023-08-26 16:05:181515browse

Dependency injection in C# explained

A dependency is an object that another object depends on. Dependency injection (or inversion) is basically providing an object with the objects it needs instead of letting it construct the object itself. This is a useful technique that makes testing easier because it allows you to mock dependencies.

For example, if class A calls a method on class B, and class B calls a method on class C, it means that A depends on B and B depends on C. Using dependency injection, we can pass an instance of class C to class B, and an instance of B to class A, instead of having these classes construct instances of B and C.

In the following example, Class Runner depends on class Logger. Note that an instance of Logger is created in the constructor of class Runner. There are some problems with this code.

  • This ties the logger class to the Runner, we cannot replace it with another class, No need to modify the Runner.

  • If Logger has any dependencies, then Worker must configure them first Instantiate the Logger.

  • Testing is more difficult. If Logger is a resource-intensive class, such as access network or file system, which can slow down the test. We can't replace it easily.

using System;
class Program{
   static void Main(string[] args){
      var runner = new Runner();
      runner.Run();
   }
}
class Runner{
   private Logger _logger;
   public Runner(){
      _logger = new Logger();
   }
   public void Run(){
      // Do some work
      _logger.Log("Message to be logged");
   }
}
class Logger{
   public void Log(string message){
      Console.WriteLine(message);
   }
}

Using dependency injection, we modify Runner's constructor to accept the interface ILogger instead of a concrete object. We change the Logger class to implement ILogger. This allows us to pass an instance of the Logger class to the Runner constructor. The advantage of this is that during testing we can create a TestLogger class that implements ILogger and pass it to the Runner's constructor.

Example

Real-time demonstration

using System;
class Program{
   static void Main(string[] args){
      var logger = new Logger();
      var runner = new Runner(logger);
      runner.Run();
   }
}
class Runner{
   private ILogger _logger;
   public Runner(ILogger logger){
      _logger = logger;
   }
   public void Run(){
      // Do some work
      _logger.Log("Message to be logged");
   }
}
interface ILogger{
   void Log(string message);
}
class Logger : ILogger{
   public void Log(string message){
      Console.WriteLine(message);
   }
}

Output

Message to be logged

The above is the detailed content of Dependency injection in C# explained. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete