BaseObjectSpace Class
A base class for the classes that implement the IObjectSpace interface.
Namespace: DevExpress.ExpressApp
Assembly: DevExpress.ExpressApp.v24.2.dll
NuGet Package: DevExpress.ExpressApp
Declaration
Remarks
When using XAF for building business applications, you can choose which data access technology to use:
XAF core does not depend on a specific data layer. The classes that are required to interact with a particular data layer are contained in separate libraries, DevExpress.ExpressApp.Xpo and DevExpress.ExpressApp.EFCore.
You can create a custom library for working with an alternative ORM system. All of these libraries should contain an Object Space class. It represents an instrument that enables managing a cache with persistent objects. For instance, the DevExpress.ExpressApp.Xpo library contains the XPObjectSpace class. It is a wrapper over the UnitOfWork class, which represents a cache with persistent objects in XPO. The DevExpress.ExpressApp.EFCore library contains the EFCoreObjectSpace class. It is a wrapper over the ObjectContext, that defines a container for in-memory objects in the Entity Framework.
Any Object Space class must implement the IObjectSpace interface. This interface exposes the members that do not depend on the used ORM system and the members that are data layer-specific. XAF supplies the BaseObjectSpace class. Its members can serve as the implementation of the IObjectSpace interface members that do not depend on the ORM system used. Both the XPObjectSpace and EFCoreObjectSpace classes are inherited from the BaseObjectSpace class to support the IObjectSpace interface.
A particular Object Space object is assigned for each View. It helps to retrieve and create objects represented by a View. Moreover, an Object Space tracks all changes made to its objects and saves them to the database when required. To access a View’s Object Space, use the View.ObjectSpace property. In addition, you can access the Object Space of an object collection to manage its entire content and individual objects. For this purpose, use the CollectionSourceBase.ObjectSpace property.
Note
The View.ObjectSpace property is not supposed to be used in scenarios where a large amount of data is processed, created or deleted. Instead, use an independent Object Space which is not used by a View. Such an Object Space can be instantiated via the XafApplication.CreateObjectSpace method.
Use an ObjectSpace object’s BaseObjectSpace.CreateObject method to create persistent objects. In this instance, this Object Space will manage the newly created object’s life cycle. All the changes made to this object will be tracked until the Object Space’s BaseObjectSpace.CommitChanges method is invoked. When executing custom features, use the CommitChanges method to save all the changes made to the persistent objects belonging to the current Object Space. The changes include: creating, modifying or deleting an object (see BaseObjectSpace.IsModified, BaseObjectSpace.SetModified, BaseObjectSpace.ModifiedChanged, and BaseObjectSpace.Delete). For instance, the CommitChanges method is called when executing the Save, SaveAndClose and other built-in Actions.
The Object Space class also allows you to perform auxiliary operations with objects: search for the required object (BaseObjectSpace.FindObject), getting information on the required object (IObjectSpace.GetKeyPropertyName, IObjectSpace.GetKeyPropertyType and IObjectSpace.GetKeyValue), getting the required objects (BaseObjectSpace.GetObject and BaseObjectSpace.GetObjectByKey), and others.
When creating a new View, you will need to create a new Object Space. For this purpose, use the XafApplication.CreateObjectSpace method. Note that in specific situations, you can create a View in the current Object Space. For instance, nested List Views that define non-aggregated collections are created in the parent View’s Object Space.
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp;
using DevExpress.Persistent.BaseImpl;
using System;
using System.Collections.Generic;
using System.Linq;
//...
public void MethodInsideController(IObjectSpace objectSpace) {
// In a ViewController, you can use the View.ObjectSpace property to access the current Object Space
// or call the Application.CreateObjectSpace method to create a new Object Space.
Person person = objectSpace.FirstOrDefault<Person>(p => p.FirstName == "John" && p.LastName == "Doe");
if(person != null) {
// IList<Task> outdatedTasks = objectSpace.GetObjects<Task>(CriteriaOperator.Parse("DueDate < ?", DateTime.Now));
IQueryable<Task> outdatedTasks = objectSpace.GetObjectsQuery<Task>().Where(t => t.DueDate < DateTime.Now);
foreach(Task task in outdatedTasks) {
task.AssignedTo = person;
}
}
objectSpace.CommitChanges();
}
//...
For more information on ways to access an Object Space in different scenarios, refer to the following help topic: Ways to Access an Object Space (the Database Context for CRUD Operations). To learn about Object Space API, see Create, Read, Update and Delete Data.