Skip to main content
.NET 6.0+

UnitOfWork.CommitChangesAsync(AsyncCommitCallback) Method

Asynchronously commits all the changes made to persistent objects to a data store and notifies upon completion.

Namespace: DevExpress.Xpo

Assembly: DevExpress.Xpo.v23.2.dll

NuGet Package: DevExpress.Xpo

Declaration

public object CommitChangesAsync(
    AsyncCommitCallback callback
)

Parameters

Name Type Description
callback AsyncCommitCallback

A AsyncCommitCallback delegate to be called after the changes have been saved. Use the callback to store the exception information passed as a parameter, and use this information later, to raise the exception again, within the original thread. Do not raise exceptions or modify persistent objects within a callback.

Returns

Type Description
Object

An object identifying the current asynchronous operation, intended for internal use.

Remarks

A Unit of Work tracks every change to every persistent object during a transaction that can affect a data store. Its CommitChangesAsync method commits all the changes made to persistent objects to a data store. The only requirement for this is that the property setters call the XPBaseObject.OnChanged method.

Calling the CommitChangesAsync method automatically calls Session.CommitTransactionAsync (in explicit units of work, ExplicitUnitOfWork.CommitTransactionAsync is called).

If a save operation fails (for example, due to a database constraint violation), the following exceptions can be raised by XPO:

Note

In a NestedUnitOfWork, calling the CommitChangesAsync method throws the System.NotSupportedException, since changes within these Units should be saved using only the main thread.

Example

The following example demonstrates how to asynchronously work with persistent objects.

In this example, a collection (xpcPersons) of Person objects is used as a data source for a grid control (gridPersons). The Update and Commit buttons (btnUpdate and btnCommit) allow users to update the grid control and store changes to a data store while continuing to interact with other UI elements. The accessibility of the grid control and these buttons is automatically updated within operation callbacks (CallBackLoadPersons and CallBackCommitChanges).

using DevExpress.Xpo;
using DevExpress.Xpo.DB;

namespace AsyncOperations {

    // A custom XPObject
    class Person : XPObject {
       //...
    }

    public class AsyncLoading {
        UnitOfWork unitOfWork;
        // Initialize unitOfWork here.
        //...

        // An XPCollection to be used as a data source for the gridPersons grid control
        XPCollection xpcPersons;

        //...

        // The Click event handler of the Update button
        private void btnUpdate_Click(object sender, EventArgs e) {
            if (xpcPersons == null) {
                xpcPersons = new XPCollection(unitOfWork, typeof(Person));
                LoadAsyncPersons();
                gridPersons.DataSource = xpcPersons;
            } else {
                LoadAsyncPersons();
            }
        }

        // Disables buttons and the grid control and initiates asynchronous collection loading
        void LoadAsyncPersons() {
            btnUpdate.Enabled = false;
            btnCommit.Enabled = false;
            gridPersons.Enabled = false;
            xpcPersons.LoadAsync(new DevExpress.Xpo.Helpers.AsyncLoadObjectsCallback(
                CallBackLoadPersons));
        }
        // Called after collection loading is complete
        // Re-enables buttons and the grid control
        void CallBackLoadPersons(ICollection[] result, Exception ex) {
            btnUpdate.Enabled = true;
            btnCommit.Enabled = true;
            gridPersons.Enabled = true;
            if (ex != null) {
                MessageBox.Show(ex.Message);
            }
        }

        // The Click event handler of the Commit button
        // Disables buttons and the grid control and asynchronously commits changes
        private void btnCommit_Click(object sender, EventArgs e) {
            btnUpdate.Enabled = false;
            btnCommit.Enabled = false;
            gridPersons.Enabled = false;
            unitOfWork.CommitChangesAsync(new AsyncCommitCallback(CallBackCommitChanges));
        }

        // Called after changes have been saved
        // Re-enables buttons and the grid control
        void CallBackCommitChanges(Exception ex) {
            btnUpdate.Enabled = true;
            btnCommit.Enabled = true;
            gridPersons.Enabled = true;
            if (ex != null) {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
See Also