🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
Delayed Fetch
This page was created by Hans.karlsen on 2017-02-26. Last edited by Wikiadmin on 2026-07-29.

You can use DelayedFetch on an attribute when you need to work with its owning object without loading that attribute's value until your application accesses it; this is intended for modelers handling large values such as documents and images.

What DelayedFetch does

Set an attribute's DelayedFetch property to true when the attribute can contain substantial data that is not needed every time its object is loaded.

For example, an OrderAttachment object might have these attributes:

Attribute Purpose DelayedFetch
FileName Shows the name of the attachment in a grid. No
Document Holds the large document Blob. Yes

When MDriven fetches an OrderAttachment, it can retrieve the attachment without retrieving Document. The document value is fetched when code, an expression, or a view accesses Document.

Configure an attribute for delayed fetch

  1. In MDriven Designer, select the attribute that holds the potentially large value. For example, select OrderAttachment.Document.
  2. Set the attribute's DelayedFetch property to true.
  3. Save the model.
  4. Review every ViewModel that displays collections of this class. Remove direct expressions that read the delayed attribute where the value is not required.

The setting applies to the attribute. It does not mean that a view can refer to the attribute without loading it.

Important: accessing the attribute triggers the fetch

DelayedFetch defers retrieval; it does not prevent retrieval. Any direct access to the attribute requests its value.

For example, this grid-column expression requires the document value for every displayed row:

self.Document

If a grid has 100 attachment rows, that expression defeats the purpose of delaying the document data: the view needs Document for all 100 rows.

Instead, show an inexpensive metadata attribute such as the file name:

self.FileName

Then provide a download action or link that retrieves the document only when the user chooses that attachment.

Use DelayedFetch for documents in grids

A common use case is a detail grid containing many attachments. Users need to see which files are available, but they normally download only one file at a time.

  1. Keep the document Blob attribute, such as Document, marked as DelayedFetch.
  2. Include metadata needed by the grid, such as FileName, as normal ViewModel columns.
  3. Do not add a grid expression that renders or otherwise reads Document.
  4. Add a download interaction that identifies the selected object and document attribute without first loading the Blob into the grid.

For Turnkey and Angular views, MVC GetImage can construct a link to download an attribute without the delayed attribute already being loaded. It accepts an object identity and Blob attribute name. Follow Using BlobDownloadLink for the download-link pattern.

This arrangement lets a user see a list such as Contract.pdf, Drawing.png, and Specification.docx while the large file contents remain unloaded until a download is requested.

DelayedFetch and ViewModel fetching

QueryPlan statically analyzes a ViewModel to determine the data that the view will use. It aims to fetch view data efficiently and avoid repeated lazy fetches.

Therefore, DelayedFetch does not override a ViewModel expression that uses the attribute. If the QueryPlan or a ViewModel expression includes self.Document, MDriven knows the view needs that value and fetches it. Marking Document as delayed does not change that requirement.

Use this rule when reviewing a ViewModel:

ViewModel usage Result for a delayed attribute Recommended approach
The attribute is not referenced. Its value remains deferred. Use this for list and grid views that only need metadata.
The attribute is referenced in a column, derivation, or other expression. The value is needed and is fetched. Remove the reference if the view does not need the value.
The user selects one object and opens a dedicated document control. The selected object's value is fetched when that control accesses it. Use this when viewing or editing one document is intentional.

For broader guidance on how ViewModels reduce database round trips, see Documentation:Efficient ViewModel fetch. To inspect what a ViewModel plans to fetch, use the QueryPlan tools described in Documentation:QueryPlan.

Verify the result

After changing an attribute and its ViewModel usage:

  1. Open the ViewModel in the client with a collection containing several objects.
  2. Inspect the QueryPlan to confirm that the large attribute is not listed for the grid level.
  3. In Turnkey debugging, enable PMapper logging and load the ViewModel as described in Documentation:QueryPlan.
  4. Select or download one item and confirm that only that interaction requires the document attribute.

If the large attribute still appears in the plan, find the ViewModel column, nesting, derivation, or control expression that reads it. The DelayedFetch setting cannot defer data that the active view explicitly requests.

When to use DelayedFetch

Use DelayedFetch when all of the following are true:

  • The attribute can be large, such as a document Blob or image.
  • Most views of the owning object do not need the attribute value.
  • You can show metadata or an explicit interaction instead of the value itself.
  • Fetching the value later, when a user opens or downloads it, is acceptable.

Do not use it as a substitute for ViewModel design. A view that needs an attribute for every row must still retrieve it. Keep large values out of collection-level expressions and use efficient ViewModel fetching for the remaining view data.

See also