Thông báo


Chia sẻ
Tùy chọn
Xem bài viết cuối
Offline admin  
#1 Đã gửi : 23/02/2016 lúc 11:52:54(UTC)

Danh hiệu: Administration

Chức danh:

Nhóm: Administrators
Gia nhập: 23-07-2013(UTC)
Bài viết: 6,110
Viet Nam
Đến từ: Vietnam

Cảm ơn: 10 lần
Được cảm ơn: 2 lần trong 2 bài viết


The dependency injection pattern one of the most popular design paradigms today. It is process of removing dependency of object which creates the independent business objects. It is very useful for Test Driven Development.

Dependency Injection is mainly for injecting the concrete implementation into a class that is using abstraction i.e. interface inside. The main idea of dependency injection is to reduce the coupling between classes and move the binding of abstraction and concrete implementation out of the dependent class.

Dependency injection can be done in three ways.

  1. Constructor injection
  2. Method injection

  3. Property injection


This tip presents an overview of the dependency injection pattern, the different types of dependency injection, and the advantages and drawback of DI with c# source code examples.

Scenario of Object Dependency

Object dependency means one object needs another object to execute properly, in multitier application (Presentation Tier, Business Logic Tier and Service Tier), the very common scenario is; Presentation Tier dependent on Business Logic and Business Logic requires different services on the basis of end user selection. For instance for Insurance domain, different services could be like ClaimService, AdjudicationService, and PaymentService. If we need some property of Payment service then the Client calls Business object and Business logic requires an object of Service objects so that Business object is dependent on Service object. Business object is coupled with Service object.

Class BusinessLogicImplementation


ClaimService claim=new ClaimService();

AdjudicationService Adjudication=new AdjudicationService ();

PaymentService Payment=new PaymentService();


In the above example class BusinessLogicImplementation dependent (coupled) on different service objects. The tightly coupled objects all most impossible to reuse and implement unit test because of the dependencies.

Definition of DI

The process of injecting (converting) coupled (dependent) objects into decoupled (independent) objects is called Dependency Injection.

Types of Dependency Injection

There are four types of DI:

  1. Constructor Injection

  2. Setter Injection

  3. Interface-based injection

  4. Service Locator Injection

Constructor Injection

Constructor is used to interface parameter that exposed through the parameterized contractor. It injects the dependencies through a contractor method as object creation other classes. The following code sample illustrates the concept, showing the BusinessLogicImplementation and Service classes. Class BusinessLogicImplementation has a constructor with IService interface as parameter of constructor.

Code Implementation Constructor Injection

Service Interface

public interface IService


string ServiceMethod();


Service Interface implementation for Claim Service

public class ClaimService:IService


public string ServiceMethod()


return "ClaimService is running";



Service Interface implementation for Adjudication Service

public class AdjudicationService:IService


public string ServiceMethod()


return "AdjudicationService is running";



Service Interface implementation for Payment Service

public class PaymentService:IService


public string ServiceMethod()


return "PaymentService is running";



Constructor Injection Implementation for all services

public  class BusinessLogicImplementation


private IService client;

public BusinessLogicImplementation(IService client)


this.client = client;

