Showing posts with label DI. Show all posts
Showing posts with label DI. Show all posts

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

Saturday, November 14, 2009

EJB3 – The much improved easy-to-use server-side component model

Earlier EJB versions have been criticized for their heavy-weight components model, but with EJB3 drastic improvements have been made to offer best possible server-side component available in the industry.


The primary goal of EJB3 is to reduce complexity EJB development model and provide ease-of-use and faster development approach to server-side components.

EJB3 has edge over other competing technologies with respect to server-side component model and I have highlighted some of the points below:
1. Annotations – The powerful feature of J2SE 1.5 contributes to the ease of development of EJB components. Using annotations, its “piece-of-cake” to convert a POJO to an Enterprise Bean.

2. Interceptors & Dependency Injection (DI) – DI provides optimization by resource injection during compile-time and frameworks like Spring have been using it for a long time. Utilizing the latest features of Core Java (J2SE 1.5 onwards), DI contributes in optimizing EJB3 development using Interceptors.
Developers can concentrate on business logic only and even log messages, security, etc. can be handled by Interceptors, which can be applied at method level or component level.

3. Elimination of Extra Classes – With EJB3, there is no need to provide Home Interface for Session Beans. Also, required interfaces for persistence entities have been eliminated as well.

4. Replacement of Entity Beans – Heavyweight Entity beans have been replaced by lightweight JPA Entities (POJO based) and it is a major shift in the way persistence has been handled so far. JPA takes the best of all worlds by providing best features of ORM tools (Hibernate, TopLink, IBATIS) , EJB2.1 CMP persistence model and JDOs.

5. Optional Callback Methods – Container Callback methods are optional and developers are no longer need to provide empty methods (general practice earlier) for these methods.

6. POJO Model – EJB3 has finally accepted plain Java objects for local/remote & enterprise bean classes and there is no need to implement any interface or extend any class. Annotations simplifies this process and bean type/client type are indicated by using annotations.

Though industry has already started using other persistence models (specially heavily relying on ORM tools) & POJO based frameworks for server-side component models, EJB3 has come up with new improved and standardized approach and it might be the revival of it.

Best of Luck to EJB3 model!!