| by Arround The Web | No comments

Dependency Injection C#

Dependency Injection is a programming design pattern used in software engineering, including C#, to provide more efficient and maintainable software. Dependency Injection (DI) helps to eliminate the hard-coded dependencies between software components, making it easier to test, change, and maintain the code The function of Dependency Injection in C# and how to utilize it will be covered in this post.

What is Dependency Injection in C#?

A programming design technique called dependency injection works to loosen the coupling between software parts. The degree to which one component is dependent upon another is referred to as coupling. When software components are tightly coupled, changing one component can cause a chain reaction of changes in other components. Numerous issues, such as code duplication, difficult code to maintain, and testing problems, can result from this.

Dependency Injection, on the other hand, involves injecting the dependencies of a component into that component instead of having the component create or find its dependencies itself. In other words, the dependencies of a component are passed in from the outside rather than being created internally. This way, the components are decoupled from their dependencies, making them more flexible, testable, and maintainable.

How does Dependency Injection Work in C#?

Dependency Injection works by providing a way for a component to specify its dependencies through its constructor or properties. When the component is created, the dependencies are passed in through these parameters or properties. This eliminates the requirement for the component to understand how to generate or locate its dependencies

There are three main types of Dependency Injection:

Constructor Injection

In Constructor Injection, the dependencies are passed in through the constructor of the component. The component declares its dependencies in the constructor parameters, and the Dependency Injection container passes them in when the component is created. The following example demonstrates the use of constructor injection to inject a dependency into a class, allowing it to perform calculations without having to create the dependency itself:

using System;

public interface ICalculatorService
{
    int Add(int a, int b);
    int Subtract(int a, int b);
}

public class CalculatorService : ICalculatorService
{
    public int Add(int a, int b)
    {
        return a + b;
    }

    public int Subtract(int a, int b)
    {
        return a - b;
    }
}

public class Calculator
{
    private readonly ICalculatorService _calculatorService;

    public Calculator(ICalculatorService calculatorService)
    {
        _calculatorService = calculatorService;
    }

    public void DoCalculations(int a, int b)
    {
        int sum = _calculatorService.Add(a, b);
        int difference = _calculatorService.Subtract(a, b);

        Console.WriteLine("Sum: {0}", sum);
        Console.WriteLine("Difference: {0}", difference);
    }
}

class Program
{
    static void Main(string[] args)
    {
        ICalculatorService calculatorService = new CalculatorService();
        Calculator calculator = new Calculator(calculatorService);

        calculator.DoCalculations(5, 3);

        Console.ReadLine();
    }
}

In this example, the Calculator class takes an ICalculatorService parameter in its constructor using constructor injection. The CalculatorService class implements the ICalculatorService interface and provides methods for adding and subtracting integers.

When the DoCalculations method of the Calculator class is called, it calls the Add and Subtract methods of the injected ICalculatorService instance to calculate the sum and difference of two integers.

The Main method creates a CalculatorService instance and passes it to the Calculator constructor using constructor injection. The DoCalculations method is then called with two integer parameters, and the sum and difference are output to the console.

Method Injection

In Method Injection, the dependencies are passed in through a method of the component. The component declares a method that takes its dependencies as parameters, and the Dependency Injection container calls this method after the component is created. This example demonstrates the use of method injection to inject a dependency into a class method, allowing it to perform calculations without creating the dependency itself.

using System;

public interface ICalculatorService
{
    int Add(int a, int b);
}

public class CalculatorService : ICalculatorService
{
    public int Add(int a, int b)
    {
        return a + b;
    }
}

public class Foo
{
    public int AddTwoNumbers(ICalculatorService calculatorService, int a, int b)
    {
        return calculatorService.Add(a, b);
    }
}

class Program
{
    static void Main(string[] args)
    {
        ICalculatorService calculatorService = new CalculatorService();
        Foo foo = new Foo();

        int result = foo.AddTwoNumbers(calculatorService, 5, 3);
        Console.WriteLine("Result: {0}", result);

        Console.ReadLine();
    }
}

In this example, the Foo class has a public method called AddTwoNumbers that takes an ICalculatorService dependency as a parameter, along with two integers. The AddTwoNumbers method calls the Add method of the injected ICalculatorService instance to add the two integers.

In the Main method, a CalculatorService instance is created and passed as a parameter to the AddTwoNumbers method of the Foo instance using method injection. The AddTwoNumbers method is then called with two integer parameters, and the result is output to the console.

Property Injection

In Property Injection, the dependencies are passed through the component’s properties. The component declares its dependencies as public properties, and the Dependency Injection container sets them after the component is created. Here is an example code that demonstrates the use of property injection to inject a dependency into a class, allowing it to perform calculations without having to create the dependency itself:

using System;

public interface ICalculatorService
{
    int Add(int a, int b);
}

public class CalculatorService : ICalculatorService
{
    public int Add(int a, int b)
    {
        return a + b;
    }
}

public class Foo
{
    public ICalculatorService CalculatorService { get; set; }

    public int AddTwoNumbers(int a, int b)
    {
        return CalculatorService.Add(a, b);
    }
}

class Program
{
    static void Main(string[] args)
    {
        ICalculatorService calculatorService = new CalculatorService();
        Foo foo = new Foo();
        foo.CalculatorService = calculatorService;

        int result = foo.AddTwoNumbers(5, 3);
        Console.WriteLine("Result: {0}", result);

        Console.ReadLine();
    }
}

In this example, the Foo class has a public ICalculatorService property called CalculatorService. The AddTwoNumbers method of the Foo class calls the Add method of the injected ICalculatorService instance to add two integers.

In the Main method, a CalculatorService instance is created and set as the CalculatorService property of the Foo instance using property injection. The AddTwoNumbers method is then called with two integer parameters, and the result is output to the console.

Conclusion

Dependency Injection is a programming design pattern used in C# and other programming languages to provide more efficient and maintainable software. It helps to eliminate the hard-coded dependencies between software components, making it easier to test, change, and maintain the code. By using Dependency Injection, developers can build more flexible, scalable, and maintainable applications that are easier to test and debug.

Share Button

Source: linuxhint.com

Leave a Reply