Subject: Object Factories with Lazy Loading in C#
Posted By: cyberjoe Post Date: 12/13/2006 4:39:49 PM
I'm looking for a lazy loading solution in c#.

The idea is, to have a kind of object factory, which will be responsible for creating my entity objects.
When a property of such an object is accesed and it is not yet loaded, the factory
(or a magic watcher) will be able to pause the execution flow for a while (maybe that step is not necessary)
load the missing data (don't know how to specify, from where to load that data----maybe via attributes) and
return to the flow.

My entity objects, do not have a possibility to load the missing data by themselves, because they
are only data containers having private members, which are accessible via public properties.
These objects need to be filled with their data from outside, and therefore many lazy loading solutions
won't work here.

Maybe anyone of you knows the solution for my problem?

Thank You in advance!


Greetz!

 cyberjoe

Reply By: woodyz Reply Date: 12/13/2006 8:39:28 PM
There are lots of ways to accomplish this.  

The class of a member object can be in charge of loading an instance of itself via a static method.  This is known as a factory method.

You can also code up a factory class or set of classes whose task it is to provide objects as you describe via factory methods.

Another popular pattern is the Abstract Factory - but your description of what you need doesn't match this pattern as far as it looks to me.  

One important question that should be addressed is regarding your architecture: Are you remoting anything?  Are objects being created on one machine and transported to another?  That comes in to play because you will need to make sure your objects are serializable if you want to take advantage of the .NET marshal by value feature.

It might be useful for you to look up Rocky Lhotkas CSLA books.  There are versions for C# and VB.NET published by Apress (as well as the older VB6 books that were published by Wrox)

Woody Z
http://www.learntoprogramnow.com
Reply By: Bob Bedell Reply Date: 12/13/2006 10:21:33 PM
 
quote:
My entity objects, do not have a possibility to load the missing data by themselves, because they are only data containers having private members, which are accessible via public properties. These objects need to be filled with their data from outside...


You could invoke a static factory method in the property accessor itself.

Say, for example, the property you want to load is a child collection declared in your object as:

ChildCollection _myChildCollection = null;


You could load this data member upon accessing the property by doing something like:

public ChildCollection MyChildCollection { 
 get { 
   if (_myChildCollection == null) {
      
      // Invoke static factory method defined in ChildCollection. 
     _myChildCollection = ChildCollection.GetChildCollection(this._id); 
   } 
   return _myChildCollection; 
 } 
}







Go to topic 25352

Return to index page 94
Return to index page 93
Return to index page 92
Return to index page 91
Return to index page 90
Return to index page 89
Return to index page 88
Return to index page 87
Return to index page 86
Return to index page 85