tags:

views:

420

answers:

2

As I am learning JSF2, I realized I am not sure what the backing components should be. From design point of view, what is the difference between EJBs and @ManagedBeans?

In the end I am going to use JPA, so EJB is a natural choice for business layer. Is it a good practice to use EJB directly from JSF (as explained here)?

At the moment I'm leaning towards using @ManagedBeans for components which don't need access to business layer (e.g. view helpers) or deal with request/session data. For other purposes, e.g. listing something in a grid, I would directly access EJB.

Is this a good design? Shall I use @ManagedBeans for all backing beans for the sake of clean layer separation, even if in some cases they only delegate to EJB?

+2  A: 

Interesting article, didn't knew about that. However, to me this article smells more like a rant towards JSF managed beans. It also tight-couples EJB with JSF. It's maybe interesting in small (personal) applications, but likely not in real world SOA applications where you'd like to have full control over the EJB's been serviced.

As to which one to use for what, I would just stick to @ManagedBean to denote a JSF model --which is tied to one or more specific JSF views. You can perfectly use @EJBs for non-JSF specific business logic and/or datamodels. You can inject @EJBs in a JSF model and delegate it in action methods and maybe also getters/setters, but you should not do the other way round, i.e. do not define the JSF model in an @EJB, this leads to tight coupling and would make @EJBs unuseable outside the JSF context. As far, your design sounds good, as long as you watch to not import anything from javax.faces package in the @EJB class.

BalusC
JSF model, validators, request handling - sure. What about doing only this in @ManagedBean and all actual logic in @EJB? It does not need to mean tight coupling: @ManagedBean is considered part of presentation layer, but not everything has to pass through them. I could still avoid dependencies from @EJB to JSF and access remote services. Just trying to differentiate between the two (overlapping?) bean types.
Konrad Garus
+1  A: 

Very valid question, but I guess the answer depends on the "strictness" of the approach for your project. There is indeed a bit of redundancy between JSF backing bean and EJB as both implement some business logic.

In a ideal usage of JSF features with converters, rendered, validator, etc. the backing bean could indeed be clean business logic code. But in practice some presentation-related logic frequently leaks in it.

This presentation-related logic should ideally not be in a EJB. This presentation-related logic may depend on the faces package, but not necessary. It's what it does that make it presentation-related or not.

A unified component model has some advantages though. And it's the approach taken by Seam and Spring. In both case, business component with declarative transaction, etc. can be used directly in JSF (Spring does not use EJB but provide a similar model). EJB purist would however say that you should dissociate the two.

So to me it's ultimately a question of taste and size of the project. I can imagine that for a small/medium project using EJB in JSF works fine. For bigger project where this strictness is critical, make sure you don't screw the layers.

ewernli
Thank you, it does shed some light. Actually, I already know Spring and wanted to try the Sun stack for a change. I'm trying to differentiate between the two bean types. I would hate to mix the logic between EJB and ManagedBeans, so I'm considering treating ManagedBeans only as view helpers/entry points and doing all logic in EJB. The unified model does have some advantages when it comes to such questions.
Konrad Garus
I find this answer more inspiring. Nonetheless there's still a lot to learn.
Konrad Garus