Tuesday, December 22, 2009

Dependency Injection - Design Paradigm and Available Solutions

Ever since Dependency Injection (or DI) coined by Martin Fowler, it has been praised & accepted by many people and frameworks like Spring has used it as their USP as well.

With EJB3 interceptor model & JSR 330, DI has established further ground with Sun Microsystems likeness towards it.
DI can be defined as:
"DI is a special form of IOC (Inversion of Control) and design paradigm flexible enough to allow external dependencies to be configured/applied dynamically to software architecture."

Martin Fowler has identified 3 types of DI:
  • Type 1 or interface injection, in which the exported module provides an interface that its users must implement in order to get the dependencies at runtime
  •  Type 2 or setter injection, in which the dependent module exposes a setter method that the framework uses to inject the dependency
  •  Type 3 or constructor injection, in which the dependencies are provided through the class constructor
DI is language-agnostic and there are solutions available in PHP, .NET, Python, Ruby, Perl, etc. This article goes further in listing solutions available from Java's perspective.

Available Popular Solutions in Java for DI
  • EJB3 Interceptors (Standardized Model by Sun) 
  • JSR 330: Dependency Injection for Java (recently finalized for future Java releases, led by Google, SpringSource, Sun)
  • Spring framework (one of the earliest framework to promote DI)
  • Google Guice (pronounced as "juice")
  • Apache HiveMind (Services & Configuration Microkernel)
  • Apache Tapestry IOC
  • JBoss Microcontainer
  • Plexus
 Benefits of DI
  • Reduction of dependent code
  • Configuration Flexibility
Drawbacks of DI
  • Excessive usage can lead to complicated/hard-to-debug code
  • Difficult to understand complex DI usage because of dynamic injection

No comments:

Post a Comment