xpo-2024-query-and-shape-data-delayed-loading.md
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)); } }
}
Imports DevExpress.Xpo
'...
Public Class ProductLine : Inherits XPLiteObject
' ...
Public Sub New(ByVal session As Session)
MyBase.New(session)
End Sub
' Returns a collection of products that correspond to the current product line.
' This collection is always loaded on demand.
Public ReadOnly Property Products As XPCollection(Of Product)
Get
Return GetCollection(Of Product)(NameOf(ProductLine.Products))
End Get
End Property
End Class
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); }
}
<Delayed(True)> _
Public Property BigPicture() As Byte()
Get
Return GetDelayedPropertyValue(Of Byte())(NameOf(BigPicture))
End Get
Set(ByVal value As Byte())
SetDelayedPropertyValue(Of Byte())(NameOf(BigPicture), value)
End Set
End Property
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
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.
You can call the Session.PreFetch method to force the collection and delayed properties to load.
Use the following objects to load data projections instead of entire objects: