学习011-03-10 Implement a Custom Base Persistent Class(实现自定义基础持久化类)

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. These classes are derived from the BaseObject base persistent class declared in the same library. We recommend that you use this feature-rich persistent class. Inherit your XPO classes from it to use features in your data model. Refer to the following topic for the list of supported features: Base Persistent Classes.
XAF附带业务类库,其中包含许多可在您的应用程序中使用的持久类。这些类派生自同一库中声明的BaseObject基本持久类。我们建议您使用这个功能丰富的持久类。从中继承您的XPO类以使用数据模型中的功能。有关支持的功能列表,请参阅以下主题:基本持久类。

Extend the BaseObject Class(扩展BaseObject类)

Create a BaseObject descendant and apply the NonPersistent attribute to it to implement additional functionality in a base class:
创建一个BaseObject后代并对其应用非持久性属性以在基类中实现附加功能:

C#
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Xpo;
//...
[NonPersistent]
public abstract class BasePersistentObject : BaseObject {
    public BasePersistentObject(Session session) : base(session) { }
    public override void AfterConstruction() {
        base.AfterConstruction();
        CreatedOn = DateTime.Now;
    }
    protected override void OnSaving() {
        base.OnSaving();
        UpdatedOn = DateTime.Now;
    }
    private DateTime _createdOn;
    private DateTime _updatedOn;
    [ModelDefault("AllowEdit", "False"), ModelDefault("DisplayFormat", "G")]
    public DateTime CreatedOn {
        get { return _createdOn; }
        set { SetPropertyValue("CreatedOn", ref _createdOn, value); }
    }
    [ModelDefault("AllowEdit", "False"), ModelDefault("DisplayFormat", "G")]
    public DateTime UpdatedOn {
        get { return _updatedOn; }
        set {
            SetPropertyValue("UpdatedOn", ref _updatedOn, value);
        }
    }
}

Inherit your data model classes from the base class to extend them with the CreatedOn and UpdatedOn properties. The NonPersistent attribute specifies that XPO should not use a separate table for the BasePersistentObject class, and the declared properties should be stored in tables of its descendants. Refer to the following topic for more information: Inheritance Mapping.
从基类继承数据模型类以使用CreatedOn和UpdatedOn属性对其进行扩展。非持久性属性指定XPO不应为BasePersistentObject类使用单独的表,并且声明的属性应存储在其后代的表中。有关详细信息,请参阅以下主题:继承映射。

For instructions on how to store user-specific information, refer to the following article: How to implement the CreatedBy, CreatedOn and UpdatedBy, UpdatedOn properties in a business class.
有关如何存储用户特定信息的说明,请参阅以下文章:如何在业务类中实现CreatedBy、CreatedOn和UpdatedBy、UpdatedOn属性。

You may need to alter or disable some features shipped with the BaseObject class. Use one of the base persistent classes offered by XPO to do this: XPO Classes Comparison. XPO also allows you to implement a base class from scratch, although this solution is rarely required and is intended for a few advanced scenarios: Add Persistence to an Existing Hierarchy: Change the Base Inheritance.
您可能需要更改或禁用BaseObject类附带的某些功能。使用XPO提供的基本持久类之一来执行此操作:XPO类比较。XPO还允许您从头开始实现基类,尽管此解决方案很少需要,并且适用于一些高级场景:将持久性添加到现有层次结构:更改基本继承。

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.
如果您派生的基类没有自动生成的键属性,则需要手动实现它。我们不建议为新数据库实现复合或复合键。虽然在某些情况下可以为具有复合键的遗留数据库设计持久类,但最好修改数据库模式以避免这种情况,因为使用复合键会对默认功能施加一些限制。请参阅如何为具有复合键的数据库表创建持久对象KB文章以了解更多信息。以下代码片段说明了一个标有Key属性的GUID属性,指定XPO应自动生成其值。请注意,XPO仅支持通过Int32和GUID类型的Key属性自动生成键属性值。

C#
using System;
using System.ComponentModel;
using DevExpress.Xpo;
using DevExpress.Xpo.Metadata;
using DevExpress.ExpressApp;
//...
[NonPersistent]
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(nameof(_Oid)), Browsable(false)]
    public Guid Oid { get { return _Oid; } }
    protected override void OnSaving() {
        base.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.
持久化这个类没有多大意义,因为它包含一个属性(用于自动生成的主键),因此该类被标记为非持久化。因此,主键列将在对应于该类后代的数据库表中创建。这消除了XPO生成的SQL查询中的冗余JOIN语句,从而提高了数据库性能。此外,您应该以演示的方式覆盖OnSave方法,以支持正确保存从您的类派生并在特定情况下通过嵌套工作单元创建的新对象。

Implement the ToString Method(实现ToString方法)

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.
要完成自定义基持久类的实现,请重写其ToString方法以管理默认属性。默认属性显示在查找属性编辑器中并参与表单标题生成。此外,它们由FullTextSearch操作自动使用,并首先显示在列表视图中。实现自定义基持久类时,重写ToString方法以检查是否将DefaultProperty属性应用于类并返回其值。

C#
[NonPersistent]
public abstract class BasePersistentObject : XPCustomObject {
    //...
    private bool isDefaultPropertyAttributeInit;
    private XPMemberInfo defaultPropertyMemberInfo;    
    public override string ToString() {
        if (!isDefaultPropertyAttributeInit) {
            DefaultPropertyAttribute attrib = XafTypesInfo.Instance.FindTypeInfo(
                GetType()).FindAttribute<DefaultPropertyAttribute>();
            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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤姆•猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值