ABP框架系列之六:(Value-Objects-值对象)
Introduction
"An object that represents a descriptive aspect of the domain with no conceptual identity is called a VALUE OBJECT." (Eric Evans).
“表示没有概念标识的域的描述方面的对象称为值对象。”(Eric Evans)。
As opposite to Entities, which have their identities (Id), a Value Object has not it's identity. If identities of two Entities are different, they are considered as different objects/entities even if all other properties of those entities are same. Think two different persons have same Name, Surname and Age but they are different people if their identity numbers are different. But, for an Address (which is a classic Value Object) class, if two addresses has same Country, City, Street number... etc. they are considered as the same address.
与具有身份(id)的实体相反,值对象没有它的标识。如果两个实体的身份不同,即使这些实体的所有其他属性相同,它们也被视为不同的对象/实体。认为两个不同的人有相同的名字,姓氏和年龄,但他们是不同的人,如果他们的身份号码是不同的。但是,对于一个地址(这是一个经典值对象)类,如果两个地址具有相同的国家,城市,街道号…等等,它们被认为是同一个地址。
In Domain Driven Design (DDD), Value Object is another type of domain object which can include business logic and is an essential part of the domain.
在领域驱动设计(DDD)中,值对象是另一种类型的域对象,它可以包含业务逻辑,是域的重要组成部分。
Value Object Base Class
ABP has a ValueObject<T> base class which can be inherited in order to easily create Value Object types. Example Address Value Object type:
public class Address : ValueObject<Address>
{
public Guid CityId { get; private set; } //A reference to a City entity. public string Street { get; private set; } public int Number { get; private set; } public Address(Guid cityId, string street, int number)
{
CityId = cityId;
Street = street;
Number = number;
}
}
ValueObject base class overrides equality operator (and other related operator and methods) to compare two value object and assumes that they are identical if all properties are identical.
值类型继承基础类重载相等运算符(以及其他相关的操作和方法)比较两个值类型,假设所有的特性都是相同的他们都是相同的。
So, all of these tests pass:
var address1 = new Address(new Guid("21C67A65-ED5A-4512-AA29-66308FAAB5AF"), "Baris Manco Street", 42);
var address2 = new Address(new Guid("21C67A65-ED5A-4512-AA29-66308FAAB5AF"), "Baris Manco Street", 42); Assert.Equal(address1, address2);
Assert.Equal(address1.GetHashCode(), address2.GetHashCode());
Assert.True(address1 == address2);
Assert.False(address1 != address2);
Even they are different objects in memory, they are identical for our domain.
即使它们是内存中不同的对象,它们对于我们的域也是相同的。
Best Practices
Here, some best practices for Value Objects:
- Design a value object as immutable (as like the Address above) if there is not a very good reason for designing it as mutable.
- The properties that make up a Value Object should form a conceptual whole. For example, CityId, Street and Number shouldn't be serarate properties of a Person entity. Also, this makes Person entity simpler.
如果没有很好的理由将值对象设计为可变的,那么就要设计一个不可变的值对象(如上面的地址)。
构成一个值对象的属性应该构成一个概念整体。例如,cityid,街道和号码不应该被一个人员实体分离。此外,这使实体更简单。
ABP框架系列之六:(Value-Objects-值对象)的更多相关文章
- 老周的ABP框架系列教程 -》 一、框架理论初步学习
老周的ABP框架系列教程 -- 一.框架理论初步学习 1. ABP框架的来源与作用简介 1.1 简介 1.1.1 ABP框架全称为"ASP.NET Boilerplate ...
- 2019 年起如何开始学习 ABP 框架系列文章-开篇有益
2019 年起如何开始学习 ABP 框架系列文章-开篇有益 [[TOC]] 本系列文章推荐阅读地址为:52ABP 开发文档 https://www.52abp.com/Wiki/52abp/lates ...
- ABP框架系列之十一:(AspNet-Core-ASPNET核心)
Introduction This document describes ASP.NET Core integration for ASP.NET Boilerplate framework. ASP ...
- DDD理论学习系列(7)-- 值对象
DDD理论学习系列--案例及目录 1.引言 提到值对象,我们可能立马就想到值类型和引用类型.而在C#中,值类型的代表是strut和enum,引用类型的代表是class.interface.delega ...
- ABP框架系列之三十九:(NLayer-Architecture-多层架构)
Introduction Layering of an application's codebase is a widely accepted technique to help reduce com ...
- ABP框架系列之五十四:(XSRF-CSRF-Protection-跨站请求伪造保护)
Introduction "Cross-Site Request Forgery (CSRF) is a type of attack that occurs when a maliciou ...
- ABP框架系列之三十四:(Multi-Tenancy-多租户)
What Is Multi Tenancy? "Software Multitenancy refers to a software architecture in which a sing ...
- ABP框架系列之二十一:(Domain-Services-领域服务)
Introduction Domain Services (or just Service, in DDD) is used to perform domain operations and busi ...
- ABP框架系列之十八:(Data-Transfer-Objects-数据转换对象)
Data Transfer Objects are used to transfer data between Application Layer and Presentation Layer. 数据 ...
随机推荐
- SAS LOGISTIC 逻辑回归中加(EVENT='1')和不加(EVENT='1')区别
区别在于:最大似然估计分析中估计是刚好正负对调加上EVENT:%LET DVVAR = Y;%LET LOGIT_IN = S.T3;%LET LOGIT_MODEL = S.Model_Params ...
- HashMap与Hashtable
1. 1) Hashtable继承自Dictionary类,HashMap是map接口的一个实现类(继承自AbstractMap抽象类) 2) 在HashMap中,null可以 ...
- linux服务nfs与dhcp篇
nfs复习: 1.简介:用于liunx与linux之间的文件传输系统 2.下载nfs-utils和rpcbind 3.打开配置文件/etc/exports——文件名(目录名)共享给予的ip地址(rw) ...
- Fiddler手机抓包软件简单使用--将h5效果显示在手机
此文章是博主自己所写,转载请注明出处 一.简介 Fiddler是一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网之间的http通讯,设置断点,查看所有的“进出”Fiddler的数据. ...
- TP5 自定义验证器
TP内置验证功能提供两种验证方法 验证器(推荐) $validate = Validate::make([ 'id' => 'require|integer', ]); if ($validat ...
- ios黑科技
1.brew brew 是 Mac 下的一个包管理工具,类似于 centos 下的 yum,可以很方便地进行安装/卸载/更新各种软件包,例如:nodejs, mysql等,可以用来快速搭建各种本地环境 ...
- 微擎系统jssdk系统快速签名变量
jssdkconfig = {php echo json_encode($_W['account']['jssdkconfig']);} || { jsApiList:[] }; jssdkco ...
- 常用curl命令
curl -F "userfile=@/Users/username/Downloads/20170502.zip" http://youip/up.php curl -X POS ...
- Linux工作中常用命令
1. 搜索 在vi和vim中如果打开一个很大的文件,不容易找到对应的内容,可以使用自带的搜索关键字进行搜索定位: 在vi和vim界面中输入:"/"(反斜杠),之后会出现一个输入框让 ...
- 牛客练习赛42(A,B)
A:链接:https://ac.nowcoder.com/acm/contest/393/A 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K 6 ...