Dave Harms
unread,Nov 26, 2009, 11:42:48 AM11/26/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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