Back to Devexpress

IObjectSpace.CommitChanges() Method

expressappframework-devexpress-dot-expressapp-dot-iobjectspace.md

latest9.2 KB
Original Source

IObjectSpace.CommitChanges() Method

Saves all the changes made to the persistent objects belonging to the current Object Space to the database.

Namespace : DevExpress.ExpressApp

Assembly : DevExpress.ExpressApp.v25.2.dll

NuGet Package : DevExpress.ExpressApp

Declaration

csharp
void CommitChanges()
vb
Sub CommitChanges

Remarks

When working with persistent objects, the changes that you make are not saved immediately. Each object change is tracked. To save all the tracked changes, the CommitChanges method is called. After calling this method, the track list is emptied and the IObjectSpace.IsModified property is set to false.

In default scenarios, this method is automatically called. But all custom manipulations that you perform with persistent objects must be saved manually via this method.

When implementing the IObjectSpace interface in the BaseObjectSpace class’s descendant, override the DoCommit method. It is invoked by the BaseObjectSpace.CommitChanges method, in which the following events raised:

In the DoCommit method, force saving changes using a container for in-memory objects (see UnitOfWork.CommitChanges in XPO).

Note

An Object Space object commits only the objects that were created with its help. Otherwise, an exception is raised.

The following example uses a Parametrized Action to search for a Person by LastName , and then assigns all deferred tasks to that person.

csharp
using System.Collections.Generic;
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.Base.General;
using DevExpress.Persistent.BaseImpl;
using MainDemo.Module.BusinessObjects;
// ...
public class AssignTasksController : ViewController {
    public AssignTasksController() {
        TargetObjectType = typeof(DemoTask);
        ParametrizedAction assignTasksAction = new ParametrizedAction(
            this, "AssignTasks", PredefinedCategory.Edit, typeof(string));
        assignTasksAction.Execute += AssignTasksAction_Execute;
    }
    private void AssignTasksAction_Execute(object sender, ParametrizedActionExecuteEventArgs e) {
        using(IObjectSpace objectSpace = Application.CreateObjectSpace(typeof(Person))) {
            string personParamValue = e.ParameterCurrentValue as string;
            Person person = objectSpace.FirstOrDefault<Person>(p => p.LastName.Contains(personParamValue));
            if(person != null) {
                CriteriaOperator taskCriteria = CriteriaOperator.Parse("[Status] = ?", TaskStatus.Deferred);
                IList<DemoTask> tasks = objectSpace.GetObjects<DemoTask>(taskCriteria);
                foreach(DemoTask task in tasks) {
                    task.AssignedTo = person;
                }
                objectSpace.CommitChanges();
            }
        }
        View.Refresh(true);
    }
}

The following code snippets (auto-collected from DevExpress Examples) contain references to the CommitChanges() method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

XAF_logon-form-manage-users-register-a-new-user-restore-a-password/CS/Security.Extensions/Services/RestorePasswordService.cs#L56

csharp
user.CreateUserLoginInfo(LoginProviderName, serializedProtectedToken);
nonSecuredObjectSpace.CommitChanges();
return $"{navigationManager.BaseUri}LoginPage?restorePasswordToken={serializedProtectedToken}";

XAF_how-to-show-filter-dialog-before-listview/CS/XPO/DialogBeforeListView/DialogBeforeListView.Module/DatabaseUpdate/Updater.cs#L20

csharp
CreateEmployee(40);
    ObjectSpace.CommitChanges();
}

xaf-how-to-import-data-in-xaf/CS/XPO/ImportData/ImportData.Module/Controllers/ImportDataListViewController.cs#L61

csharp
importDataDelegate(nestedImportObjectSpace, masterObject);
nestedImportObjectSpace.CommitChanges();

xaf-how-to-show-persistent-objects-in-a-non-persistent-objects-view/CS/EFCore/ComplexDialogEF/ComplexDialogEF.Module/Controllers/MyController.cs#L40

csharp
}
    os.CommitChanges();
}

xaf-how-to-implement-a-singleton-class/CS/EF/SingletonSolutionEF/SingletonSolutionEF.Module/DatabaseUpdate/Updater.cs#L23

csharp
}
    ObjectSpace.CommitChanges();
}

connect-winforms-grid-to-backend-using-middletier-server/VB/DataModel.Shared/DatabaseUpdate/Updater.vb#L46

vb
End Sub)
    ObjectSpace.CommitChanges()
End If

xaf-how-to-implement-a-security-system-user-based-on-an-existing-business-class/VB/EmployeeAsUserExample.Module/DatabaseUpdate/Updater.vb#L46

vb
End If
    ObjectSpace.CommitChanges()
End Sub

XAF-CRUD-for-Non-Persistent-Objects-Stored-Remotely/VB/NonPersistentObjectsDemo.Module/DatabaseUpdate/Updater.vb#L28

vb
'CreateSecurityObjects();
    ObjectSpace.CommitChanges() 'This line persists created object(s).
End Sub

XAF_Non-Persistent-Objects-Filtering-Demo/VB/NonPersistentObjectsDemo.Module/DatabaseUpdate/Updater.vb#L54

vb
userAdmin.Roles.Add(adminRole)
    ObjectSpace.CommitChanges() 'This line persists created object(s).
End Sub

XAF_How-to-get-role-code-from-the-UI/VB/XafSolution.Module/DatabaseUpdate/Updater.vb#L31

vb
CreateAdmin()
    ObjectSpace.CommitChanges()
End Sub

See Also

ObjectSaving

ObjectSaved

ObjectChanged

Delete

IObjectSpace Interface

IObjectSpace Members

DevExpress.ExpressApp Namespace