Today 28/11/2020 #1
Tags: todayilearned, todayisearched, programming, productivity
Published: 2020-11-28 01:19:15

#**IoC, DIP and Dependency Injection**#
### **Introduction**

So, today I asked myself what should I learn next what should I do to get better and I ended up in the IoC, DIP, and Dependency Injection. 
In this post, I will try to explain what I read in layman's terms.


## **IoC**

IoC stands for "Inversion of Control" and it is a design principle, not a design pattern.
By the name, you understand that it is used to invert different kinds of controls in object-oriented design to achieve loose coupling.
Loosely coupled means changes in one class should not force other classes to change.
The IoC principle helps in designing loosely coupled classes which make them testable, maintainable, and extensible.
Easy way to understand that is to explain it. So a class must have only one responsibility(it is an OOP principle) so if it has more than one here is where IoC comes. You should give control of the extra responsibility to something else(probably to a class). 

```cs
namespace Ioc
{
    class Program
    {
        static void Main(string[] args)
        {
           Console.WriteLine("Enter First Name:");
           var firstName = Console.ReadLine();
           Console.WriteLine("Enter Last Name:");
           var lastName = Console.ReadLine();
           Console.WriteLine("Do you want to save it? Y/N: ");
           var wantToSave = Console.ReadLine();
           if (wantToSave.ToUpper() == "Y")
                   SaveToDB(firstName, lastName);

           private static void SaveToDB(string firstName,
                                                  string lastName)
           {
            //save firstName and lastName to the database here..
           }
        }
    }
```
***^IoC can be applied to the above program by creating a GUI-based application.***

```cs
public class CustomerBusinessLogic
{
    DataAccess _dataAccess;

    public CustomerBusinessLogic()
    {
        _dataAccess = new DataAccess();
    }

    public string GetCustomerName(int id)
    {
        return _dataAccess.GetCustomerName(id);
    }
}

public class DataAccess
{
    public DataAccess()
    {
    }

    public string GetCustomerName(int id) {
        return "Dummy Customer Name"; // get it from DB in real app
    }
}
```
***^Without IoC Implementation***

```cs
public class CustomerBusinessLogic
{

    public CustomerBusinessLogic()
    {
    }

    public string GetCustomerName(int id)
    {
        DataAccess _dataAccess =  
               DataAccessFactory.GetDataAccessObj();

        return _dataAccess.GetCustomerName(id);
    }
}
public class DataAccess
{
    public DataAccess()
    {
    }

    public string GetCustomerName(int id) {
        return "Dummy Customer Name"; // get it from DB in real app
    }
}
public class DataAccessFactory
{
    public static DataAccess GetDataAccessObj() 
    {
        return new DataAccess();
    }
}
```
***^With IoC Implementation***

#####As you can see above, class CustomerBusinessLogic uses DataAccessFactory class to get an object of class DataAccess. Thus, we have inverted the dependent object creation from class CustomerBusinessLogic to DataAccessFactory. Class CustomerBusinessLogic no longer creates an object of class DataAccess, instead, it uses the DataAccessFactory class to get the object of class DataAccess.#####

## **DIP**

DIP stands for "Dependency Inversion Principle" and it is the second step to achieve loosely coupled classes.
DIP is one of the SOLID object-oriented principle.
####It is separated into two parts####
 **1.High-level modules should not depend on low-level modules. Both should depend on the abstraction.**
 **2.Abstractions should not depend on details. Details should depend on abstractions.** <br>
A high-level module is a module that depends on other modules and that module that not depends on other modules is low-level. 

```cs
public class CustomerBusinessLogic
{

    public CustomerBusinessLogic()
    {
    }

    public string GetCustomerName(int id)
    {
        DataAccess _dataAccess =  
               DataAccessFactory.GetDataAccessObj();

        return _dataAccess.GetCustomerName(id);
    }
}
public class DataAccess
{
    public DataAccess()
    {
    }

    public string GetCustomerName(int id) {
        return "Dummy Customer Name"; // get it from DB in real app
    }
}
public class DataAccessFactory
{
    public static DataAccess GetDataAccessObj() 
    {
        return new DataAccess();
    }
}
```
***^Without DIP Implementation***

```cs
public interface ICustomerDataAccess
{
    string GetCustomerName(int id);
}

public class CustomerBusinessLogic
{
    ICustomerDataAccess _custDataAccess;

    public CustomerBusinessLogic()
    {
        _custDataAccess = 
            DataAccessFactory.GetCustomerDataAccessObj();
    }

    public string GetCustomerName(int id)
    {
        return _custDataAccess.GetCustomerName(id);
    }
}
public class DataAccess: ICustomerDataAccess
{
    public DataAccess()
    {
    }

    public string GetCustomerName(int id) {
        return "Dummy Customer Name"; // get it from DB in real app
    }
}
public class DataAccessFactory
{
    public static ICustomerDataAccess GetCustomerDataAccessObj() 
    {
        return new CustomerDataAccess();
    }
}
```
***^With DIP Implementation***
#####The advantages of implementing DIP in the above example is that the CustomerBusinessLogic and CustomerDataAccess classes are loosely coupled classes because CustomerBusinessLogic does not depend on the concrete DataAccess class, instead it includes a reference of the ICustomerDataAccess interface. So now, we can easily use another class which implements ICustomerDataAccess with a different implementation.#####

