Monday, November 24, 2014

Inversion of Control in Seam

Inversion of control, also known by many as dependency injection, takes the need to
instantiate objects away from the normal user calls. Instead, you allow the container to
handle the creation of the component and its subcomponents. Seam is no different in
using dependency injection to inject objects. However, Seam takes it to the next step by
allowing the injection to go both ways.

IoC is needed because of the nature of Seam. Because we don’t use any JSF action
classes to translate our presentation tier requests into objects and then set them on the
EJB and call the EJB, we have to use IoC to make up for it. This helps us save time and
space by letting the container manage the objects.

The usage of IoC in Seam is often referred to as bijection, because the injection is
two-way (injection and “outjection”). You can specify the direction of the components.
However, bijection is so much more in Seam than it is in most typical IoC patterns. Seam
takes IoC and expands it by making it dynamic, contextual, and bidirectional, and allowing
assembly by the container.

So where can you use bijection? Bijection can be used on any object that is a Seam
object. Remember, a Seam object is any object that you have defined with an @Name annotation
on the class. You can then biject your Seam objects into any SB or JavaBean.
However, you cannot biject objects into your EB. This is because the domain model
should be independent of business logic and because EBs are instantiated at the application
level and Seam could not intercept them anyway.

Seam performs bijection by dividing it into two areas: one going into the system and
one going out. As I have said, these are more commonly defined as injection and outjection,
and we use @In and @Out for these, respectively.

No comments: