Executing HQL queries in ApplicationService Layer

0 views
Skip to first unread message

sb

unread,
Nov 26, 2009, 5:05:57 AM11/26/09
to S#arp Architecture
Hello!
Would you please explain me is it ok to execute some custom queries in
ApplicationServices Layer.
I have repositories in Data layer. But sometimes I need to retrieve
from DB not whole object but only part of it (or couple of objects of
different classes).
Do I need to create methods in Data layer's repositories for every
single task, or is it ok to create HQL in ApServices?
Thanks!

Dave Harms

unread,
Nov 26, 2009, 11:42:48 AM11/26/09
to S#arp Architecture
I have this requirement frequently. If I'm displaying a list of
records, especially if there's a join involved, I typically don't want
to retrieve the full object graph because a) it's more data than I
need and b) if I update the data I'm going to get the individual
entity from the database anyway, usually via a form.

I started out doing this with HQL, but it's a bit clunky. As I've
become more comfortable with the Criteria API I've found out it will
do everything I want, so I've gone that route.

What I do is create an entity class that matches the result set I'm
expecting back from the query.

List<BrowseRolesDto> = (List<BrowseRolesDto>)
session.Current.CreateCriteria<Role>()
// Add any needed CreateCriteria and Add(Expression calls
here
.SetProjection(Projections.ProjectionList()
.Add(Projections.Property("Role.Id"),"Id")
.Add(Projections.Property("Role.Name"),"Name")
.Add(Projections.Property("Role.Description"),"Description")
)
.SetResultTransformer(new
NHibernate.Transform.AliasToBeanResultTransformer(typeof
(BrowseRolesDto)))
.List<BrowseRolesDto>

The key is the AliasToBeanResultTransformer which populates the DTO
object.

Dave

Dave Harms

unread,
Nov 26, 2009, 11:54:53 AM11/26/09
to S#arp Architecture
Ayende's "Repository is the new Singleton" is also worth a read:
http://ayende.com/Blog/archive/2009/04/17/repository-is-the-new-singleton.aspx

Some good discussion there.

Dave

sb

unread,
Nov 27, 2009, 1:32:54 AM11/27/09
to S#arp Architecture
Thanks for useful answers!
Reply all
Reply to author
Forward
0 new messages