Dependency injection interview questions: Complete guide

Dependency injection interview questions: Any software is composed of many objects that collaborate with every different to carry out some beneficial stuff. traditionally each object is answerable for acquiring its own references to the based gadgets (dependencies) it collaborates with. This ends in tremendously coupled lessons and hard-to-check code.

For example, take into account a car object. A vehicle depends on Wheels, Engine, gas, Battery, and so on to run. traditionally we outline the brand of such structured items together with the definition of the auto item.

class Car{

  private Wheel wh= new NepaliRubberWheel();

  private Battery bt= new ExcideBattery();

  //rest

}

Here, the automobile object is answerable for creating the dependent gadgets.

What if we need to change the kind of its established item – say Wheel – after the initial NepaliRubberWheel() punctures? We need to recreate the automobile item with its new dependency say ChineseRubberWheel(), but best the automobile producer can try this.

Then what Dependency Injection will we for …

Whilst using Dependency Injection, items are given their dependencies at run time in preference to collect time (vehicle production time). in order that we can now change the Wheel every time we want. right here, the Dependency (Wheel) can be injected into an automobile at run time.

Inversion of manipulating (IoC) is a well-known concept, and it could be expressed in many exclusive ways and Dependency Injection is simply one concrete instance of Inversion of control.

This concept says that you do not create your gadgets however describe how they have to be created. You do not without delay join your additives and offerings collectively in code however describe which offerings are wanted with the aid of which additives in a configuration document. A container is then chargeable for hooking all of it up.

Provide a top-level view of the way Dependency Injection Works

Dependency Injection is a method to eliminate the dependencies of 1 magnificence on another via making it unbiased and decouple using shared gadgets. It uses a builder item to initialize the items and injects the dependency from outside the class via imparting the required dependencies.

Dependency Injection has the subsequent obligations:

  • Create the gadgets of classes.
  • hold a track of instructions that have a dependency on different lessons.
  • provide all of the items which might be required by using the lessons to finish the functionality.

Different Ways To Implement Dependency Injection? 

Dependency Injection is a design sample that lets you put in force IoC. Suppose we take an example of the Account class, which has references to the present Account and Saving Account

public class SavingAccount  {  

    public void GetData()  

    {  

        Console.WriteLine(“Get Saving Account Data.”);  

    }  }  

  public class CurrentAccount  {  

    public void GetData()  

    {  

        Console.WriteLine(“Get Current Account Data.”);  

    }  }  //CustomerAccount is tightly dependent on Saving and Current Account classes public class CustomerAccount  {  

    // Create object of the class SavingAccount

    SavingAccount objSavingAccount = new SavingAccount();  

    // Create object of the class CurrentAccount

    CurrentAccount objCurrentAccount = new CurrentAccount();  

    public void GetAccountData()  

    {  

        objSavingAccount.GetData();  

        objCurrentAccount.GetData();  

    }  }   public class Program  {  

    static void Main(string[] args)  

    {  

        CustomerAccount objCustomerAccount = new CustomerAccount();  

        account.GetAccountData(); 

        Console.ReadLine();  

    }  }

 

Constructor Injection

//Create ICustomerAccount  interface to decouple the objects  public interface ICustomerAccount  {  

    void GetData ();  }  

  //Implemented the IAccount interface in SavingAccount class  public class SavingAccount  : ICustomerAccount  {  

    public void GetData()  

    {  

        Console.WriteLine(“Get Saving Account Data.”);  

    }  }  

  public class CurrentAccount  : ICustomerAccount  {  

    public void GetData()  

    {  

        Console.WriteLine(“Get Current Account Data.”);  

    }  }  

  public class CustomerAccount  {  

    private ICustomerAccount  CustomerAccount;  

    //Passing ICustomerAccount  interface as a parameter

    public Account(ICustomerAccount  account)  

    {  

        this.CustomerAccount = account;  

    }  

    public void GetAccountData()  

    {  

        CustomerAccount.GetData();  

    }  }  

  class Program  {  

    static void Main()  

    {  

        //Create an object of SavingAccount() and  pass it to CustomerAccount constructor  

        ICustomerAccount savingAccount = new SavingAccount();  

        CustomerAccount account = new CustomerAccount (savingAccount);  

        account.GetAccountData();  

        //Create an object of CurrentAccount() and  pass it to CustomerAccount constructor  

        ICustomerAccount currentAccount = new CurrentAccount();  

        account = new CustomerAccount(currentAccount);  

        account.GetAccountData();  

        Console.ReadLine();  

    }  }  

 

Setter Injection

public class CustomerAccount  {  

    public ICustomerAccount  CustomerAccount {get; set;} 

    public void GetAccountData()  

    {  

        CustomerAccount.GetData();  

    }  }  

class Program  {  

    static void Main()  

    {  

        //Create an object of SavingAccount() and assign it to the property  

        CustomerAccount account = new CustomerAccount (); 

        account.CustomerAccount = new SavingAccount();

        account.GetAccountData();  

        //Create an object of CurrentAccount() and assign it to the property  

        CustomerAccount account = new CustomerAccount (); 

        account.CustomerAccount = new CurrentAccount();

        account.GetAccountData();  

        Console.ReadLine();  

    }  }  

To get more knowledge about Dependency injection interview questions: please check our site: dfchecking.com

Leave a Reply

Your email address will not be published. Required fields are marked *