Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

Exploring the dependency injection principle

Joydip Kanjilal | Aug. 24, 2015
Take advantage of the dependency injection principle to provide support for pluggable implementations in your application and build loosely coupled, testable components

Dependency Injection Principle
Dependency Injection Principle

The Dependency Inversion Principle states that the high level modules in an application should not depend on the low level modules; both should rather depend on abstractions.

Both inversion of control and dependency injection are ways that enable you to break the dependencies between the components in your application. The IOC design pattern states that objects should not create objects on which they depend to perform some activity.

As an example, you would typically have a business layer and a core or framework layer in your application. The business layer code should invoke the framework layer code. Using the IOC principle, this can be inverted, i.e., the framework layer would call the business layer. The IOC containers helps you in automatic instantiation and life cycle management of the objects. Some examples of popular IOC containers include Castle Windsor and Structure Map.

Dependency injection

Dependency injection is a subset of the Inversion of Control (IoC) principle. The DI principle is a technique that is used to remove internal dependencies from the implementation by enabling these dependencies to be injected externally. It states that when an object is dependent on other objects, such objects should be created using a separate framework or component. The IOC containers take advantage of dependency injection to invert the flow of control and provide the necessary implementation or the dependent code. In essence, while IoC is the ability of varying the implementation of a contract, DI is the ability to provide the necessary implementation when asked for. In other words, Dependency Injection is a realization of the Inversion of Control (IoC) principle.

There are basically three types of dependency injection: constructor injection, setter injection, and interface injection.  While constructor injection uses a constructor to inject the dependencies, setter injection take advantage of setter properties to inject the object dependencies. The following code snippet illustrates how constructor injection is used.

public class BusinessLogic

    {

        public BusinessLogic(BusinessEntity entity)

        {

            this.BusinessEntity = entity;

        }

    }

Note how the dependency is passed using the constructor of the BusinessLogic class. interface injection uses interfaces to inject the dependencies.

Implementing interface injection

In this section we would implement dependency injection using interfaces, i.e., using the interface injection technique.

The following code snippet shows three classes: EntityBase, Employee, and Customer. EntityBase is the base of both the entity classes Employee and Customer.

 public abstract class EntityBase

    {

        public Int32 ID

        {

            get;

            private set;

        }

    }

    public class Employee : EntityBase

    {

        //TODO: Properties that correspond to the Employee entity

    } 

    public class Customer : EntityBase

 

1  2  3  Next Page 

Sign up for CIO Asia eNewsletters.