Linq lazy loading

F

fredd00

Hi,

i'm trying to use lazy loading with Linq to sql and related objects

seems like you can only call the child object if the context is still
open, this is not real lazy loading.

here is my actual implementation
my class product has a categories collection (using a one-to-many
relation in .dbml)

I bind a product list to a repeater that need to display
product.category.name (with should be lazy loaded)

-> when I use a linqdataobject as the repeater source , i get the
error that the context is closed
-> when I use a facade that returns a list of products - the
categories are not early nor lazy loaded
public static List<Product> GetProductList()
{
using(context ctx = new context())
{
var list = from p in ctx.products
join c in ctx.categories on p.categoryid equals c.id
select p;
return list.ToList<Product>();
}
}

-> when I use

using (context ctx = new context())
{
repeater.DataSource = from p in ctx.products
repeater.DataBind();
}
this works since the context is open but i don't want to do it in my
web page and this is not using a LinqDataObject

I want to simply call a method that returns a list of product with
lazy loading of the category list.

how can i do this?

thanks
 
F

fredd00

Hi,

i'm trying to use lazy loading with Linq to sql and related objects

seems like you can only call the child object if the context is still
open, this is not real lazy loading.

here is my actual implementation
my class product has a categories collection (using a one-to-many
relation in .dbml)

I bind a product list to a repeater that need to display
product.category.name (with should be lazy loaded)

-> when I use a linqdataobject as the repeater source , i get the
error that the context is closed
-> when I use a facade that returns a list of products - the
categories are not early nor lazy loaded
public static List<Product> GetProductList()
{
using(context ctx = new context())
{
var list = from p in ctx.products
join c in ctx.categories on p.categoryid equals c.id
select p;
return list.ToList<Product>();
}

}

-> when I use

using (context ctx = new context())
{
repeater.DataSource = from p in ctx.products
repeater.DataBind();}

this works since the context is open but i don't want to do it in my
web page and this is not using a LinqDataObject

I want to simply call a method that returns a list of product with
lazy loading of the category list.

how can i do this?

thanks

seems like i found some good info

http://davidhayden.com/blog/dave/archive/2006/06/07/2983.aspx
and
http://davidhayden.com/blog/dave/ar...ataLoadOptionsAvoidingDatabaseRoundtrips.aspx
 
F

fredd00

sorry i was too happy to find something those are about prefetching
and loadingOptions
good info but not exactly what i'm looking for

I want a list of products that if i ask product.category then it will
fetch the data, this after populating my list and releasing my context
object.
 
N

Nicholas Paldino [.NET/C# MVP]

Basically, you are going to have to do the loading yourself. I actually
brought this up with the PM for LINQ-to-SQL at MS, indicating that I don't
want to have to do all of the loading myself, because as the type hierarchy
grows, so do the number of calls I need to place in other places to load the
hierarchy.

I think that you could probably create a reflection-based routine
(assuming you don't have any strong base types which represent related
records) which will access the properties which expose other entities and
therefore, load them.
 
F

fredd00

Basically, you are going to have to do the loading yourself. I actually
brought this up with the PM for LINQ-to-SQL at MS, indicating that I don't
want to have to do all of the loading myself, because as the type hierarchy
grows, so do the number of calls I need to place in other places to load the
hierarchy.

I think that you could probably create a reflection-based routine
(assuming you don't have any strong base types which represent related
records) which will access the properties which expose other entities and
therefore, load them.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


sorry i was too happy to find something those are about prefetching
and loadingOptions
good info but not exactly what i'm looking for
I want a list of products that if i ask product.category then it will
fetch the data, this after populating my list and releasing my context
object.

Hi
thanks for the reply,

so the short answer is there is no real lazy loading if the context is
closed

i would need to implement it in the property
open a new context get the data a maintain it i my object

i don't see why microsoft did not do the extra step , it's not such a
big job but would have been a real implementation of lazy loading

thanks again
 
N

Nicholas Paldino [.NET/C# MVP]

BTW, it should be mentioned that what you want to do isn't really lazy
loading, it's really up-front loading.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

fredd00 said:
Basically, you are going to have to do the loading yourself. I
actually
brought this up with the PM for LINQ-to-SQL at MS, indicating that I
don't
want to have to do all of the loading myself, because as the type
hierarchy
grows, so do the number of calls I need to place in other places to load
the
hierarchy.

I think that you could probably create a reflection-based routine
(assuming you don't have any strong base types which represent related
records) which will access the properties which expose other entities and
therefore, load them.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


sorry i was too happy to find something those are about prefetching
and loadingOptions
good info but not exactly what i'm looking for
I want a list of products that if i ask product.category then it will
fetch the data, this after populating my list and releasing my context
object.

Hi
thanks for the reply,

so the short answer is there is no real lazy loading if the context is
closed

i would need to implement it in the property
open a new context get the data a maintain it i my object

i don't see why microsoft did not do the extra step , it's not such a
big job but would have been a real implementation of lazy loading

thanks again
 
F

fredd00

BTW, it should be mentioned that what you want to do isn't really lazy
loading, it's really up-front loading.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Basically, you are going to have to do the loading yourself. I
actually
brought this up with the PM for LINQ-to-SQL at MS, indicating that I
don't
want to have to do all of the loading myself, because as the type
hierarchy
grows, so do the number of calls I need to place in other places to load
the
hierarchy.
I think that you could probably create a reflection-based routine
(assuming you don't have any strong base types which represent related
records) which will access the properties which expose other entities and
therefore, load them.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

sorry i was too happy to find something those are about prefetching
and loadingOptions
good info but not exactly what i'm looking for
I want a list of products that if i ask product.category then it will
fetch the data, this after populating my list and releasing my context
object.
Hi
thanks for the reply,
so the short answer is there is no real lazy loading if the context is
closed
i would need to implement it in the property
open a new context get the data a maintain it i my object
i don't see why microsoft did not do the extra step , it's not such a
big job but would have been a real implementation of lazy loading
thanks again

you are right in this exemple i should use up-front loading of the
child, which i am now doing

but for some other situation i might want to use lazy loading , and I
wanted to know if it was possible.
Seems like it's not possible unless I code it myself

thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top