简体   繁体   English

DDD:实体可以具有原始数据类型的属性吗?

[英]DDD: Can an entity have attributes of primitive data types?

The domain driven design differentiates two model types: entities and value objects. 域驱动的设计区分两种模型类型:实体和值对象。 In the most examples the attributes of an entity are value objects or other entities, while the attributes of a value object are mostly simple strings, integers etc. (ie primitive data types). 在大多数示例中,实体的属性是值对象或其他实体,而值对象的属性主要是简单的字符串,整数等(即原始数据类型)。

That leads me to the question: Can an entity also have attributes of primitive data types? 这使我想到一个问题:实体是否也可以具有原始数据类型的属性? Or do you normally model each attribute of an entity as a value object or another entity? 还是通常将实体的每个属性建模为值对象或另一个实体?

The following might be an example to answer this question: We have an entity Comment with an attribute text . 以下可能是回答此问题的示例:我们有一个带有属性text的实体Comment Is text simply a string variable or a value object? text仅仅是字符串变量还是值对象?

While it is common attitude to compose an entity of another entities or value objects, it is not necessary. 虽然通常将另一实体或值对象组合成一个实体,但这不是必需的。 Please remember that you should think about an abstraction. 请记住,您应该考虑一个抽象。 Primitive types are ok when there is no business logic involved in using them. 当使用原始类型没有业务逻辑时,原始类型就可以。 For example: 例如:

public class User {
    private UserId id;
    private String nickname;
    private Date joinDate;
}

As you can see, nickname is an primitive type, because we can't do anything special with nickname. 如您所见,昵称是一种原始类型,因为我们不能对昵称做任何特殊的事情。 On the other hand joinDate should be Value Object, because dates has some logic (as comparing dates, adding, subtracting etc.) 另一方面,joinDate应该是Value Object,因为日期具有某些逻辑(例如比较日期,加,减等)。

Even in "Implemeting Domain-Driven Design" by Vaughn Vernon are examples of entities composed of primitive types. 即使在沃恩·弗农(Vaughn Vernon)的“实现域驱动设计的实现”中,也包含由原始类型组成的实体的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM