Pankaj
Pankaj

Reputation: 4503

can i do lazy loading in Nhibernate at runtime using c#

I am new in implement Nhibernete.

If i use XML documents (.hbm.xml files) in Nhibernete, i enable/disable lazy loading in that xml.

Is there any way in Nhibernete where i can set lazy loading at run time?

Upvotes: 1

Views: 525

Answers (1)

LeftyX
LeftyX

Reputation: 35597

I would suggest you not to define lazy loading/eager loading in your hbm file.

You can control everything using QueryOver

Lazy loading:

var order = Session.QueryOver<Domain.Order>()
    .Where(x => x.id == 12)
    .SingleOrDefault();

Eager loading:

Domain.OrderLine orderLine = null;

var order = Session.QueryOver<Domain.Order>()
    .Where(x => x.id == 12)
    .Fetch(x => x.OrderLines).Eager
        .JoinAlias(x => x.OrderLines, () => orderLine, JoinType.LeftOuterJoin)
    .SingleOrDefault();

or

var order = Session.QueryOver<Domain.Order>()
    .Where(x => x.id == 12)
        .Inner.JoinAlias(x => x.OrderLines, () => orderLine)
        .SingleOrDefault();

I would suggest you to read this interesting article.

Upvotes: 2

Related Questions