Description:
The task is: retrieve objects by particular criteria from the database, process them and save them. I don't need to display objects in data bound visual controls. It's a kind of background data processing, where the low memory footprint and performance are critical. Is there a better way to get a list of the objects other than loading them into an XPCollection?
Answer:
The XPCollection is a component with a lot of features. It implements the IBindingList and ITypedList interfaces, supports on-the-fly filtering and sorting and it can be bound to visual controls. Obviously, you don't need this functionality for background data processing. It may make sense to load objects into a simple array rather than to use the XPCollection. We advise you to use the Session.GetObjects method in your task. Below is some sample code.
C#using DevExpress.Xpo;
using DevExpress.Xpo.DB;
using DevExpress.Xpo.Metadata;
using DevExpress.Data.Filtering;
using System.Collections;
void ProcessDiscontinued() {
XPClassInfo productClass;
CriteriaOperator criteria;
SortingCollection sortProps;
using(UnitOfWork uow = new UnitOfWork()) {
// Obtain the persistent object class info required by the GetObjects method
productClass = uow.GetClassInfo(typeof(Product));
// Create criteria to get objects
criteria = new BinaryOperator("Discontinued", true);
// Create a sort list if objects must be processed in a specific order
sortProps = new SortingCollection(null);
sortProps.Add(new SortProperty("Price", SortingDirection.Ascending));
// Call GetObjects
ICollection products = uow.GetObjects(productClass, criteria, sortProps, int.MaxValue, false, true);
// Do processing
foreach(Product product in products) {
// do some action with the product object
Console.WriteLine("{0,-25} {1,8:c} discontinued: {2}", product.Name, product.Price, product.Discontinued);
}
// Save changes if any
//if(uow.InTransaction)
// uow.CommitChanges();
}
}
Visual BasicSub ProcessDiscontinued()
Dim products As ICollection
Dim productClass As XPClassInfo
Dim criteria As CriteriaOperator
Dim sortProps As SortingCollection
Dim session As New UnitOfWork
' Obtain the persistent object class info required by the GetObjects method
productClass = session.GetClassInfo(GetType(Product))
' Create criteria to get objects
criteria = New BinaryOperator("Discontinued", True)
' Create a sort list if objects must be processed in a specific order
sortProps = New SortingCollection(Nothing)
sortProps.Add(New SortProperty("Price", SortingDirection.Ascending))
' Call GetObjects
products = session.GetObjects(productClass, criteria, sortProps, Integer.MaxValue, False, True)
' Do processing
Dim product As Product
For Each product In products
' do some action with the product object
Console.WriteLine("{0,-25} {1,8:c} discontinued: {2}", product.Name, product.Price, product.Discontinued)
Next
' Save changes if any
'If session.InTransaction Then
' session.CommitChanges()
'End If
session.Dispose()
End Sub
See Also:
How to obtain a collection of persistent objects by a set of their IDs