Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Object Factories with Lazy Loading in C#

Object Factories with Lazy Loading in C#

Scheduled Pinned Locked Moved C#
csharphelptutorialquestion
2 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cebyrjoe2
    wrote on last edited by
    #1

    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

    C 1 Reply Last reply
    0
    • C cebyrjoe2

      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

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      cebyrjoe2 wrote:

      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.

      Have you considered using properties?

      public class MyClass
      {
      private bool isLazyLoaded;
      private string someData;

      public string SomeData
      {
          get
          {
              DoLazyLoadIfNecessary();
              return this.someData;
          }
      }
      
      private void DoLazyLoadIfNecessary()
      {
          if (!isLazyLoaded)
          {
              // Do what ever is necessary to get the values.
          } 
      }
      

      }

      Once you have this only the code that does the lazy load will access the lazy loaded fields directly. Everything else goes through the property.

      cebyrjoe2 wrote:

      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.

      I should have read the whole thing before typing out that example - Sod it, I've typed it now so I'm keeping it! :-D Each object will need to know at least an ID so that it can go and request its data from a DAL or some such thing. Many of my objects have lazy load functionality, when they are created all that is known about them is a corresponding database ID. For any lazy load solution to work some sort of identifier needs to be known.


      Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups