Skip to main content
A newer version of this page is available. .

Object Space

  • 6 minutes to read

In XAF applications, all data-aware manipulations and custom business logic are performed via the Object Space. The Object Space is an abstraction on the database context. The Object Space allows you to query or edit data in the transaction. Object Space is an ORM-independent implementation of the well-known Repository and Unit Of Work design patterns.

Object Space members are declared in the IObjectSpace interface. The BaseObjectSpace and CompositeObjectSpace classes implement the common code. XAF includes the following CompositeObjectSpace descendants:

ObjectSpaceDiagram

In most cases, you will access Object Space via the IObjectSpace interface and do not need to use these classes directly.

When implementing custom business logic (Create, Read, Update and Delete Data), Object Space is required if data the current business object exposes is insufficient for your logic, and you need to query more data. You may also use Object Space in your database initialization code, in complex View Items, etc. This topic describes the API you can use to get Object Space in various contexts.

Get an Existing Object Space

XAF creates Object Spaces for each Views in an application. To load data or perform custom logic with already loaded objects, use the approaches from the following table to get an Object Space from various contexts of your code.

Get an Existing Object Space in Business Object

An Object Space reference is automatically assigned to the IObjectSpaceLink.ObjectSpace property when a business object supporting an IObjectSpaceLink is instantiated. You can implement this interface and access other business objects directly in the current object code using the Object Space passed to the ObjectSpace property.

If you additionally implement an IXafEntityObject, then you can place your logic into the IXafEntityObject.OnCreated, IXafEntityObject.OnLoaded and IXafEntityObject.OnSaving methods. You can use the XAF Business Object | EF 6 Business Object from Template Gallery to add a class that supports both IXafEntityObject and IObjectSpaceLink.

We recommend using Controllers instead of accessing UI-related entities (Views, Controllers, Actions) and executing UI-specific logic within the business class code, because it violates the separation of concerns principle and is against the MVC architecture.

See also: IXafEntityObject

Get an Existing Object Space in a Controller

| In XAF applications, an Object Space is automatically assigned for each View. In a View Controller, you can get the current View using the ViewController.View property. Then, access the Object Space with the View.ObjectSpace. You can also use the protected ViewController.ObjectSpace property that refers to the same Object Space as View.ObjectSpace.

A Window Controller does not expose a View directly. You can access the current Frame using the Controller.Frame property and get the View with Frame.View.

A View handles Object Space events to update the UI each time an object changes. We recommend creating a new Object Space that is not bound to a current View instead of using the View.ObjectSpace property to process large amounts of data. You should also use a new Object Space when you create a new View with the XafApplication.CreateListView or XafApplication.CreateDetailView methods.

See also:

Get an Existing Object Space in a Module Updater

In a ModuleUpdater descendant, you can use the protected ModuleUpdater.ObjectSpace property to access the Object Space instance that can be used for database update operations. Do not use a new Object Space to update the database.

See also:

Get an Existing Object Space in a List Editor

A reference to the CollectionSourceBase object is automatically passed to the IComplexListEditor.Setup method if your custom List Editor supports the IComplexListEditor interface. You can implement this interface and access the Object Space via the CollectionSourceBase.ObjectSpace property.

Get an Existing Object Space in a View Item or a Property Editor

A reference to an Object Space is automatically passed to the IComplexViewItem.Setup method if your custom View Item or Property Editor supports the IComplexViewItem interface. You can implement this interface and store the Object Space reference to a local variable for future use.

See also: IComplexViewItem

Access Object Space in Events

Object Space is also available using arguments passed to various events of the XafApplication class.

Event Object Space Parameter
XafApplication.CreateCustomCollectionSource CreateCustomCollectionSourceEventArgs.ObjectSpace
XafApplication.CreateCustomLogonWindowObjectSpace CreateCustomPropertyCollectionSourceEventArgs.ObjectSpace
XafApplication.CreateCustomPropertyCollectionSource CreateCustomLogonWindowObjectSpaceEventArgs.ObjectSpace
XafApplication.ObjectSpaceCreated ObjectSpaceCreatedEventArgs.ObjectSpace
XafApplication.ViewCreating ViewCreatingEventArgs.ObjectSpace

Create a New Object Space

XAF Views listen to events of the Object Space assigned to a View. For example, the Save and Cancel Actions become enabled when there are changes in the View’s Object Space.

You may need a View does not react to modifications you perform in your business logic. In this scenario, create a new Object Space instance. With this, if you need to revert your changes, you can rollback changes in a separate Object Space without affecting changes in the View’s Object Space. You need to instantiate a new Object Space when you create a View using the XafApplication.CreateListView or XafApplication.CreateDetailView methods. Use the XafApplication.CreateObjectSpace(Type) method instead of an Object Space constructor to create an Object Space. For instance, the Controller.Application property is available in a Controller context, and you can create an Object Space as follows:

IObjectSpace objectSpace = this.Application.CreateObjectSpace(typeof(MyBusinessClass)); 

In XAF Blazor applications, if you need to access an ObjectSpace instance in a custom ASP.NET Core service or middleware, use Dependency Injection to inject the DevExpress.ExpressApp.Blazor.Services.IXafApplicationProvider.

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Blazor.Services;

public class CustomService {
    IXafApplicationProvider applicationProvider;
    public CustomService(IXafApplicationProvider applicationProvider) {
        this.applicationProvider = applicationProvider;
    }
    public IObjectSpace GetObjectSpace() {
        return applicationProvider.GetApplication().CreateObjectSpace(typeof(MyBusinessClass));
    }
}

Refer to the XafApplication class description to see how to obtain an XafApplication instance in various contexts. You cannot create new Object Spaces in the business object context because an XafApplication instance is not available there.

Examples:

Important

You should manually dispose of an Object Space when you are finished using it if you do not assign it to a View. An Object Space associated with a View is removed automatically together with this View.

When you need to perform a bunch of operations, but avoid immediately updating the View’s ObjectSpace, create a nested Object Space by using the IObjectSpace.CreateNestedObjectSpace() method. When you commit a nested Object Space, changes in it are merged into the parent Object Space and are not persisted in a database. To persist changes in a database, commit the parent Object Space. Please note that only XPO supports nested Object Spaces.

Once you have obtained or created an Object Space, you can use it to query or modify data (see Create, Read, Update and Delete Data).

See Also