DDD - 业务规则

1. Business rules are an important part of the business domain. They define data validation and other constraints that need to be applied on domain objects in specific business process scenarios. Business rules typically fall into the following categories:

  • Data validation
  • Data transformation
  • Business decision-making
  • Process routing (work-flow logic)

2. The context is very important in DDD world. Context specificity dictates(规定,命令,影响) the domain object collaboration as well as other run-time factors like what business rules to apply etc. Validation and other business rules are always processed in a specific business context. This means the same domain object, in a different business context, will have to process different set of business rules. For example, some attributes of a loan domain object (such as loan amount and interest rate) cannot be changed after the loan has been through the Underwriting step in the loan approval process. But the same attributes can be changed when the loan is just registered and locked for a specific interest rate.

注释:可知,在不同业务上下文,会处理不同的业务规则。

3. Even though all the domain specific business rules should be encapsulated in the domain layer, some application designs put the rules in facade classes, which leads to domain classes becoming "anemic(贫血的)" in terms of(在...方面) business rules logic. This may be an acceptable solution in small size applications, but it is not recommended for mid-size to large enterprise applications that contain complex business rules. A better design option is to put the rules where they belong, inside the domain objects. If a business rule logic spans(跨越) two or more Entity objects, then it should become part of a Service class.

注释:可知,业务逻辑应该放到相应的领域对象了,如果这个业务逻辑涉及多个实体,那就放到服务里。

4. Also, if we are not diligent in the application, design business rules will end up being coded in the form of several switch statements in the code. And over time as the rules get more complex, developers don‘t take time to refactor the code to move "switch" statements into a more manageable design. Hardcoding the complex routing or decision-making rules logic in the classes leads to longer methods in the classes, code duplication, and ultimately a rigid (死板的)application design which will become a maintenance nightmare in the long run. A good design is to place all the rules (especially complex rules that change frequently as the business strategy changes) into a Rules engine (using a rules framework like JBoss Rules, OpenRules, or Mandarax) and invoke them from the domain classes.

注释:可知,就是把所有的业务逻辑放到一个业务引擎里,然后通过领域类来调用。  "3"里不是说这样会造成业务逻辑的贫血吗?

5. Validation rules are usually implemented in different languages like Javascript, XML, Java code, and other scripting languages. But due to the dynamic nature of business rules, scripting languages such as Ruby, Groovy, or Domain Specific Languages (DSL) are a better choice to define and manage these rules. Struts (Application layer), Spring (Service) and Hibernate (ORM) all have their own validation modules where we can apply the validation rules on the incoming or outgoing data objects. In some cases, validation rules can also be managed as Aspects (link AOP rules article here) that can be weaved into different layers (e.g. Service and Controller) of the application.

注释: 验证规则一般用脚本来写和管理,也可以通过AOP来管理。 例子呢?

6. It‘s important to keep in mind the unit testing aspect when writing the domain classes to manage business rules. Any changes in the rules logic should be easily unit testable in isolation.

注释:时刻记得业务逻辑的可测试性。

7. The sample application includes a business rule set to validate the loan parameters are within the allowed product and rate specifications. The rules are defined in a scripting language (Groovy) and are applied on the loan data passed to FundingService object

Design:

1. From a design stand-point, the domain layer should have a well defined boundary to avoid the corruption of the layer from non-core domain layer concerns such as vendor-specific(特定供应商的) translations, data filtering, transformations, etc. Domain elements should be designed to hold the domain state and behavior correctly. Different domain elements are structured differently based on state and behavior. Table 2 below shows the domain elements and what they contain.

Domain Element State/Behavior
Entity, Value Object, Aggregate State and Behavior
Data Transfer Object State only
Service, Repository
Behavior only

注释:领域层需要定义一个号的边界,这样可以避免领域被其它非核心的领域所污染。另外领域元素主要是基于 “状态” 和 “行为”。

2. Entities, Value Objects, and Aggregates which contain both state (data) and behavior (operations), should have clearly defined state and behavior. At the same time, this behavior should not extend beyond the limits of the object‘s boundaries. Entities should do most of the work in the use case acting on their local state. But they shouldn‘t know about too many unrelated concepts.

时间: 2024-10-12 15:25:47

DDD - 业务规则的相关文章

Ckrule业务规则管理系统简介

1.   简述 Ckrule业务规则管理系统(BRMS)是一个集成的应用程序存储.管理.执行和测试的平台,允许组织定义.部署.监控和维护运营系统使用的各种复杂决策逻辑.Ckrule BRMS 独立于核心应用程序代码提取并管理决策逻辑,以便可以跨整个组织轻松理解.维护和重用这些决策逻辑. Ckrule BRMS由下图4个部分组成: 各部分功能明细如下: 一级功能 二级功能 说明 规则存储 -- 存储库允许规则置于核心应用程序代码之外.它还允许将决策逻辑作为一项企业资产管理,从而支持更轻松地理解和更

