简体   繁体   English

DDD-为什么实体的身份不仅仅是原始类型?

[英]DDD - Why are the identities of entities not just a primitive type?

将实体的ID定义为自定义类型而不是像数据库驱动的程序员通常那样定义为原始类型的目的是什么?

First of all, Domain entities must be designed independant from storage (other question is that sometimes it's impossible, but let's assume this). 首先,域实体的设计必须独立于存储(另一个问题是有时是不可能的,但让我们假设这一点)。 In real world (or in domain world) entites often has complex identity like first name, last name and password number. 在现实世界(或领域世界)中,实体通常具有复杂的身份,例如名字,姓氏和密码。 It's not a primitive. 这不是原始的。 People don't have an int Id from thier birth, but they have names and other documents. 人们从出生起就没有int ID,但是他们有姓名和其他文件。 So, you need to translate it to your domain model. 因此,您需要将其转换为您的域模型。 Of couse, sometimes, you don't have such complex identity and you can replace it with some more simple, like int number. 当然,有时您没有这么复杂的身份,可以用更简单的方式(例如int number)代替它。 But in both cases your choice shouldn't be based on database if you try to use DDD (it's difficult, yes). 但是在两种情况下,如果您尝试使用DDD,您的选择都不应该基于数据库(这很困难,是的)。 And later, you create mapping from domain entities to tables or other storage. 然后,您创建从域实体到表或其他存储的映射。

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

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