## **Dependency Injection**
Dependency Injection (DI) is a design pattern used to implement IoC. 
It allows the creation of dependent objects outside of a class and provides those objects to a class in different ways.
####The Dependency Injection pattern involves 3 types of classes####
**1.Client Class: The client class (dependent class) is a class that depends on the service class.
2.Service Class: The service class (dependency) is a class that provides service to the client class.
3.Injector Class: The injector class injects the service class object into the client class.**
####Types of Dependency Injection####
1.**Constructor Injection**: In the constructor injection, the injector supplies the service (dependency) through the client class constructor.
2.**Property Injection**: In the property injection (aka the Setter Injection), the injector supplies the dependency through a public property of the client class.
3.**Method Injection**: In this type of injection, the client class implements an interface that declares the method(s) to supply the dependency and the injector uses this interface to supply the dependency to the client class.

```cs
public class CustomerBusinessLogic
{
    ICustomerDataAccess _dataAccess;

    public CustomerBusinessLogic(ICustomerDataAccess 
                               custDataAccess)
    {
        _dataAccess = custDataAccess;
    }

    public CustomerBusinessLogic()
    {
        _dataAccess = new CustomerDataAccess();
    }

    public string ProcessCustomerData(int id)
    {
        return _dataAccess.GetCustomerName(id);
    }
}

public interface ICustomerDataAccess
{
    string GetCustomerName(int id);
}

public class CustomerDataAccess: ICustomerDataAccess
{
    public CustomerDataAccess()
    {
    }

    public string GetCustomerName(int id) 
    {
        //get the customer name from the db in real application        
        return "Dummy Customer Name"; 
    }
}
public class DataAccessFactory
{
    public static ICustomerDataAccess GetCustomerDataAccessObj() 
    {
        return new CustomerDataAccess();
    }
}
public class CustomerService
{
    CustomerBusinessLogic _customerBL;

    public CustomerService()
    {
        _customerBL = new CustomerBusinessLogic(new 
                             CustomerDataAccess());
    }

    public string GetCustomerName(int id) {
        return _customerBL.ProcessCustomerData(id);
    }
}
```
***^Constructor Injection***
#####As you can see in the above example, the CustomerService class creates and injects the CustomerDataAccess object into the CustomerBusinessLogic class. Thus, the CustomerBusinessLogic class doesn't need to create an object of CustomerDataAccess using the new keyword or using factory class. The calling class (CustomerService) creates and sets the appropriate DataAccess class to the CustomerBusinessLogic class.#####
```cs
public class CustomerBusinessLogic
{
    public CustomerBusinessLogic()
    {
    }

    public string GetCustomerName(int id)
    {
        return DataAccess.GetCustomerName(id);
    }

    public ICustomerDataAccess DataAccess { get; set; }
}

public class CustomerService
{
    CustomerBusinessLogic _customerBL;

    public CustomerService()
    {
        _customerBL = new CustomerBusinessLogic();
        _customerBL.DataAccess = new CustomerDataAccess();
    }

    public string GetCustomerName(int id) {
        return _customerBL.GetCustomerName(id);
    }
}
```
***^Property Injection***
#####As you can see above, the CustomerBusinessLogic class includes the public property named DataAccess, where you can set an instance of a class that implements ICustomerDataAccess. So, CustomerService class creates and sets CustomerDataAccess class using this public property.#####
```cs
interface IDataAccessDependency
{
    void SetDependency(ICustomerDataAccess customerDataAccess);
}

public class CustomerBusinessLogic : IDataAccessDependency
{
    ICustomerDataAccess _dataAccess;

    public CustomerBusinessLogic()
    {
    }

    public string GetCustomerName(int id)
    {
        return _dataAccess.GetCustomerName(id);
    }
        
    public void SetDependency(ICustomerDataAccess customerDataAccess)
    {
        _dataAccess = customerDataAccess;
    }
}

public class CustomerService
{
    CustomerBusinessLogic _customerBL;

    public CustomerService()
    {
        _customerBL = new CustomerBusinessLogic();
        ((IDataAccessDependency)_customerBL)
                  .SetDependency(new CustomerDataAccess());
    }

    public string GetCustomerName(int id) {
        return _customerBL.GetCustomerName(id);
    }
}
```
***^Method Injection***
#####In the above example, the CustomerBusinessLogic class implements the IDataAccessDependency interface, which includes the SetDependency() method. So, the injector class CustomerService will now use this method to inject the dependent class (CustomerDataAccess) to the client class.#####

### **Questions**
  1.Should I learn IoC container?
  2.Should I use an IoC container?

### **Next goals**
The next goals are to learn MvvmCross for Xamarin and Xamarin itself.

Any tips or suggestions will be useful so feel free to comment or dm me.