architect-handbook

Software Architect Handbook

View on GitHub

Lazy Load

An object that doesn’t contain all of the data you need but knows how to get it.

Overview

Loading an object can have the effect of loading a huge number of related objects (when only a few are actually needed).

A Lazy Load interrupts this loading process for the moment, leaving a marker in the object structure so that if the data is needed it can be loaded only when it is used.

How It Works

Problems

When to Use It

This is about deciding how much you want to pull back from the database as you load an object, and how many database calls that will require.

In performance terms it’s about deciding when you want to take the hit of bringing back the data.

Often it’s a good idea to bring everything you’ll need in one call so you have it in place, particularly if it corresponds to a single interaction with a UI.

The best time to use Lazy Load is when it involves an extra call and the data you’re calling isn’t used when the main object is used.