views:

331

answers:

4

If I have an object that lazy loads an association with very large objects, is there a way I can do processing at the time the lazy load occurs? I thought I could use AssociateWith or LoadWith from DataLoadOptions, but there are very, very specific restrictions on what you can do in those. Basically I need to be notified when an EntitySet<> decides it's time to load the associated object, so I can catch that event and do some processing on the loaded object. I don't want to simply walk through the EntitySet when I load the parent object, because that will force all the lazy loaded items to load (defeating the purpose of lazy loading entirely).

A: 

Hey,

I don't see any extensibility points for this available; the only thing I can see is in the FK entity there is a Created method on each individual object that gets fired from the constructor...

So the constructor calls created, and personally, I'm not 100% sure that the entity set loading creates each individual object at that time, and fires the event...

HTH.

Brian
Well, that didn't help directly, but it did remind me that PLINQO has an OnLoaded method I can use. Still not perfect, but it may get me past my current problem.
Matt Holmes
A: 

There are a bunch of built in extensibility methods to the datacontext and data classes generated by Linq2SQL.

http://msdn.microsoft.com/en-us/library/bb882671.aspx

http://csainty.blogspot.com/2008/01/linq-to-sql-extending-data-classes.html

Either of these may be able to serve the purpose you need.

Adam
+1  A: 

Subscribe to the ListChanged Event

EntitySet<T> exposes an event called ListChanged which you can use to detect if an item is being added. Evaluate the ListChangedType property of the ListChangedEventArgs. Here's a link to the values available in the ListChangedType enum.

There is no danger of forcing the load to execute as long as you avoid requesting the enumerator.

http://msdn.microsoft.com/en-us/library/system.componentmodel.listchangedtype.aspx

randolphcabral
Will not work, the Load() function on EntitySet, which will enumerate the IEnumerable (=lazy) source, does not raise any ListChangedEvents.
Johannes Rudolph
It must be populating an innerlist when loading. I wonder if the latest Rx (reactive extensions) might help to detect loading. See http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx.
randolphcabral
@randolphcabral: It does indeed populate an inner list, which doesn't raise any ListChanged notifications.
Johannes Rudolph
A: 

You are definitely not bound to use the default EntitySet<> but can use any IList<> collection instead. I did a little reflection on EntitySet<> but have found no hook into the Load() method, which implements enumerating the lazy source of the entity set (it's where the EntitySet actually gets queried and materialized).

Linq To SQL will use the Assign() method to assign an IEnumerable source (which is lazy by default) to your collection. Starting from there, you can implement your own lazy loading EntitySet with a custom hook at the point you first enumerate the source collection (execute the query).

Johannes Rudolph