You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by skow <ry...@skow.org> on 2012/06/07 18:58:26 UTC

Injecting Session into DAO

There is a lot of info out there on Tapestry Injection...perhaps too much. ;) 
I'm having trouble with what I believe to be something fairly simple - the
answer just evades me...

Here is a simplified view of the classes I'm trying to get to work together:

NewUserPage.java  (Tapestry Page backing up the similarly named
NewUserPage.tml)

User.java (Hibernate Entity - business logic goes here)

UserDao.java (Data Access Object - this is where the HQL will be)

Ok, I understand how I can inject the Session from within the NewUserPage -
it is a Component and is injection aware.  Registering the UserDao as a
service and injecting it in the page and then having the page do a bunch of
DAO operations on the Entity seems to be what most examples I've seen do. 
The problem is that I'm stubborn and don't want my 'view' (page) accessing
the DAO directly and prefer it to only interact with the business object
layer.

Below is an example of what I'm trying to do.  Its goal is to verify a
username doesn't already exist when a new user is creating an account.

NewUserPage.java:
    @Property
    private User newUser;
    public void onValidate()
    {
        if(newUser.exists())
        {
            form.recordError("Username exists.  Please choose another.");
        }
    }

User.java:
    private IUserDao dao = new UserDao();
    public boolean exists()
    {
        User existingUser = dao.findByUsername(getUsername());
        return existingUser != null;
    }

UserDao.java:
public class UserDao implements IUserDao
{
    @InjectService("Session")
    Session session;

    public User findByUsername(String username)
    {
        Query query = session.createQuery("from User where
LOWER(username)=LOWER(:username)");
        ...
        return user;
    }
}


The problem is that @InjectService("Session")  in UserDao doesn't seem to do
anything.  I've tried various permutations of @Inject, @InjectService, and
messing around in AppModule, but the session is always null when it goes to
use it to createQuery.

Is the fundamental design of View->BusinessObject->DAO deprecated and needs
to be adjusted for the IoC world of Tapestry?  Hopefully I'm just missing
something simple and the example above can work.

Thanks for any insights you may have!


--
View this message in context: http://tapestry.1045711.n5.nabble.com/Injecting-Session-into-DAO-tp5713703.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Injecting Session into DAO

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Fri, 08 Jun 2012 12:47:43 -0300, Lance Java <la...@googlemail.com>  
wrote:

> This conversation sounds like the "Anemic Domain Model vs. Rich Domain  
> Model" argument where an ADM only contains data and a RDM contains data  
> and
> services.

Hey, I don't use ADM! I explicitly said it sucks! ;)

> I have always used an ADM and like Thiago, I sometimes add
> functions that do not require a database connection.

I always add methods that don't require external dependencies (DAOs,  
controller classes, etc). :) I'd say I use a medium class domain model  
(not anemic nor rich, something in between).

> I have never really
> understood how to inject the session into an RDM without using a factory
> pattern. I once read that someone was able to intercept the constructor  
> for domain objects and inject the session so that a factory pattern was  
> not
> required but I can't seem to find the link for that. It sounded a bit too
> magic to me and when it comes to code, I hate magic ;)

Using Tapestry, you could create a factory service that uses  
ObjectLocator.autobuild() for instantiation and dependency injection  
(supposing there's a constructor that receives the DAOs needed in the  
entity classes.).

-- 
Thiago H. de Paula Figueiredo

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Injecting Session into DAO

Posted by Lance Java <la...@googlemail.com>.
This conversation sounds like the "Anemic Domain Model vs. Rich Domain Model"
argument where an ADM only contains data and a RDM contains data and
services. I have always used an ADM and like Thiago, I sometimes add
functions that do not require a database connection. I have never really
understood how to inject the session into an RDM without using a factory
pattern. I once read that someone was able to intercept the constructor for
domain objects and inject the session so that a factory pattern was not
required but I can't seem to find the link for that. It sounded a bit too
magic to me and when it comes to code, I hate magic ;)

