You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Paul Sideleau <pa...@yahoo.com> on 2006/07/03 19:12:27 UTC

Dependency Injection ,POJO Entity, Design Question

I have a design question regarding a POJO entity bean,
dependency injection, and storing the POJO in the
user's session. 

For example, there is a web application where you
update some entity through several "wizard" like
screens. Then, this information is persisted to a
database. The information is stored in a POJO entity
bean that uses a data access object to persist the
data. The access object is set through dependency
injection using JSF's managed bean facility when the
POJO is first needed. The POJO is then stored in the
user's session. 

Since the POJO entity bean is stored in the session,
it must be serializable. However, I feel that the data
access object should not be serializable b/c an
implementation may be holding out to a
java.sql.connection or javax.sql.DataSource. So the
dependency is marked as transient. 

Now, the problem is that the dependency must be
re-injected if the session is deserialized. This can
be done my implementing a
HttpSessionActivationListener. 

Another option that I can think of would be to use a
simple value bean/transfer object to maintain state
and store that in the user's session. Then, create the
entity bean at the end and pass the value bean to the
entity bean. However, several articles and books have
warned against using transfer objects. 

On the other hand, if the POJO entity bean uses a
ServiceLocator by calling a static factory method in
its "save" method, then this problem does not occur.
However, using the service locator pattern makes
testing with mock objects more difficult. 

Which is the best option or is there another option
that I have not thought of?

Thank you. 

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

Re: Dependency Injection ,POJO Entity, Design Question

Posted by Jonathan Harley <jo...@parkplatz.net>.
Paul Sideleau wrote:
> I have a design question regarding a POJO entity bean,
> dependency injection, and storing the POJO in the
> user's session. 
> 
> For example, there is a web application where you
> update some entity through several "wizard" like
> screens. Then, this information is persisted to a
> database. The information is stored in a POJO entity
> bean that uses a data access object to persist the
> data.

If your bean needs its DAO to be injected, I don't think
it's really a POJO. It's coupled to your DAO and I can't
re-use your class without also using your DAO framework.
If you mean that your class is an entity bean in the
EJB sense, then it's definitely not a POJO, being
coupled to that framework too (for some value of EJB).

A real POJO contains business logic and data and doesn't
need to know anything about how it's stored.

> Another option that I can think of would be to use a
> simple value bean/transfer object to maintain state
> and store that in the user's session. Then, create the
> entity bean at the end and pass the value bean to the
> entity bean. However, several articles and books have
> warned against using transfer objects. 

The main objection to DTOs, at least as I understand it,
is that they duplicate domain objects. Use real POJOs
(to store in the database and pass around between layers)
and you're avoiding this issue.

> Which is the best option or is there another option
> that I have not thought of?

I think this is the best option. Rather than your bean knowing
how to store itself, just use your domain class (the bean that
actually gets stored in the database). Store it in the session
and when it's time to save, pass it to a service object which
knows how to persist it. Separation of concerns in this way
makes testing much easier. The POJO can be fully tested with
unit tests, and the service object can be tested with mocks
or just with functional tests that inspect the database after
each operation.


Jon.
-- 
.....................................................................
           Dr Jonathan Harley   .
                                .   Email: jon@parkplatz.net
            Zac Parkplatz Ltd   .   Office Telephone: 024 7633 1375
            www.parkplatz.net   .   Mobile: 079 4116 0423