The requested page is not available for the requested platform. You are viewing the content for Default platform.

Non-Persistent Objects

A non-persistent class is a type of business class. XAF generates a UI for this class but does not bind it to an application's database table. You can use this class to display a List or Detail View with temporary data generated in code or loaded from storage. You can also use it to display an empty View (dialog) and process the user's input.

Important Notes

Basic Non-Persistent Class Implementation

Declare a regular class in the module project and decorate it with the DomainComponentAttribute to implement a non-persistent class.

using DevExpress.ExpressApp.DC;
// ...
[DomainComponent]
public class MyNonPersistentObject {
    public string Name { get; set; }
    public string Description { get; set; }
}

XAF automatically registers the class with this attribute in the Types Info Subsystem and adds it to the Application Model. Rebuild the solution and open the Model Editor to ensure that XAF added the class to the BOModel node and created the corresponding List and Detail Views.

NonPersistentObjectModel

XAF can display this MyNonPersistentObject's Detail View in a PopupWindowShowAction's popup dialog (see the Add an Action that Displays a Pop-up Window topic).

MyNonPersistentObjectPopup

You can also use the ActionAttribute to show a non-persistent object's popup. For this purpose, apply this attribute to a business class' method that takes a non-persistent type parameter.

Examples:

Non-Persistent Object Space

NonPersistentObjectSpace is an Object Space used to manage non-persistent object instances in your application. You can create, read or update non-persistent object instances from code if the XafApplication object supports this Object Space type. To support this Object Space type, add the following code to the overridden CreateDefaultObjectSpaceProvider method in the WinApplication.cs (WinApplication.vb), WebApplication.cs (WebApplication.vb) and/or MobileApplication.cs (MobileApplication.vb) file (in addition to the existing XPObjectSpaceProvider or EFObjectSpaceProvider registration).

protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) {
    // ...
    args.ObjectSpaceProviders.Add(new NonPersistentObjectSpaceProvider(TypesInfo, null));
}

Note that the Solution Wizard adds the code above automatically.

XAF uses the first registered Object Space Provider for the following purposes:

Ensure that NonPersistentObjectSpaceProvider is not the first registered Provider in your application.

Important

The NonPersistentObjectSpace cannot handle persistent objects. Create an additional Object Space for the persistent object type and add this Object Space to the NonPersistentObjectSpace.AdditionalObjectSpaces collection to allow processing persistent objects. Refer to the How to: Show Persistent Objects in a Non-Persistent Object's View topic for more information.

The NonPersistentObjectSpace class exposes events that occur when the Object Space loads non-persistent objects. You can handle the following events to supply data to non-persistent objects:

You can use the New, Delete and Save Actions to modify loaded non-persistent objects. The NonPersistentObjectSpace.ModifiedObjects property provides access to modified objects. Use the CreateObjectSpace(Type) method to create an Object Space and manage non-persistent objects in your code (for example, in Controller's code):

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
// ...
public class ShowDetailViewController : ViewController<ListView> {
    // ...
    void showDetailViewAction_CustomizePopupWindowParams(
        object sender, CustomizePopupWindowParamsEventArgs e) {
        IObjectSpace newObjectSpace = Application.CreateObjectSpace(typeof(NonPersistentObject));
        // ...
    }
}

Examples

Key Property

In XAF ASP.NET and Mobile applications, business objects should have a key property to be correctly displayed in List Views.

In ASP.NET applications, each ASPxGridView control's row should have a unique key. It is required for select, filter, sort and group operations and the standard XAF functionality that depends on them.

In Mobile applications, the key value is included in requests sent by a client application to receive a non-persistent object (for example, when showing a Detail View from a List View). Handle the NonPersistentObjectSpace.ObjectByKeyGetting event to pass an appropriate object to the client application.

Use the KeyAttribute to declare a key property. The BrowsableAttribute allows you to hide this property from UI. The following code snippet demonstrates how to declare the key property in the non-persistent class:

[DomainComponent]
public class NonPersistentObject {
    [Browsable(false)]
    [DevExpress.ExpressApp.Data.Key]
    public int Oid { get; set; }
    // ...
}
Important

Use the Key attribute from the DevExpress.ExpressApp.Data namespace only (not from the System.ComponentModel.DataAnnotations or DevExpress.Xpo namespaces).

You can allow users to open a specific non-persistent object instance from the Navigation:

  1. Add an item to the navigation. Set its IModelNavigationItem.View property to the identifier of the non-persistent object's DetailView and the IModelNavigationItem.ObjectKey to an arbitrary integer value.

    NonPersistentKey

  2. Create a WindowController descendant. In the NonPersistentObjectSpace.ObjectByKeyGetting event handler, get an object instance whose key value coincides with the Navigation Item's ObjectKey property value in the Model Editor.

Example: How to: Display a Non-Persistent Object's Detail View from the Navigation

INotifyPropertyChanged Support

Implement the INotifyPropertyChanged interface in your non-persistent class to use XAF features that track and process property value changes (for example, Conditional Appearance). Call the OnPropertyChanged method from the property's setter to trigger the PropertyChanged event. The Object Space handles this event internally. The PropertyChanged event triggers the IObjectSpace.ObjectChanged event.

Example: How to: Perform CRUD Operations with Non-Persistent Objects | PropertyChanged Event in Business Classes

IXafEntityObject and IObjectSpaceLink Support

Implement the IXafEntityObject interface to add custom business logic to the non-persistent class code. This interface declares methods that are called when the Object Space creates, loads, or saves an object. You can also implement the IObjectSpaceLink interface and use the Object Space the IObjectSpaceLink.ObjectSpace property returns. This allows you to query other objects in the same Object Space.

using DevExpress.ExpressApp;
// ...
[DomainComponent]
public class MyNonPersistentObject : IXafEntityObject, IObjectSpaceLink{
    // ...
    void IXafEntityObject.OnCreated() {
        // Place the entity initialization code here.
        // You can initialize reference properties using Object Space methods; e.g.:
        // this.Address = objectSpace.CreateObject<Address>();
    }
    void IXafEntityObject.OnLoaded() {
        // Place the code that is executed each time the entity is loaded here.
    }
    void IXafEntityObject.OnSaving() {
        // Place the code that is executed each time the entity is saved here.
    }
    IObjectSpace IObjectSpaceLink.ObjectSpace {
        get { return objectSpace; }
        set { objectSpace = value; }
    }
}

Example: How to: Perform CRUD Operations with Non-Persistent Objects

Non-Persistent Object Template

You can use a Visual Studio template to create a non-persistent class. For this purpose, open the Template Gallery, switch to the XAF category and choose the XAF Business Object | Non-Persistent Object item. The added class can be used in different complex scenarios listed in this topic.

NonPersistentObjectTemplate

This non-persistent class includes the IXafEntityObject, IObjectSpaceLink and INotifyPropertyChanged interface implementations and an integer type key property.

See Also