Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Dependency Injection in C#

Introduction

Dependency Injection (DI) is a design pattern used to implement IoC (Inversion of Control). It allows the creation of dependent objects outside of a class and provides those objects to a class in various ways. This technique helps to make the code more modular, easier to test, and maintain.

Why Use Dependency Injection?

Dependency Injection is beneficial for several reasons:

  • Improves code modularity
  • Enhances testability
  • Promotes loose coupling
  • Facilitates better management of dependencies

Types of Dependency Injection

There are three common types of Dependency Injection:

  • Constructor Injection: Dependencies are provided through a class constructor.
  • Property Injection: Dependencies are provided through public properties.
  • Method Injection: Dependencies are provided through method parameters.

Constructor Injection Example

Let's create a simple example demonstrating Constructor Injection in C#.


public interface IService
{
    void Serve();
}

public class Service : IService
{
    public void Serve()
    {
        Console.WriteLine("Service Called");
    }
}

public class Client
{
    private readonly IService _service;

    public Client(IService service)
    {
        _service = service;
    }

    public void Start()
    {
        _service.Serve();
    }
}

class Program
{
    static void Main(string[] args)
    {
        IService service = new Service();
        Client client = new Client(service);
        client.Start();
    }
}
                

In this example, the Client class depends on the IService interface. The Service class implements the IService interface and is injected into the Client class through its constructor.

Property Injection Example

Now, let's see an example of Property Injection.


public interface IService
{
    void Serve();
}

public class Service : IService
{
    public void Serve()
    {
        Console.WriteLine("Service Called");
    }
}

public class Client
{
    public IService Service { get; set; }

    public void Start()
    {
        Service.Serve();
    }
}

class Program
{
    static void Main(string[] args)
    {
        IService service = new Service();
        Client client = new Client();
        client.Service = service;
        client.Start();
    }
}
                

In this example, the Client class has a public property Service of type IService which is set by the Main method.

Method Injection Example

Finally, let's look at an example of Method Injection.


public interface IService
{
    void Serve();
}

public class Service : IService
{
    public void Serve()
    {
        Console.WriteLine("Service Called");
    }
}

public class Client
{
    public void Start(IService service)
    {
        service.Serve();
    }
}

class Program
{
    static void Main(string[] args)
    {
        IService service = new Service();
        Client client = new Client();
        client.Start(service);
    }
}
                

In this example, the Client class has a method Start that takes an IService parameter. The Main method injects the Service instance when calling the Start method.

Conclusion

Dependency Injection is a powerful design pattern that allows better management of dependencies in your code. It enhances modularity, testability, and maintainability. By using DI, you can easily swap out implementations, making your code more flexible and adaptable to changes.