Skip to main content

Ways to Access an Object Space (the Database Context for CRUD Operations)

  • 8 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.

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).

Get an Existing Object Space

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

Get an Existing Object Space in a Controller

In XAF applications, an Object Space is automatically assigned for each View. XAF Views listen to events of the Object Space assigned to a View. In a View Controller, you can 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.

The previous options are suitable only for the simplest data manipulations that use an Object Space (for instance, modify a property of a current View object and commit changes). To process large amounts of data or create a new View with the XafApplication.CreateListView or XafApplication.CreateDetailView methods, create a new Object Space that is not bound to a current View instead of using the View.ObjectSpace property.

For more information on how to get an Object Space in an ASP.NET Core Controller, refer to the following section: Create a New Object Space in Advanced ASP.NET Core Scenarios.

See also:

Get an Existing Object Space in Business Object

In a EF Core Business Class

Classes that implement the IObjectSpaceLink interface can access an IObjectSpace object with the IObjectSpaceLink.ObjectSpace property.

Persistent EF Core classes implement the IObjectSpace interface at runtime. You can also use EF Core classes to implement this interface in following ways:

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 Core Business Object from Template Gallery to add a class that supports both IXafEntityObject and IObjectSpaceLink.

In an XPO Business Class

You do not need an Object Space inside a persistent class. Do not implement the IObjectSpaceLink and IXafEntityObject interfaces in this context. Instead, use the Session property and override the AfterConstruction, OnLoaded, and OnSaving methods respectively in an XPO class. For more information, review the following code diagnostics: XAF0023 | XAF0024.

In a Non-Persistent Business Class

Classes that implement the IObjectSpaceLink interface can access an IObjectSpace object with the IObjectSpaceLink.ObjectSpace property. To use this interface in non-persistent classes, use one of the following techniques:

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

Create a new Object Space instance in the following scenarios:

  • You create a View using the XafApplication.CreateListView or XafApplication.CreateDetailView methods in Controllers, custom List or Property Editors.
  • You need a View that does not react to modifications you perform in your business logic. For example, the Save and Cancel Actions become enabled when there are changes in the View’s Object Space, because a View handles Object Space events to update the UI. You need to revert your changes in a separate Object Space without affecting changes in the View’s Object Space.
  • You process large amounts of data (for instance, load and modify hundreds of related persistent objects).

Note

With XPO, 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() or XPObjectSpace.CreateNestedObjectSpace() methods. 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. Nested Object Spaces are implemented based on XPO NestedUnitOfWork and inherit their behavior and specifics.

Create a New Object Space in a Controller

Use the XafApplication.CreateObjectSpace(Type) method to create an Object Space. Refer to the XafApplication class description to see how to obtain an XafApplication instance in various contexts. 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)); 

See also:

Create a New Object Space in Business Object

In most cases, it is sufficient to get an existing Object Space to perform custom logic inside EF Core and Non-Persistent Business Classes.

You cannot create new Object Spaces in the business object context because an XafApplication instance is not available there. It is also NOT possible to access Views, and other XAF UI-related entities within the business class code, because it violates the separation of concerns principle and is against the MVC architecture - your data model should not be tied to the UI. To access XafApplication, Views, and other UI-related entities, implement Controllers.

Create a New Object Space in Advanced ASP.NET Core Scenarios

If you need to access an Object Space instance in a custom ASP.NET Core service or middleware, Web API controllers, custom Razor pages, and components, use Dependency Injection to inject the IObjectSpaceFactory or INonSecuredObjectSpaceFactory interfaces and invoke their CreateNonSecuredObjectSpace method. Refer to the following help articles to find the examples:

Create a New Object Space in Non-XAF Apps

In a non-XAF application, you can instantiate the Object Space Provider manually. Then, you can call the provider’s CreateObjectSpace method to create an Object Space. Refer to the following help article to find the example: Access XAF Application Data in a non-XAF Application

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.

See Also