Back to Devexpress

IObjectSpace.Refresh() Method

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

latest6.9 KB
Original Source

IObjectSpace.Refresh() Method

Updates the persistent objects belonging to the current Object Space.

Namespace : DevExpress.ExpressApp

Assembly : DevExpress.ExpressApp.v25.2.dll

NuGet Package : DevExpress.ExpressApp

Declaration

csharp
bool Refresh()
vb
Function Refresh As Boolean

Returns

TypeDescription
Boolean

true if persistent objects were refreshed; otherwise, false.

|

Remarks

Use this method to update the current Object Space‘s persistent objects.

The following code snippet adds a new Contact object to a List View and refreshes the Object Space to show the new object.

csharp
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();
    }
}

When you implement the IObjectSpace interface in a class that descends from BaseObjectSpace, override the protected virtual BaseObjectSpace.Reload method. The BaseObjectSpace.Refresh method calls Reload.

BaseObjectSpace.Refresh works as follows:

If no objects in the current Object Space have changed after you retrieve them or after the last commit (see IObjectSpace.CommitChanges), BaseObjectSpace.Refresh recreates the in-memory object container. It calls the Reload method, retrieves the objects again, and returns true.

If at least one object has changed, the IObjectSpace.ConfirmationRequired event occurs. Handle this event to display a confirmation window that asks whether to save changes. Use the XafApplication.AskConfirmation method. This method returns a ConfirmationResult enumeration value that matches the end user’s choice. The next actions depend on this choice.

  • Yes

  • No

  • Cancel

The WinModificationsController handles the IObjectSpace.ConfirmationRequired event. This Controller only activates for Detail Views in Windows Forms applications. The handler shows a confirmation window as described above. The application does not show a confirmation window when you call the Refresh method in a List View. By default, the system sets the ConfirmationResult.No value. This value discards all changes and retrieves objects again through a new database connection.

The following events related to the Refresh method are available:

To implement a refresh operation, override the BaseObjectSpace.Reload method. In this method, recreate the Object Space’s container for in-memory objects. In XPO, use XPObjectSpace.Session. In Entity Framework Core, use EFCoreObjectSpace.DbContext.

The following code snippets (auto-collected from DevExpress Examples) contain references to the Refresh() 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-how-to-import-data-in-xaf/CS/XPO/ImportData/ImportData.Module/Controllers/ImportDataListViewController.cs#L65

csharp
importObjectSpace.CommitChanges();
    targetListView.ObjectSpace.Refresh();
}

XAF_generate-a-sequential-number-for-a-persistent-object-within-a-database-transaction/CS/XPO/Demo.Module/Controllers/TestListViewController.cs#L45

csharp
}
View.ObjectSpace.Refresh();
testAction.Caption = "Succeeded";

XAF_generate-a-sequential-number-for-a-persistent-object-within-a-database-transaction/VB/Demo.Module/Controllers/TestListViewController.vb#L46

vb
End Try
View.ObjectSpace.Refresh()
testAction.Caption = "Succeeded"

See Also

IObjectSpace Interface

IObjectSpace Members

DevExpress.ExpressApp Namespace