Refresh Objects and Rollback Changes
- 4 minutes to read
Members | Description |
---|---|
Methods: | |
Updates the persistent objects belonging to the current Object Space. | |
Updates the specified object with data from the data source. | |
Cancels the changes made to the persistent objects belonging to the current Object Space. | |
Events: | |
Occurs before refreshing the current Object Space’s persistent objects. | |
Occurs after the current Object Space reconnects to the database. | |
Occurs after an object is reloaded from the database. | |
Replace the default processes of refreshing persistent objects with a custom one. | |
Occurs before rolling back the changes made to the current Object Space’s persistent objects. | |
Replaces the default process of persistent objects rollback with a custom one. |
In your code, you can modify business objects to create new ones, delete objects or edit objects’ properties. If you perform this in an ObjectSpace different from the current View’s ObjectSpace, refresh the current View’s ObjectSpace to show changes in the View. Call the IObjectSpace.Refresh method to show new data in the View.
The example below adds a new Contact object to a List View and refreshes the Object Space to show the new object.
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.Persistent.Base;
using MainDemo.Module.BusinessObjects;
// ...
public class AddContactController : ObjectViewController<ListView, Contact> {
public AddContactController() {
ParametrizedAction addContactAction = new ParametrizedAction(
this, "AddContact", PredefinedCategory.Edit, typeof(string));
addContactAction.Execute += AddContactAction_Execute;
}
private void AddContactAction_Execute(object sender, ParametrizedActionExecuteEventArgs e) {
using(IObjectSpace objectSpace = Application.CreateObjectSpace(typeof(Contact))) {
Contact contact = objectSpace.CreateObject<Contact>();
contact.FirstName = e.ParameterCurrentValue as string;
objectSpace.CommitChanges();
}
View.ObjectSpace.Refresh();
}
}
The following code reloads the parent Detail View when one of the child objects is reloaded.
using DevExpress.ExpressApp;
using YourSolutionName.Module.BusinessObjects;
namespace YourSolutionName.Module.Controllers {
public class MyViewController : ObjectViewController<DetailView, Parent> {
protected override void OnActivated() {
base.OnActivated();
ObjectSpace.ObjectReloaded += ObjectSpace_ObjectReloaded;
}
private void ObjectSpace_ObjectReloaded(object sender, ObjectManipulatingEventArgs e) {
Child child = e.Object as Child;
if (child != null && ReferenceEquals(child.Parent, View.CurrentObject)) {
View.Refresh();
}
}
protected override void OnDeactivated() {
ObjectSpace.ObjectReloaded -= ObjectSpace_ObjectReloaded;
base.OnDeactivated();
}
}
}
To reload an object changed in another Object Space in the current Object Space, use the IObjectSpace.ReloadObject method. The following controller demonstrates how to open a Popup Window to edit an Task object and then, reload this object in the Task List View.
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Editors;
public class EditTaskController : ObjectViewController<ListView, Task> {
public EditTaskController() {
PopupWindowShowAction action = new PopupWindowShowAction(this, "EditTask", DevExpress.Persistent.Base.PredefinedCategory.Edit);
action.SelectionDependencyType = SelectionDependencyType.RequireSingleObject;
action.CustomizePopupWindowParams += Action_CustomizePopupWindowParams;
action.Execute += Action_Execute;
}
private void Action_Execute(object sender, PopupWindowShowActionExecuteEventArgs e) {
e.PopupWindowView.ObjectSpace.CommitChanges();
ObjectSpace.ReloadObject(e.PopupWindowViewCurrentObject);
}
private void Action_CustomizePopupWindowParams(object sender, CustomizePopupWindowParamsEventArgs e) {
IObjectSpace objectSpace = Application.CreateObjectSpace(typeof(Task));
Task task = (Task)objectSpace.GetObject(View.CurrentObject);
DetailView detailView = Application.CreateDetailView(objectSpace, task);
detailView.ViewEditMode = ViewEditMode.Edit;
e.View = detailView;
}
}
When you use a Data Access Mode that does not load real objects (ServerView, DataView, and InstantFeedbackView), use the IObjectSpace.Refresh method to reload a changed object in the current List View.