title: Lazy loading in complex RavenDb objects
body: <p>Looks like we're dealing with a poorly thought out object design issue that is now manifesting itself into significant performance/memory issues.</p>
  
  <p>We have thousands of root aggregate objects stored in a RavenDb database.  For certain large customers, these objects are becoming too large to effectively perform web operations (opening pages, saving data, etc).</p>
  
  <p>Structure is as follows:
  Account object is the aggregate root
  Underneath it, there is a plethora of smaller objects and collections that are all "fine" in size, except one collection called Resources that can grow very large and can cause the root objects to be multiple megabytes in size.  This causes basic CRUD operations on Account and its internal data to perform very slow</p>
  
  <p>Objects in the Resource collection are not huge themselves, but they have children of their own and those drag the size way up.
  Each Resource object has Metrics, Actions, Alerts, Scaling, and other collections that are "heavy"</p>
  
  <p>Our codebase is super complex with hundreds of thousands of lines of code; and hundreds if not thousands of lines of code reference the Resource collection and inspect Resource objects within it, but access to the underlying child collections of each Resource object appears to be infrequent and mostly done one resource at a time</p>
  
  <p><strong>Question</strong>: How do we load the Account object, all of its miscellaneous children and objects, and only the first level of Resource objects, and then <strong>lazy-load</strong> sub-children of Resources? (there are like 7 specific collections that can be lazy-loaded)</p>
  
  <p>We have a single Repository that is responsible for loading/saving of the data</p>