Skip to main content

Delayed Loading

  • 3 minutes to read

Collection Properties

The association properties (of the XPCollection type) are always loaded on demand. Collections are filled when their items are accessed for the first time.

using DevExpress.Xpo;

//...

public class ProductLine : XPLiteObject {
    // ...
    public ProductLine(Session session) : base(session) { }

    // Returns a collection of products that correspond to the current product line.
    // This collection is always loaded on demand.
    public XPCollection<Product> Products { get { return GetCollection<Product>(nameof(Products)); } }
}

Persistent Properties

Persistent properties mapped to columns that contain large amounts of data (images, large text documents, or binary data) require a lot of memory. Decorate such properties with the DelayedAttribute to enable delayed loading. XPO does not populate such properties until you call the GetDelayedPropertyValue method.

[Delayed(true)]
public Byte[] BigPicture {
    get { return GetDelayedPropertyValue<Byte[]>(nameof(BigPicture)); }
    set { SetDelayedPropertyValue<Byte[]>(nameof(BigPicture), value); }
}

The DelayedAttribute.UpdateModifiedOnly property specifies whether to include delayed properties in UPDATE statements. Set this property to true (Delayed(true)) to reduce the amount of data transferred between your application and the database server.

If you have multiple delayed properties in a persistent class, use the DelayedAttribute.GroupName property to arrange delayed properties in groups. When you read a property that belongs to a group, XPO loads all properties in the same group simultaneously.

Tip

If you make most properties of your objects delayed, performance probably won’t be significantly improved, and objects may become unnecessary complicated.

Use XPO Profiler to determine the loading time bottlenecks.

Note

  • If a property type is a persistent class, the GroupName property is not taken into account and all properties in a group are loaded separately.
  • Persistent members declared as fields do not support delayed loading.
  • Do not use the DelayedAttribute with association properties. XPO always loads association properties on demand.

Reference Properties

When a property that references a persistent object is loaded, XPO generates and executes multiple queries to retrieve this property. The first query retrieves a reference to the persistent object and the other for each property of the persistent object. It is a recursive process because the retrieved property can reference another persistent object.

To control an object graph that is loaded with reference properties, use ExplicitLoadingAttribute. See the attribute description for more details.

Manage Delayed Loading

  1. You can call the Session.PreFetch method to force the collection and delayed properties to load.

  2. Use the following objects to load data projections instead of entire objects: