Skip to main content
.NET 8.0+

XPObjectSpace.GetObjectByKeyAsync<ObjectType>(Object, CancellationToken) Method

Returns the persistent object that has the specified value for its key property.

Namespace: DevExpress.ExpressApp.Xpo

Assembly: DevExpress.ExpressApp.Xpo.v24.2.dll

NuGet Package: DevExpress.ExpressApp.Xpo

Declaration

public Task<ObjectType> GetObjectByKeyAsync<ObjectType>(
    object key,
    CancellationToken cancellationToken = default(CancellationToken)
)

Parameters

Name Type Description
key Object

An object that is the persistent object’s key property value.

Optional Parameters

Name Type Default Description
cancellationToken CancellationToken null

A CancellationToken object that delivers a cancellation notice to the running operation.

Type Parameters

Name Description
ObjectType

The type of an object to search for.

Returns

Type Description
Task<ObjectType>

A Task that returns the searched object. null (Nothing in Visual Basic) if no persistent object is found with the specified key.

Remarks

The GetObjectByKeyAsync method searches the memory for the object that has the specified value for its key property. If such an object is found, it is not reloaded from the database. Otherwise, the object is retrieved from the database.

To get the key property value, use the IObjectSpace.GetKeyValue method.

The following code demonstrates how you can use this method in a View Controller to find and show a Contact the selected DemoTask is assigned to. If this DemoTask is not assigned to any Contact, a Detail View for a new Contact object is shown.

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Xpo;
using System;
using System.Threading;
// ...
public class AsyncAssignedToInfoController : ObjectViewController<ListView, DemoTask> {
    View contactView = null;
    public AsyncAssignedToInfoController() : base() {
        SimpleAction showAssignedToInfoAction = new SimpleAction(this, "Assigned contact's info", "Edit");
        showAssignedToInfoAction.SelectionDependencyType = SelectionDependencyType.RequireSingleObject;
        showAssignedToInfoAction.Execute += showAssignedToInfoAction_Execute;
    }
    async private void showAssignedToInfoAction_Execute(object sender, SimpleActionExecuteEventArgs e) {
        CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
        XPObjectSpace contactObjectSpace = (XPObjectSpace)Application.CreateObjectSpace(typeof(Contact));
        contactView = Application.CreateDetailView(contactObjectSpace, "Contact_DetailView", true);
        e.ShowViewParameters.CreatedView = contactView;
        Contact assignedTo = (Contact)ViewCurrentObject.AssignedTo;
            if (assignedTo != null) {
                object key = ObjectSpace.GetKeyValue(assignedTo);
                object obj = await contactObjectSpace.GetObjectByKeyAsync<Contact>(key,
                    cancellationTokenSource.Token);
                contactView.CurrentObject = obj ?? contactObjectSpace.CreateObject(typeof(Contact));
            }
            else {
                contactView.CurrentObject = contactObjectSpace.CreateObject(typeof(Contact));
            }
        if (contactObjectSpace.IsNewObject(contactView.CurrentObject)) {
            contactObjectSpace.Committed += contactObjectSpace_Committed;
        }
    }
    private void contactObjectSpace_Committed(object sender, EventArgs e) {
        ViewCurrentObject.AssignedTo = ObjectSpace.GetObject(contactView.CurrentObject) as Contact;
    }
}

In the current example, the cancellationToken parameter is used for demonstration purposes. You can skip it or use to cancel an asynchronous operation as shown in the CancellationToken topic.

See Also