Here's some further reading:
http://www.substanceofcode.com/2007/01/17/from-anemic-to-rich-domain-model/
http://codeflow.blogspot.co.uk/2007/05/anemic-vs-rich-domain-models.html
http://vitamic.wordpress.com/2007/01/04/anemic-domain-model-illustrated/



--
View this message in context: http://tapestry.1045711.n5.nabble.com/Injecting-Session-into-DAO-tp5713703p5713725.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Injecting Session into DAO

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Thu, 07 Jun 2012 16:06:31 -0300, skow <ry...@skow.org> wrote:

> I got the notion that there was a 'Session' service from the code below.

Ah, I've just figured out you were talking about Hibernate's session, not  
the Tapestry Session. :P

> I think that obtaining the DAO from Tapestry-IoC is where I'm getting
> confused.  If the User 'business object' was created with a regular  
> 'new', is it possible for it to obtain a UserDAO from Tapestry-IoC?

I'd create a service for being a factory of User objects (maybe other  
classes too). In this service, you can inject ObjectLocator, which is the  
interface that allows you to get service instances programmatically  
(getService() methods).

> Related to the initial architecture - I certainly respect your point of  
> view on the Entities using DAOs directly.  Although I don't want to go  
> too off
> topic, but I am interested in your preferred architecture.  Do you use a
> separate 'business object' and treat the Entity as only a ValueObject  
> with getters/setters?

I use a separate object, which some people call controller class (not the  
controller from MVC). I think that imposing entity classes as purely value  
objects suck, being something very not OOP.

My rationale is this: I put inside the entity all the logic that can be  
implemented without using DAOs and services. In other words, all logic  
that can implemented by using the object state (fields) Example: an Order  
class would have a field containing a list of OrderItem's, each one with  
its value. The getTotalValue() method would be place in Order, as this  
class has all the info it needs to do that. Logic which doesn't fit in  
this model go to controller classes. I typically have one for each entity  
class.

-- 
Thiago H. de Paula Figueiredo

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Injecting Session into DAO

Posted by skow <ry...@skow.org>.
Thank you for the reply Thiago - it is much appreciated!  A couple follow-up
items:

I got the notion that there was a 'Session' service from the code below. 
When put in AppModule.java, it seems to work just fine when the DAO is
directly injected in a Tapestry Page:

    public static IUserDao buildIUserDao(@InjectService("Session") Session
session)
    {
        IUserDao dao = new UserDao(session);
        return dao;
    }

I think that obtaining the DAO from Tapestry-IoC is where I'm getting
confused.  If the User 'business object' was created with a regular 'new',
is it possible for it to obtain a UserDAO from Tapestry-IoC?  If it is
possible, do you have a code snippit as an example?

Related to the initial architecture - I certainly respect your point of view
on the Entities using DAOs directly.  Although I don't want to go too off
topic, but I am interested in your preferred architecture.  Do you use a
separate 'business object' and treat the Entity as only a ValueObject with
getters/setters? 

Thanks again!

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Injecting-Session-into-DAO-tp5713703p5713707.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Injecting Session into DAO

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Thu, 07 Jun 2012 13:58:26 -0300, skow <ry...@skow.org> wrote:

> The problem is that @InjectService("Session")  in UserDao doesn't seem  
> to do anything.

That's because there's no Session service. Just inject Request and use one  
of the getSession() methods. And don't forget that UserDao should be a  
Tapestry-IoC service and instances of it (UserDao) must be obtained from  
Tapestry-IoC in one way of another (@Inject or some ObjectLocator or  
Registry method).

> I've tried various permutations of @Inject, @InjectService, and messing  
> around in AppModule, but the session is always null when it goes to use  
> it to createQuery.
>
> Is the fundamental design of View->BusinessObject->DAO deprecated and  
> needs to be adjusted for the IoC world of Tapestry?

No. I wouldn't architect my code like yours (I don't like the idea of  
entity classes using DAOs themselves), but the answer is still no.

-- 
Thiago H. de Paula Figueiredo

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org