Dependency injections

 Concrete class must not be dependent on each other


This is called low couplings. It is good.

High coupling is bad


Using system;

 

Namespace coupling

{

 Class program

  {

      Static void main(string[] args)

      {

        Console.writelinw("hello world");

        }

     }

Public class UserInterface 

{

Public void GetData()

{

Console.write("enter your username");

Var userName = console.readLine();

Console.write("enter your password");

Var password = console.readLine();

IBusines business= new Business();

business.SignUp(userName, password);

}

}

Public class Business : IBusines

{

Public void SignUp(string userName, string password)

{

// validation 



Var dataAccess = new DataAccess();

DataAccess.Store(userName, password);

}

}


Public class BusinessV2 : IBusiness


{


Public void SignUp(string userName, string password)


{


// validation 






Var dataAccess = new DataAccess();


DataAccess.Store(userName, password);


}


}



Public class DataAccess : IDataAccess

{

Public void Store(string userName, string password)

{

// write the data to db


}

}



Public interface IBusiness

{

 Void SignUp(string userName, string password);

}


Public interface IDataAccess

{

Void store(string userName, string password);

}

}




Reducing coupling


UserInterface -----------> IBusinesClass Interface --------> IDataAccessClass



Benefits of dependency injections

  • Clean code : your code is easier to understand
  • Better reusability: low couplings allows modules to be reused
  • Better unit testing: concrete classes can be replaced by mocks
  • Low coupling : concrete classes can be replaced 



DI in action



Dependency injections vis constructor 

Public class Business: IBusiness

{

Private readonly IDataAccess _dataaccess;

Public Business (IDataAccess dataAccess)

{

_dataAccess = dataAccess;

}

Public void SignUp(string userName, string password)

{

_dataaccesd.Store(username, password);

}

}





Public class UserInterface 


{


Public void GetData()


{


Console.write("enter your username");


Var userName = console.readLine();


Console.write("enter your password");


Var password = console.readLine();

IDataAccess dal = new DataAccess();

IBusines business= new Business(dal);


business.SignUp(userName, password);


}


}







Public class UserInterface 

{

Private readonly IBusiness _business;

Public UserInterface(IBusiness business)

{

_business = business;

}



Public void GetData()

{

Console.write("enter your username");

Var userName = console.readLine();

Console.write("enter your password");

Var password = console.readLine();

_business.SignUp(userName, password);

}

}





Static void main(string[] args)


      {

        IDataAccess dal = new DataAccess();

       IBusiness biz= new Business(dal);

       Var UserInterface= new UserInterface(biz);


        }




3 method of injections in serviceCollection Class

AddSingleton

Same instance for the entire application

One instance for the whole application 


AddScoped

Same instance for the whole request

Everytime a page is viewed (in MVC)

Not perfect for multi threading 



AddTransient

Different instance every time object is requested or injected 

Everytime a new instance is injected

Suitable for multi threading



Using system;

Using Microsoft.Extenaions.DependencyInjection;

Using System.Threading;


namespace scopedVSTransient 

{

Public class ScopedClass

{


}


Public class TransientClass

{

}

Class program

{

 Static void main(string [] args)

{

Var collection= New serviceCollection();

collection.AddScoped<ScopedClass>();

collection.AddTransient<TransientClass>();

Var builder=collection.BuildServiceProvider();

Console.Clear();

Parallel.For(1,10, i =>{

Console.WriteLine($"Scoped ID = {builder.GetService<ScopedClass>().GetHashCode().ToString}");

Console.WriteLine($"Transient ID {builder.GetService<TransientClass>().GetHashCode().ToString}");

});

Console.WriteLine("Hello world!");

Console.WriteLine("press a key");

Console.ReadKey();

}

}

}

Comments