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.

XAF 随业务类库一起提供,该库包含许多可供在应用程序中使用的持续类。所有这些类都派生自在同一库中声明的 BaseObject 基持久性类。这是推荐使用功能丰富的持久性类。但是,在某些情况下,它可能不符合您的要求。在这种情况下,可以使用 XPO 提供的基本持久性类之一来实现自定义类。本主题介绍实现自定义基持久性类所需的步骤,以确保它在整个 eXpressApp 框架中按预期运行。如果不需要实现自定义类,并且希望使用 XPO 提供的基础类之一,请参阅基本持久性类帮助主题。

Tip 提示
A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E1255
完整的示例项目可在 DevExpress 代码示例数据库中找到,http://www.devexpress.com/example=E1255

.

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.

虽然可以从头开始实现自定义基持久性类,但最好通过从 XPO 提供的基础类之一派生退出功能来重用退出功能。在此示例中,我们将从 XPCustomObject 派生自定义基类。此 XPCustomObject 类支持延迟删除和乐观并发控制,但不提供自动生成的主键属性。

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 文章。以下代码段演示了一个 GUID 属性,该属性用 Key 属性标记,指定 XPO 应自动生成其值。请注意,XPO 仅支持通过 Int32 和 GUID 类型的 Key 属性自动生成键属性值。

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

[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();
}
}

How to: Implement a Custom Base Persistent Class 如何:实现自定义持久化基类的更多相关文章

  1. C++ - 派生类访问模板基类(templatized base class)命名

    派生类访问模板基类(templatized base class)命名 本文地址: http://blog.csdn.net/caroline_wendy/article/details/239936 ...

  2. 空基类优化empty base class optimization

    1.为什么C++中不允许类的大小是0 class ZeroSizeT {}; ZeroSizeT z[10]; &z[i] - &z[j]; 一般是用两个地址之间的字节数除以类型大小而 ...

  3. How to implement a custom type for NHibernate property

    http://blog.miraclespain.com/archive/2008/Mar-18.html <?xml version="1.0" encoding=&quo ...

  4. [Angular] Implement a custom form component by using control value accessor

    We have a form component: <label> <h3>Type</h3> <workout-type formControlName=& ...

  5. [Angular 8] Implement a Custom Preloading Strategy with Angular

    Preloading all modules is quite an extreme approach and might not always be desirable. For instance, ...

  6. How to implement a custom PropertyEditor so that it supports Appearance rules provided by the ConditionalAppearance module

    https://www.devexpress.com/Support/Center/Question/Details/T505528/how-to-implement-a-custom-propert ...

  7. Sitecore Digital Marketing System, Part 1: Creating personalized, custom content for site visitors(自定义SiteCore中的 Item的Personalize的Condition) -摘自网络

    Sitecore’s Digital Marketing System (DMS) can help you personalize the content your site displays to ...

  8. object base基类分析

    uvm_object,是所有uvm data和hierarchical class的基类,实现了copy,compare,print,record之类的函数 扩展类中必须实现create和get_ty ...

  9. 空基类优化—— EBCO—— empty base class optimization

    完全参考自:<C++ Templates The Complete Guide>e_CN,p_281 16.2 空基类优化 最近周围有点吵,论文没看进去,随便翻了本书…… 下文没有多大意义 ...

随机推荐

  1. 用Python调用华为云API接口发短信

    [摘要] 用Python调用华为云API接口实现发短信,当然能给调用发短信接口前提条件是通过企业实名认证,而且有一个通过审核的短信签名,话不多说,showcode #!/usr/bin/python3 ...

  2. 《手把手教你》系列练习篇之9-python+ selenium自动化测试 -番外篇 - 最后一波啊!!!(详细教程)

    1. 简介 本来上一篇就是练习篇的最后一篇文章了,但是有的小伙伴私下反映说是做了那么多练习,没有一个比较综合的demo练练手.因此宏哥在这里又补存了一些常见的知识点进行练习,在文章最后也通过实例给小伙 ...

  3. luogu P1412 经营与开发 |dp

    题目描述 4X概念体系,是指在PC战略游戏中一种相当普及和成熟的系统概念,得名自4个同样以"EX"为开头的英语单词. eXplore(探索) eXpand(拓张与发展) eXplo ...

  4. luogu P3939 数颜色 |vector

    题目描述 小 C 的兔子不是雪白的,而是五彩缤纷的.每只兔子都有一种颜色,不同的兔子可能有 相同的颜色.小 C 把她标号从 1 到 n 的 n 只兔子排成长长的一排,来给他们喂胡萝卜吃. 排列完成后, ...

  5. openlayers4 入门开发系列结合 echarts4 实现散点图(附源码下载)

    前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...

  6. Mysql双活方案

    #### 说明 Mysql主主互备即为两个mysql的互为备份机   ##### Windows下安装步骤(Linux下步骤类似,基本就是装上mysql,然后修改配置来完成主从的设置) - step1 ...

  7. 从头学pytorch(二) 自动求梯度

    PyTorch提供的autograd包能够根据输⼊和前向传播过程⾃动构建计算图,并执⾏反向传播. Tensor Tensor的几个重要属性或方法 .requires_grad 设为true的话,ten ...

  8. GoLand相同目录(包)下方法调用

    之前写的python,摸索go的时候发现相同目录下写的方法调用,在编译时竟然会报错,Mark~ 相同目录下方法调用,main.go文件调用tool.go中的add方法,要点如下: 1.两个文件的pak ...

  9. 【Java】在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

    题目描述: 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整 ...

  10. Node6-1单元测试mocha

    1.初步简单的测试 在src新建一个math.js的文件.src/math.js module.exports ={ add:(...args)=>{ return args.reduce((p ...