Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 13th, 2006, 05:39 PM
Authorized User
 
Join Date: Nov 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Object Factories with Lazy Loading in C#

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

 
Old December 13th, 2006, 09:39 PM
Friend of Wrox
 
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old December 13th, 2006, 11:21 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

 
Quote:
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:

Code:
ChildCollection _myChildCollection = null;
You could load this data member upon accessing the property by doing something like:

Code:
public ChildCollection MyChildCollection { 
 get { 
   if (_myChildCollection == null) {

      // Invoke static factory method defined in ChildCollection. 
     _myChildCollection = ChildCollection.GetChildCollection(this._id); 
   } 
   return _myChildCollection; 
 } 
}









Similar Threads
Thread Thread Starter Forum Replies Last Post
Over-using Lazy properties in GridView? kalel_4444 BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 November 5th, 2008 01:43 AM
Lazy Load... Cool but... pbernardo01 BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 7 December 28th, 2007 02:00 AM
Error in loading the serialized canvas object Dhruv_er16 Windows Presentation Foundation 0 May 23rd, 2007 08:04 AM
Is the Internet making programmers lazy? dparsons Intro Programming 12 April 8th, 2007 10:45 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.