Console.WriteLine("Constructor Injection Injection ==>

Current Service : {0}",client.ServiceMethod());


Consuming Constructor Injection

BusinessLogicImplementation  ConInjBusinessLogic = 

new BusinessLogicImplementation(new ClaimService());

Getter and Setter Injection

Getter and Setter Injection injects the dependency by using default public properties procedure such as Gettter(get(){}) and Setter(set(){}). The following code sample illustrates the concept, a public property Client has get and set properties of IService interface that accepts the reference of different services.

Code Implementation Getter and Setter Injection

Business Logic

//Implementing Getter and Setter Injection 

public class BusinessLogicImplementation


private IService _client;

public IService Client


get { return _client; }

set { _client = value; }


public void testSetterInj()


Console.WriteLine("Getter and Setter Injection ==>

Current Service : {0}", Client.ServiceMethod());



Consuming Getter and Setter Injection

BusinessLogicImplementation ConInjBusinessLogic = new BusinessLogicImplementation();

ConInjBusinessLogic.Client = new ClaimService();


Interface Injection

Interface Injection is similar to Getter and Setter DI, the Getter and Setter DI uses default getter and setter but Interface Injection uses support interface a kind of explicit getter and setter which sets the interface property. The following code sample illustrates the concept, ISetService is a support interface which has method setServiceRunService which set the interface property.

Code Implementation Interface Injection

Business Logic

//Support interface

interface ISetService


void setServiceRunService(IService client);


//Implementing Interface Injection

public class BusinessLogicImplementationInterfaceDI : ISetService


IService _client1;

public void setServiceRunService(IService client)


_client1 = client;

Console.WriteLine("Interface Injection ==>

Current Service : {0}", _client1.ServiceMethod());



Consuming Interface Injection

BusinessLogicImplementationInterfaceDI objInterfaceDI = 

new BusinessLogicImplementationInterfaceDI();

objInterfaceDI= new ClaimService();


Service Locator Injection

Service Locator Injection is also known as Dependency Absorption. It is used to replacement of new operator. It hides the class dependency by invoking methods directly (without creating object). The following code sample illustrates the concept, in the consumption setService method is invoked by class name BusinessLogicImplementation without creating object.

Code Implementation Service Locator Injection

Business Logic

//Implementing Service Locator Injection

public class BusinessLogicImplementation


private static IService _clientLocator;

public static IService getService()


return _clientLocator;


public static void setService(IService clientSL)


_clientLocator = clientSL;



Consuming Service Locator Injection

BusinessLogicImplementation.setService(new PaymentService());

IService client2 = BusinessLogicImplementation.getService();

Console.WriteLine("Service Locator => Current Service : {0}",client2.ServiceMethod());

Advantages of DI

  • Reduces class coupling
  • Increases code reusability
  • Improves code maintainability
  • Improves application testing
  • Centralized configuration


The main drawback of dependency injection is that using many instances together can become a very difficult if there are too many instances and many dependencies that need to be resolved.

Source : codeproject

Ai đang xem chủ đề này?
OceanSpiders 2.0
Chủ đề tương tự
ASP.NET Core Dependency Injection (.NET Core)
Bởi admin 10-12-2018 lúc 10:22:46(UTC)
Dependency Injection vs Inversion of Control (Application Patterns)
Bởi admin 28-04-2017 lúc 03:53:13(UTC)
Dependency Injection with Web API and Autofac (Web API)
Bởi admin 23-02-2016 lúc 05:03:12(UTC)
Dependency Injection (Object-oriented programing)
Bởi admin 22-05-2015 lúc 09:29:14(UTC)
Managing Dependency Injection with Autofac (Object-oriented programing)
Bởi admin 13-04-2015 lúc 05:11:35(UTC)
Simple Dependency Injection (Object-oriented programing)
Bởi admin 13-04-2015 lúc 05:09:07(UTC)
AngularJS Modularization & Dependency Injection (Angular Modules)
Bởi admin 17-03-2015 lúc 10:59:04(UTC)
Dependency Injection (Dependency Injection in Angular)
Bởi admin 16-03-2015 lúc 04:20:57(UTC)
Di chuyển  
Bạn không thể tạo chủ đề mới trong diễn đàn này.
Bạn không thể trả lời chủ đề trong diễn đàn này.
Bạn không thể xóa bài của bạn trong diễn đàn này.
Bạn không thể sửa bài của bạn trong diễn đàn này.
Bạn không thể tạo bình chọn trong diễn đàn này.
Bạn không thể bỏ phiếu bình chọn trong diễn đàn này.

| Cung cấp bởi YAF.NET | YAF.NET © 2003-2020, Yet Another Forum.NET
Thời gian xử lý trang này hết 0.418 giây.