AspBucket offers ASP.NET, C#, VB, Jquery, CSS, Ajax, SQL tutorials. It is the best place for programmers to learn

Saturday 16 April 2016

How to implement Dependency Injection in C#?

Dependency Injection is a design pattern used to develop loosely coupled code. It allows you to inject a dependency from outside the class. Before discuss on Dependency Injection let's discuss what is loose coupling & Tight coupling in code.

Tight Coupling
public class ClassA
{
    public ClassB some_attribute{ get; set; }
}
 
public class ClassB
{
}
The following code illustrates the concept of tight coupling.

Loose Coupling
public class ClassA
{
    public IClassB some_attribute{ get; set; }
}
 
public interface IClassB 
{
}
 
public class ClassB : IClassB
{
 
}
ClassA class needs to use a ClassB component, then the best you can do is to make your ClassA aware of an IClassB interface rather than a ClassB. In this way, you can change the implementation of the ClassB at any time.

We can implement dependency injection following three ways:

  1. Constructor Injection
  2. Property injection
  3. Method injection
1- Constructor Injection
In Constructor dependency you need to pass specified values at the time of creation to initiate the object.
public interface IClassA
{
 void DoWork();
}
 
public class ClassA: IClassA
{
 public void DoWork()
 {
  Console.WriteLine("ClassA Called");
 //To Do: Some Stuff
 }
}
 
public class Client
{
 private IClassA _service;
 
 public Client(IClassA service)
 {
  this._service = service;
 }
 
 public void Start()
 {
  Console.WriteLine("ClassA Started");
  this._service.DoWork();
  //To Do: Implement your logic
 }
}
class Program
{
 static void Main(string[] args)
 {
 Client client = new Client(new ClassA());
 client.Start(); 
 Console.ReadKey();
 }
}

2-Property injection(Setter Injection)
Property injection is also called setter Injection. It does not require any modification in the constructor. We are passing an object instance of service class using SET operation.
public interface IService
{
 void Serve();
}
 
public class Service : IService
{
 public void Serve()
 {
 Console.WriteLine("Service Called");
 //To Do: Implement your logic
 }
}
 
public class Client
{
 private IService _service;
 
 public IService Service
 {
 set
 {
 this._service = value;
 }
 }
 
 public void Start()
 {
 Console.WriteLine("Service Started");
 this._service.Serve();
 //To Do: Implement your logic
 }
}
class Program
{
 static void Main(string[] args)
 {
 Client client = new Client();
 client.Service = new Service();
 client.Start();
 
 Console.ReadKey();
 }
}

3-Method injection
In Method injection we pass the service class object instance in Method.
public interface IService
{
 void Serve();
}
 
public class Service : IService
{
 public void Serve()
 {
 Console.WriteLine("Service Called");
 //To Do: Implement your logic
 }
}
 
public class Client
{
 private IService _service;
 
 public void Begin(IService service)
 {
 this._service = service;
 Console.WriteLine("Service Started");
 this._service.Serve();
 //To Do: Implement your logic
 }
}
class Program
{
 static void Main(string[] args)
 {
 Client client = new Client();
 client.Begin(new Service()); 
 Console.ReadKey();
 }
}

0 comments :

Post a Comment

  • Popular Posts
  • Comments