Just another WordPress.com site

Archive for February, 2012

Interface Dependecy Injection : A way of loose coupling in C#

To know more about Dependency Injection please read my previous post. Dependency injection can be implement by three possible ways. One of these Interface Injection.

As we know in C# an interface object can have reference of any of the class, implementing the same interface. This is the base behind the Interface Dependency Injection.

Interface injection allows to pass the dependency into dependent object in the form of interface, implemented by dependent class.

Or in easy words, Dependency declared inside the dependent class  in the form of Interface. So any class that implements dependency interface, can be substituted into dependent class.

Lets look an example:-

IDependent interface :- This interface defines the methods that inject one or more dependency into dependent class.

Dependent Class :- The class, that implement the IDependent interface.

IDependency interface :- This interface includes all dependency members that can be called from the Dependent class. By mean of IDependency interface, we can easily inject any of the class that implements IDependency interface.

Dependency Class :- Any class that implements IDependency interface use to substitute into dependent class using Interface injection.

Lets look into simple example:-


using System;
using System.Collections.Generic;
using System.Text;
namespace InterfaceInjectionSample {

 // Project entity class
 public class Project {

private string title;
 private int id;
public Project(string title, int id) {
   this.title = title;
   this.id = id;
}

 public string Title {
   get {
    return title;
   }
   set {
    title = value;
   }
 }

 public int ID {
   get {
    return id;
   }
   set {
    id = value;
   }
 }

}

// Project Business Layer class
public class ProjectBL{

  IProjectDL _projectDL;

  public ProjectBL(IProjectDL projectDL){
    _projectDL=projectDL;
  }

  public Project GetProjectById(int id){
    return _projectDL.GetProjectById(id);
  }

  public List<Project> GetProjects(){
    return _projectDL.GetProjects();
  }
}

public interface IProjectDL{
  public Project GetProjectById(int id);
  public List<Project> GetProjects();
}

// Project Data Layer to interact with database.
public class ProjectDL : IProjectDL{

  public Project GetProjectById(int id){
    Project project=null;
    // logic to access database and return the mapped data object to project entity.
    return project;
  }

  public List<Project> GetProjects(){
    List<Project> projects=null;
    // logic to access database and return the mapped data object to project entity.
    return projects;
  }

 }
}

Here Project BL class depends on IProjectDL interface to perform any operations on data layer. Since IProjectDL object can reference to any class implementing IProjectDL like ProjectDL.

By using interface dependency injection we can insert the any interface dependency.

Lets we have another implementation of IProjectDL as below:


// Another implementation of Project data layer.
// Lets assume its to interact with other database like oracle.
public class ProjectDLForOracle : IProjectDL{

  public Project GetProjectById(int id){

    Project project;

    // logic to get project from oracle database.

    return project;

  }

  public List<Project> GetProjects(){

    List<Project> projects;

    // logic to get projects from oracle database.

    return projects;

  }

}

So ProjectBL just depend on IProjectDL implementation.  We can pass either ProjectDL or ProjectDLForOracle without changing in ProjectBL class.

Many dependency injection container available to automate and configurable this. Enterprise library also provides the policy injection block. By means of this block we can map interface to its implementing class. Policy injection block will read this configuration and inject the corresponding class instance into dependent class.

In this way BL layer classes loosely coupled with DL. We can easily change the one data layer implementation with other one.

So at the end i want to conclude about the whole content.

If dependency in the form of interface its called “Interface Dependency”.

Injecting the interface dependency reference by either mean is called “Interface Dependency Injection”.

I hope this article will helpful for novice to understand the concept.