2
votes

I have set up my POCO classes with ICollection for related objects. They are in a WCF Service, so I have decorated them with DataContract/DataMember. I do not use virtual properties for the related objects, because they create a proxy that won't serialize (I get a seemingly unrelated message, "The underlying connection was closed", but when I remove the virtual modifier, that goes away.)

What I am having trouble understanding is how to lazy-load the collections for related objects. I don't think the POCO's can do that for themselves, because they don't have access to the context.

For example, I have a Company class, which has an ICollection<Principals> property. I usually don't want to load all the Principals when I retrieve a Company, but I would like a reference to Company.Principals to go get them. Clearly, Company simply can't do that on its own.

What are folks doing to combine the desires to have (1) POCO objects, (2) typical WCF Serialization, and (3) lazy-loaded related properties?

1
Here's an article from MS that might help: msdn.microsoft.com/en-us/library/dd456855.aspxChris Gessler
You want your Company.Principals to get filled on demand in server or client code?Grzegorz W
@GrzegorzWilczura, those are exactly the two relevant questions. Apparently, Company.Principals must have access to the Context, hence the virtual properties option; I am beginning to see that it simply is not practical to fill Principals on demand client-side. In the real world, however, everyone knows that Company has Principals, so it seems (at least for the client) I'm left with loading the whole object graph, no matter how big it gets. Is that what everyone is doing? Are POCO's really useless, then (in a disconnected case, as with WCF), except in the simplest examples?Kelly Cline

1 Answers

0
votes

Lazy loading requires proxies and virtual navigation properties. If you don't have proxies you must handle loading in different way. For example by using eager loading:

var companies = context.Companies.Include("Principals").ToList();

or with EF 4.1

var companies = context.Companies.Include(c => c.Prinicpals).ToList();

You know which operation should load related principals as well so using eager loading is not a problem. Using lazy loading in WCF service with serialization will always result to load whole object graph.