Set Fetch Mode on child properties

24 views
Skip to first unread message

markharrison

unread,
Mar 27, 2009, 8:14:40 AM3/27/09
to NhLambdaExtensionsUsers
Hi,

I have just started using Lambda extensions, and it is making my code
a lot nicer, without hard coded strings for aliases.

I have an object whereby I would like to eager load all of its
properties, including an object which contains another object. I will
display with an example:

Class Orchard
{
public virtual IList<Tree> AppleTrees{ get; set; }
}

Class Tree
{
public virtual Fruit fruit{ get; set; }
}

Class Fruit
{
public virtual int Seeds { get; set; }
}

If I wanted to eager load just the list of trees within orchard, I can
do this:
DetachedCriteria.For<Orchard>()
.SetFetchMode<Orchard>(x => x.AppleTrees,
FetchMode.Eager)

How would I be able to eager load the Fruit too? e.g.
DetachedCriteria.For<Orchard>()
.SetFetchMode<Orchard>(x => x.AppleTrees.fruit,
FetchMode.Eager

The alias I am trying to code in a lambda is the string equivalent of:
"AppleTrees.fruit"

Is this possible?

Thanks
Mark

Richard Brown (GMail)

unread,
Mar 27, 2009, 8:47:50 AM3/27/09
to NhLambdaExtensionsUsers
Hi Mark,

The key thing is to figure out how you would do it without the extensions.
I think you need to use an alias to do this in regular ICriteria using
something like:

DetachedCriteria before =
DetachedCriteria.For<Orchard>()
.CreateAlias("AppleTrees", "treesAlias")
.SetFetchMode("AppleTrees", FetchMode.Eager)
.SetFetchMode("treesAlias.Fruit", FetchMode.Eager);


I think the extensions equivalent would be:

Tree treesAlias = null;
DetachedCriteria after =
DetachedCriteria.For<Orchard>()
.CreateAlias<Orchard>(o => o.AppleTrees, () =>
treesAlias)
.SetFetchMode<Orchard>(o => o.AppleTrees,
FetchMode.Eager)
.SetFetchMode(() => treesAlias.Fruit, FetchMode.Eager);


Let me know if that works.

Cheers,
Richard

--------------------------------------------------
From: "markharrison" <harriso...@gmail.com>
Sent: Friday, March 27, 2009 12:14 PM
To: "NhLambdaExtensionsUsers" <nhlambdaext...@googlegroups.com>
Subject: Set Fetch Mode on child properties

markharrison

unread,
Mar 27, 2009, 9:49:44 AM3/27/09
to NhLambdaExtensionsUsers
Hi Richard,

Thanks for the quick reply. Taking the example below, I have managed
to make it work on my business model, but only by making sure the Tree
alias object is called the same as the list of trees in the Orchard
object itself.

Your below almost works, but I have to alter a couple of lines where
the alias is referenced:

Tree AppleTrees = null;
DetachedCriteria after =
DetachedCriteria.For<Orchard>()
.CreateAlias<Orchard>(o => o.AppleTrees, () =>
AppleTrees)
.SetFetchMode<Orchard>(o => o.AppleTrees,
FetchMode.Eager)
.SetFetchMode(() => AppleTrees.Fruit,
FetchMode.Eager);

Can you see any reason for this? It seems odd that I have to have a
variable with the same name, as its not really being used, other than
to get the name of the alias correct.

I assume that it is because nHibernate can't find anything called
treeAlias in the database, but don't see why it needs the alias to be
the same as the object.

Thanks
mark



On Mar 27, 12:47 pm, "Richard Brown \(GMail\)"
<fluke...@googlemail.com> wrote:
> Hi Mark,
>
> The key thing is to figure out how you would do it without the extensions.
> I think you need to use an alias to do this in regular ICriteria using
> something like:
>
>             DetachedCriteria before =
>                 DetachedCriteria.For<Orchard>()
>                     .CreateAlias("AppleTrees", "treesAlias")
>                     .SetFetchMode("AppleTrees", FetchMode.Eager)
>                     .SetFetchMode("treesAlias.Fruit", FetchMode.Eager);
>
> I think the extensions equivalent would be:
>
>             Tree treesAlias = null;
>             DetachedCriteria after =
>                 DetachedCriteria.For<Orchard>()
>                     .CreateAlias<Orchard>(o => o.AppleTrees, () =>
> treesAlias)
>                     .SetFetchMode<Orchard>(o => o.AppleTrees,
> FetchMode.Eager)
>                     .SetFetchMode(() => treesAlias.Fruit, FetchMode.Eager);
>
> Let me know if that works.
>
> Cheers,
>     Richard
>
> --------------------------------------------------
> From: "markharrison" <harrisonmeis...@gmail.com>

Richard Brown (GMail)

unread,
Mar 27, 2009, 10:00:14 AM3/27/09
to NhLambdaExtensionsUsers
If you use the 'before' version (with the strings) does it work?

The extensions are merely converted to strings behind the scenes, so if it
works with strings, it should work with the type-safe variables. (in fact
it should be identical)

i.e., does the following work:

>> DetachedCriteria before =
>> DetachedCriteria.For<Orchard>()
>> .CreateAlias("AppleTrees", "treesAlias")
>> .SetFetchMode("AppleTrees", FetchMode.Eager)
>> .SetFetchMode("treesAlias.Fruit", FetchMode.Eager);

--------------------------------------------------
From: "markharrison" <harriso...@gmail.com>
Sent: Friday, March 27, 2009 1:49 PM
To: "NhLambdaExtensionsUsers" <nhlambdaext...@googlegroups.com>
Subject: Re: Set Fetch Mode on child properties

markharrison

unread,
Mar 27, 2009, 10:13:09 AM3/27/09
to NhLambdaExtensionsUsers
Hi Richard

The before version also doesn't work, however the String equivalent of
the lambda extension does e.g.

DetachedCriteria before =
DetachedCriteria.For<Orchard>()
.CreateAlias("AppleTrees", "AppleTrees")
.SetFetchMode("AppleTrees", FetchMode.Eager)
.SetFetchMode("AppleTrees.Fruit",
FetchMode.Eager);


very strange behaviour...

Mark.

On Mar 27, 2:00 pm, "Richard Brown \(GMail\)"
<fluke...@googlemail.com> wrote:
> If you use the 'before' version (with the strings) does it work?
>
> The extensions are merely converted to strings behind the scenes, so if it
> works with strings, it should work with the type-safe variables.  (in fact
> it should be identical)
>
> i.e., does the following work:
>
> >>             DetachedCriteria before =
> >>                 DetachedCriteria.For<Orchard>()
> >>                     .CreateAlias("AppleTrees", "treesAlias")
> >>                     .SetFetchMode("AppleTrees", FetchMode.Eager)
> >>                     .SetFetchMode("treesAlias.Fruit", FetchMode.Eager);
>
> --------------------------------------------------

Richard Brown (GMail)

unread,
Mar 27, 2009, 10:16:17 AM3/27/09
to NhLambdaExtensionsUsers
Ah, if the before version doesn't work then it's probably my bad on the
NHibernate ICriteria syntax.

If you can get it working with strings, then it should be possible to turn
it into a type-safe expression.

--------------------------------------------------
From: "markharrison" <harriso...@gmail.com>
Sent: Friday, March 27, 2009 2:13 PM

markharrison

unread,
Mar 27, 2009, 10:24:39 AM3/27/09
to NhLambdaExtensionsUsers
Cool-thanks, I have got a version of the source, so I'll take a look
to see what it does in there.

Thanks for all the help
Mark.

On Mar 27, 2:16 pm, "Richard Brown \(GMail\)"
<fluke...@googlemail.com> wrote:
> Ah, if the before version doesn't work then it's probably my bad on the
> NHibernate ICriteria syntax.
>
> If you can get it working with strings, then it should be possible to turn
> it into a type-safe expression.
>
> --------------------------------------------------
Reply all
Reply to author
Forward
0 new messages