Theme-Logo
  •   .Net
    •   C Sharp(C#)
    •   Web API
    •   Micro Services
    •   ASP.Net
    • ASP.Net MVC
    • .Net Core
  •   Database
    • SQL Server
    • Oracle
    • PostgreSQL
  •   jQuery
    • jQuery Tutorials
    • jQuery Plugins
    • jQuery UI
    • More on jquery
  •   Tutorials
    • Microservices Tutorials
    • DotNet Core Tutorials
    • PostgreSql Tutorials
Dependency Injection (DI)

Dependency Injection (DI) is a software design pattern. It allows us to develop loosely-coupled code. The intent of Dependency Injection is to make code maintainable. Dependency Injection helps to reduce the tight coupling among software components. Dependency Injection reduces the hard-coded dependencies among your classes by injecting those dependencies at run time instead of design time technically. This article explains how to implement Dependency Injection in C# and .NET code.

The process of injecting (converting) coupled (dependent) objects into decoupled (independent) objects is called Dependency Injection.
Types of Dependency Injection

  • Constructor Injection
  • Setter Injection
  • Interface-based injection
  • Service Locator Injection
  • 1. Constructor Injection

    Construction injection is the most commonly used dependency pattern in Object Oriented Programming. The constructor injection normally has only one parameterized constructor, so in this constructor dependency there is no default constructor and we need to pass the specified value at the time of object creation. We can use the injection component anywhere within the class. It addresses the most common scenario where a class requires one or more dependencies.

    2. Setter Injection

    Getter and Setter Injection injects the dependency by using default public properties procedure such as Gettter(get(){}) and Setter(set(){}).

    3. Interface Injection

    Interface Injection is similar to Getter and Setter DI, the Getter and Setter DI uses default getter and setter but Interface Injection uses support interface a kind of explicit getter and setter which sets the interface property. The following code sample illustrates the concept, ISetService is a support interface which has method setServiceRunService which set the interface property.

                                //Support interface
                                interface ISetService
                                {
                                     void setServiceRunService(IService client);
                                }
    
                                //Implementing Interface Injection  
                                public class BusinessLogicImplementationInterfaceDI : ISetService
                                {
                                    IService _client1;
                                    public void setServiceRunService(IService client)
                                    {
                                        _client1 = client;
                                        Console.WriteLine("Interface Injection ==> 
                                        Current Service : {0}", _client1.ServiceMethod());
                                     }
                                }
                            
    Consuming Interface Injection
                                BusinessLogicImplementationInterfaceDI objInterfaceDI = 
                                new BusinessLogicImplementationInterfaceDI();
                                objInterfaceDI= new ClaimService();
                                objInterfaceDI.setServiceRunService(serviceObj);
                            
    4. Service Locator Injection

    Service Locator Injection is also known as Dependency Absorption. It is used to replacement of new operator. It hides the class dependency by invoking methods directly (without creating object). The following code sample illustrates the concept, in the consumption setService method is invoked by class name BusinessLogicImplementation without creating object.

                                //Implementing Service Locator Injection
                                public  class BusinessLogicImplementation
                                {
                                    private static IService _clientLocator;
    
                                    public static IService getService()
                                    {
                                        return _clientLocator;
                                    }
    
                                    public static void setService(IService clientSL)
                                    {
                                     _clientLocator = clientSL;
                                    }
                                }
    
    Consuming Service Locator Injection
                                BusinessLogicImplementation.setService(new PaymentService());
                                IService client2 = BusinessLogicImplementation.getService();
                                Console.WriteLine("Service Locator => Current Service : {0}"
                                ,client2.ServiceMethod());
                            
    Advantages of DI

  • Reduces class coupling
  • Increases code reusability
  • Improves code maintainability
  • Improves application testing
  • Centralized configuration
  • Drawback

    The main drawback of dependency injection is that using many instances together can become a very difficult if there are too many instances and many dependencies that need to be resolved.

    Trending Post
    Dependency Injection in C#
    Inversion of controls (IoC)
    Design patterns
    Service-Oriented Architecture(SOA)
    Interview Questions and Answers
    What is SOLID Principles?
    What is Design patterns?
    What is tight coupling and loose coupling?
    Advantage of design patterns?
    What is difference between IoC and DI?
    About us

    DotNet Palace is a community platform created and maintained by The articles are mainly focused on Microsoft stack technologies like C#, ASP.Net, MVC, .Net Core, SQL Server and Oracle, PostgreSQL, SQLite etc. To improve the site's content you can send your valuable suggestions at info.dotnetpalace@gmail.com

    Quick links
  • SOLID Principles
  • Questions
  • OOPS Principles
  • DotNet Palace Tutorials
  • Privacy Policy
  • Terms and Condition