How to: Implement a Custom Base Persistent Class

XAF ships with the Business Class Library that contains a number of persistent classes ready for use in your applications. All these classes derive from the BaseObject base persistent class declared in the same library. This is a recommended-to-use feature-rich persistent class. In certain scenarios though, it may not meet your requirements. In this case, you can either use one of the base persistent classes offered by XPO to implement a custom one. This topic describes the steps you need to perform to implement a custom base persistent class to ensure that it functions as expected throughout the entire eXpressApp Framework. If you do not need to implement a custom class and want to use one of the base classes offered by XPO, refer to the Base Persistent Classes help topic instead.


A complete sample project is available in the DevExpress Code Examples database at

While you can implement a custom base persistent class from scratch, it is better to reuse an exiting functionality by deriving it from one of the base classes offered by XPO. In this example we will derive the custom base class from XPCustomObject. This XPCustomObject class supports deferred deletion and optimistic concurrency control, but does not offer an auto-generated primary key property.

Implement the Auto-Generated Primary Key Property

If the base class you derive from does not have an auto-generated key property, you need to implement it manually. We do not recommend implementing composite or compound keys for new databases. While it is possible to design persistent classes for legacy databases with composite keys in certain scenarios, it is always better to modify the database schema to avoid this as using composite keys imposes some limitations on the default functionality. Refer to the How to create a persistent object for a database table with a compound key KB article to learn more. The following code snippet illustrates a GUID property marked with the Key attribute specifying that XPO should auto-generate its values. Note that XPO supports automatic generation of key property values via the Key attribute for the Int32 and GUID types only.

using System;
using System.ComponentModel;
using DevExpress.Xpo;
using DevExpress.Xpo.Metadata;
using DevExpress.ExpressApp;
public abstract class BasePersistentObject : XPCustomObject {
    public BasePersistentObject(Session session) : base(session) { }
    [Persistent("Oid"), Key(true), Browsable(false), MemberDesignTimeVisibility(false)]
    private Guid _Oid = Guid.Empty;
    [PersistentAlias("_Oid"), Browsable(false)]
    public Guid Oid { get { return _Oid; } }
    protected override void OnSaving() {
        if (!(Session is NestedUnitOfWork) && Session.IsNewObject(this))
            _Oid = XpoDefault.NewGuid();

It does not make much sense to persist this class as it contains a single property (for the auto-generated primary key) and thus the class is marked as non-persistent. As a result, primary key columns will be created in database tables corresponding to a descendant of this class. This eliminates redundant JOIN statements from SQL queries generated by XPO, thus improving database performance. Additionally, you should override the OnSaving method in the demonstrated way to support the correct saving of new objects derived from your class and created via a nested unit of work in specific situations.

Implement the ToString Method

To complete custom base persistent class implementation, override its ToString method to manage default properties. Default properties are displayed in Lookup Property Editors and take part in form caption generation. Additionally they are automatically used by the FullTextSearch Action and are displayed first in List Views. When implementing a custom base persistent class, override the ToString method to check whether or not the DefaultProperty attribute is applied to the class and return its value.

public abstract class BasePersistentObject : XPCustomObject {
    private bool isDefaultPropertyAttributeInit;
    private XPMemberInfo defaultPropertyMemberInfo;    
    public override string ToString() {
        if (!isDefaultPropertyAttributeInit) {
            DefaultPropertyAttribute attrib = XafTypesInfo.Instance.FindTypeInfo(
            if (attrib != null)
                defaultPropertyMemberInfo = ClassInfo.FindMember(attrib.Name);
            isDefaultPropertyAttributeInit = true;
        if (defaultPropertyMemberInfo != null) {
            object obj = defaultPropertyMemberInfo.GetValue(this);
            if (obj != null)
                return obj.ToString();
        return base.ToString();