基于RulesEngine的业务规则实现

规则引擎由推理引擎发展而来,是一种嵌入在应用程序中的组件,实现了将业务决策从应用程序代码中分离出来,并使用预定义的语义模块编写业务决策.接受数据输入,解释业务规则,并根据业务规则做出业务决策.比较常见的业务规则引擎有Drools.VisualRules 和iLog.这里介绍另外一个C#开源工具RulesEngine.下面通过一个例子来他如何使用. 1 项目结构 在RulesEngine源代码中添加一个RulesEngineDemo的窗体应用程序,然后引用需要的类库,如下图所示: 2 订单等实体类

微软Dynamics CRM 2013 中业务规则,有点像C#的正则表达式

微软Dynamics CRM 2013  中业务规则,我的理解就是有点像C#的正则表达式, 如方某个字段,必须输入什么范围的数值,其它字符不能乱输入. 打开方式有二种: 1种像上篇文章中写的那样, 在系统视图中 找到 业务规则 2. 打开表单编辑器,在上面的菜单栏中找到 业务规则. 选中 业务规则 新建一个业务规则 我们设定的规则是总金额大于80000,把一般销售助理表单中的 付款方式 锁定,放权给销售经理可以更改. 保存一下: 最后一定要 "激活" 如下图,状态为已激活. 业务规则功

Dynamics CRM 2015Online Update1 new feature之 通过业务规则清空字段的值

自2013引入业务规则后很多的功能就不需要通过javascript来实现,业务人员直接通过配置就能解决.那随着版本的更新业务规则的功能也越来越强大,从之前很单纯的逻辑到后面的if..else,相信后面的功能还会更加强大. 那2015Online Update1一个新功能又解决了程序猿的一痛点,之前通过业务规则实现了显示隐藏,那一个字段显示另外个字段隐藏,隐藏的字段的值肯定是要清空的,但2015之前的版本对于这么个简单的功能还是无法通过业务规则来实现的,还是得写脚本代码,当然这个问题在Online

开源业务规则引擎JBoss Drools

Drools 是什么? 规则引擎由推理引擎发展而来,是一种嵌入在应用程序中的组件,实现了将业务决策从应用程序代码中分离出来,并使用预定义的语义模块编写业务决策.接受数据输入,解释业务规则,并根据业务规则做出业务决策. http://www.tuicool.com/articles/3EFNV3M

需求用例分析之业务规则

作者:张克强 作者微博:张克强-敏捷307 在雅各布森用例分析方法和科伯恩用例分析方法中其实都没有"业务规则"的属性,在科伯恩方法中有个相近的属性是约束条件.但是业界使用中常常会给用例加上这个属性,这是为什么呢?为什么两位大师没有加上,是大师们疏忽了?而为什么不少人加上了呢? 从时间和传播上很容易推断,业务规则的来源是传统的需求规格说明书.在传统的需求规格说明书中,整理提炼业务规则或称业务逻辑是其中核心的分析产物.受到传统需求规格说明书的深远影响,不少人觉得这样的业务规则是值得写的用例

业务规则引擎浅析

在CRM(客户关系管理)系统或者其他业务支撑型系统的开发过程中,最经常多变的就是复杂的业务规则.因为这些规则要迎合.顺应市场的变化,如何能有效到做到业务规则和整体的系统支撑架构解耦分离,这个是开发过程中必须考虑的一个问题.每当客户要求改变一个业务规则的时候,我们又如何能做到在最短的时间内完成需求的开发提交,提高系统的灵活度?业务规则引擎无非是一个比较好的解决方案.它把复杂.冗余的业务规则同整个支撑系统分离开,做到架构的可复用移植,这个就是我们的终极目标. 那规则引擎又是什么东西?严格来说,它是一

BizTalk动手实验(九)业务规则引擎使用

1 课程简介 通过本课程熟悉业务规则引擎(BRE)的使用(本环境为Windows 2008 32位操作系统环境 + Visual Studio 2010 + BizTalk 210) 2 准备工作 1. 熟悉BizTalk Schema,Orchestration相关开发技术 3 演示 1. 创建BizTalk项目 2. 新建Schema,新建product(string类型),quantity(int类型),price(double类型)个字段,如下图所示 3. 创建Orchestration

Dynamics CRM2013 业务规则的新建、激活与删除

CRM2013的一个新的feature叫做业务规则,一些页面的简单的显示隐藏的控制.字段是否必填.有条件的锁定字段.错误提示等等,以前都是需要些脚本代码实现现在只需通过业务规则做一些简单的配置就可以达到同样的效果. 进入窗体设计页面,点击上方的业务业务规则,右侧下方点新建业务规则即可. 设置条件如下 可以设置多样的操作 设置完保存激活即可(要使业务规则生效必须激活,仅仅保存是没用的).规则可以新建,但如果不需要了如何删除呢,在页面上一时无法找到删除的地,那我们就通过本文示例的条件中的字段去寻找,