DistinctRootEntityResultTransformer in NH 3 Linq

222 views
Skip to first unread message

MichaelK

unread,
Aug 26, 2010, 12:14:19 PM8/26/10
to nhusers
Hello,

I just upgraded to NH 3.0.0 Alpha 2 where previously I was using NH
2.2.4 with Linq addin. One feature I had in my old repository was a
SetResultTransformer(new DistinctRootEntityResultTransformer()) to
ensure unique records when I eagerly fetched values using left joins.
My Repository exposes IQueryable<T>. In NH 3 I am using
Session.Query<T> but how can I add a ResultTransformer to that so its
always there?



Diego Mijelshon

unread,
Aug 26, 2010, 12:23:38 PM8/26/10
to nhu...@googlegroups.com
.Distinct() should do...
 
    Diego





--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.


MichaelK

unread,
Aug 29, 2010, 12:43:18 PM8/29/10
to nhusers
I don't think Distinct() works for what I am talking about. That seems
to add the DISTINCT keyword to the SQL command, but using the
DistinctRootEntityResultTransformer() used to take that sql result and
when hydrating the entities apply the transformer providing only
Distinct entries. I see on the IQuery interface there is a
SetTransformer method. Can I get from IQuery to IQueryable?

On Aug 26, 12:23 pm, Diego Mijelshon <di...@mijelshon.com.ar> wrote:
> .Distinct() should do...
>
>     Diego
>
> On Thu, Aug 26, 2010 at 13:14, MichaelK <mkob...@gmail.com> wrote:
> > Hello,
>
> > I just upgraded to NH 3.0.0 Alpha 2 where previously I was using NH
> > 2.2.4 with Linq addin. One feature I had in my old repository was a
> > SetResultTransformer(new DistinctRootEntityResultTransformer()) to
> > ensure unique records when I eagerly fetched values using left joins.
> > My Repository exposes IQueryable<T>.  In NH 3 I am using
> > Session.Query<T> but how can I add a ResultTransformer to that so its
> > always there?
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "nhusers" group.
> > To post to this group, send email to nhu...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com>
> > .

Diego Mijelshon

unread,
Aug 29, 2010, 1:00:40 PM8/29/10
to nhu...@googlegroups.com
  queryable.AsEnumerable().Distinct().ToList()

will get you the same results as

  query.SetResultTransformer(Transformers.DistinctRootEntity).List()
 
    Diego


To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.

MichaelK

unread,
Aug 29, 2010, 5:08:03 PM8/29/10
to nhusers
I will give that a try, but maybe I should be more explicit as to what
I had before and what IDEALLY I was looking for.

In NH2 with linq provider I exposed from my repository...

IQueryable<T> Query
{
var query = Session.Linq<T>();
query.QueryOptions.RegisterCustomAction(x =>
x.SetResultTransformer(new DistinctRootEntityResultTransformer()));
return query;
}

That always ensured that the Distinct Results were always applied.
This was useful when eagerly fetching a few collections at once (left
joins) which produced a Carteasian product. I still needed to be
careful regarding large results sets being returned but it was nice
knowing you would always get the distinct results.

Now in NH3 I have this, but would like to still apply the Distinct
transformer if possible.

IQueryable<T> Query
{
return Session.Query<T>();
}



On Aug 29, 1:00 pm, Diego Mijelshon <di...@mijelshon.com.ar> wrote:
>   queryable.AsEnumerable().Distinct().ToList()
>
> will get you the same results as
>
>   query.SetResultTransformer(Transformers.DistinctRootEntity).List()
>
>     Diego
>
> > <nhusers%2Bunsu...@googlegroups.com<nhusers%252Buns...@googlegroups.com>

Diego Mijelshon

unread,
Aug 29, 2010, 5:51:28 PM8/29/10
to nhu...@googlegroups.com
OK, that's different.
You might be able to accomplish this using the new extensible Linq generators.

However, having a Distinct in your main repository method looks like a dirty (and somewhat expensive) way to fix a few misbehaving queries that could be fixed individually while adding an unnecesary cost to all other queries.
It's like an empty catch{} statement, but worse.
 
    Diego


To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.

Frans Bouma

unread,
Aug 30, 2010, 3:03:26 AM8/30/10
to nhu...@googlegroups.com
Also, exposing IQueryable outside the repositories is not really a good
idea, if you want to follow Evans' DDD approach: the query isn't executed
when you call the method. This means that the repositories aren't really
repositories but rather query factories.

FB

> OK, that's different.
> You might be able to accomplish this using the new extensible Linq
> generators.
> Check
>
https://nhibernate.svn.sourceforge.net/svnroot/nhibernate/trunk/nhibernate/s
> rc/NHibernate.Test/Linq/CustomExtensionsExample.cs
>
>

<https://nhibernate.svn.sourceforge.net/svnroot/nhibernate/trunk/nhibernate/
> src/NHibernate.Test/Linq/CustomExtensionsExample.cs> However, having a

> <mailto:nhusers%2Bunsu...@googlegroups.com>
> <nhusers%2Bunsu...@googlegroups.com
> <mailto:nhusers%252Buns...@googlegroups.com> >
>
> > > <nhusers%2Bunsu...@googlegroups.com
> <mailto:nhusers%252Buns...@googlegroups.com>
> <nhusers%252Buns...@googlegroups.com
> <mailto:nhusers%25252Bun...@googlegroups.com> >


>
> >
> > > > > .
> > > > > For more options, visit this group at
> > > > >http://groups.google.com/group/nhusers?hl=en.
> >
> > > --
> > > You received this message because you are subscribed to the
> Google Groups
> > > "nhusers" group.
> > > To post to this group, send email to nhu...@googlegroups.com.
> > > To unsubscribe from this group, send email to
> > > nhusers+u...@googlegroups.com

> <mailto:nhusers%2Bunsu...@googlegroups.com>
> <nhusers%2Bunsu...@googlegroups.com
> <mailto:nhusers%252Buns...@googlegroups.com> >


> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/nhusers?hl=en.
>
> --
> You received this message because you are subscribed to the Google
> Groups "nhusers" group.
> To post to this group, send email to nhu...@googlegroups.com.
> To unsubscribe from this group, send email to
> nhusers+u...@googlegroups.com

> <mailto:nhusers%2Bunsu...@googlegroups.com> .

Reply all
Reply to author
Forward
0 new messages