Day 14: Extension Points on a Business Object and When to Override Them

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

Day 14: Extension Points on a Business Object and When to Override Them

行为定义无需编写任何代码即可实现。无法通过定义表达的需求才需要接管:例如在提交文档前进行检查、保存后通知相关人员,以及在何种情况下禁止删除。框架提供的接管方式是继承业务对象并重写其中的几个步骤。子类根据需要,在相应的步骤中添加该表单特有的逻辑。难点在于判断哪个步骤是匹配的,以及代码应该放在基类方法之前还是之后。方法名称只能回答顺序问题,例如 DoBeforeSave 在保存前执行,DoAfterSave 在保存后执行。其余部分必须由框架规定。本文涵盖:何时继承业务对象以及何时不该继承;为什么一次“保存”被拆分为多个步骤;六个步骤各自的用途;重写时基准线前后的区别;以及什么决定了一个方法是否应该开放重写。

1. When to inherit from a business object

In “The Northwind Case Study: Eight Forms and a Custom Business Object,” seven of the eight forms contain zero lines of application code, with only the order naming its own type. Create, read, update, delete, and validation all emerge from definitions; code is only required for the stretch that definitions cannot express. Therefore, there are three levels to consider before inheriting, from shallow to deep:

  • Can it be delivered by: An expression on a field or a rule (Declarative Business Rules and Computed Fields).
  • Can it be changed by: Editing one definition file (Someone who understands the business).
  • Can it be written as: A plugin hooked onto a point in time (Replacing an assembly, by a developer).
  • Neither: Only then inherit from the business object (Replacing an assembly and changing the type this form is bound to, by a developer).

1. 何时继承业务对象

在《Northwind 案例研究:八个表单与一个自定义业务对象》中,八个表单中有七个没有任何应用代码,只有订单指定了自己的类型。创建、读取、更新、删除和验证都源于定义;只有定义无法表达的部分才需要编写代码。因此,在继承之前,有三个层次需要考虑,由浅入深:

  • 能否通过以下方式实现: 字段上的表达式或规则(声明式业务规则和计算字段)。
  • 能否通过以下方式更改: 编辑一个定义文件(由了解业务的人员操作)。
  • 能否通过以下方式编写: 挂载在特定时间点的插件(替换程序集,由开发人员操作)。
  • 都不行: 只有在这种情况下才继承业务对象(替换程序集并更改表单绑定的类型,由开发人员操作)。

The difference between these three is not difficulty, but the delivery path. The higher levels travel alongside the definitions, while the lower levels are closer to the code side. Both of the lower two produce an assembly, but a plugin only adds a stretch at a fixed point in time, so the packaged stretch always runs. Inheriting, however, holds the base line, allowing the subclass to decide whether the packaged stretch survives. This axis is the backbone of “Three Depths of Development and the Big Picture of the Framework,” and only one inference is taken from it here: inheriting is the last level. The test is the one from “An Overview of Definition-Driven Architecture”: Is this something every ERP in the world does the same way? What is the same eventually gets collected into a mechanism, and what differs is what an application should write.

这三者之间的区别不在于难度,而在于交付路径。较高层次随定义一起交付,而较低层次则更接近代码端。后两者都会产生程序集,但插件只是在固定的时间点添加一段逻辑,因此打包的逻辑总是会运行;而继承则掌握了基准线,子类可以决定是否保留打包的逻辑。这一轴线是《开发的三种深度与框架全景》的骨干,此处仅得出一个推论:继承是最后一个层次。测试标准来自《定义驱动架构概述》:这是世界上每个 ERP 系统都以相同方式处理的事情吗?相同的部分最终会被收集到机制中,而不同的部分才是应用程序应该编写的内容。

2. Why one Save is cut into several steps

Suppose the framework offered only one Save to override. An application wanting to block a record before saving would have to rewrite the entire process. Inside that one method would be the authorization check, data scope check, rule evaluation, persistence, and audit write. To add three lines of code, those five existing tasks would have to be copied verbatim; missing one during the copy creates a silent hole. By cutting it apart, the person overriding only takes over their own specific stretch. On the writing side, there are six protected virtual steps available:

  • Save: DoBeforeSave → DoSave → [change audit] → DoAfterSave
  • Delete: DoBeforeDelete → DoDelete → [delete audit] → DoAfterDelete (Note: The middle two steps are inside the transaction.)

2. 为什么一次“保存”被拆分为多个步骤

假设框架只提供一个 Save 方法供重写。如果应用程序想要在保存前拦截某条记录,就必须重写整个过程。在该方法内部包含了权限检查、数据范围检查、规则评估、持久化和审计写入。为了添加三行代码,必须将那五个现有任务原封不动地复制过来;如果在复制过程中遗漏了一个,就会产生一个隐蔽的漏洞。通过拆分,重写者只需接管他们自己需要的那部分逻辑。在写入端,有六个受保护的虚方法步骤可用:

  • 保存: DoBeforeSave → DoSave → [变更审计] → DoAfterSave
  • 删除: DoBeforeDelete → DoDelete → [删除审计] → DoAfterDelete (注:中间两个步骤位于事务内部。)

The skeleton of Save is excerpted in FormBusinessObject.Write.cs, where the order is clear: DoBeforeSave(context); plugins.RunBeforeSave(context); DoSave(context); WriteChangeAudit(...); DoAfterSave(context); plugins.RunAfterSave(context). The transaction is opened and closed by the repository inside DoSave, covering the entire document: one statement for the master and as many for the detail as there are rows. It commits only when everything succeeds and rolls back the entire document if one statement fails. What gets stored must be a complete document, not a master with missing detail rows. Delete requires the same logic: once the master is deleted, the detail must disappear with it, or you are left with orphaned detail rows.

Save 的骨架在 FormBusinessObject.Write.cs 中有所体现,其顺序一目了然:DoBeforeSave(context);plugins.RunBeforeSave(context);DoSave(context);WriteChangeAudit(...);DoAfterSave(context);plugins.RunAfterSave(context)。事务由 DoSave 内部的存储库打开和关闭,涵盖整个文档:主表一条语句,明细表根据行数对应多条语句。只有当所有操作都成功时才会提交,如果其中任何一条语句失败,则回滚整个文档。存储的内容必须是一个完整的文档,而不是缺少明细行的主表。删除操作也需要同样的逻辑:一旦主表被删除,明细表必须随之消失,否则就会留下孤立的明细行。