有一个聚合,其字段与数据库对象不同,这样做是否正确?

huangapple go评论143阅读模式
英文:

Is correct to have an aggregate with fields different than database object?

问题

我正在尝试应用领域驱动设计(DDD),但有一些疑问。我正在阅读斯科特·米勒特的DDD书,刚刚读完了聚合章节。

想象一个简单的领域模型,类似这样:

class Candidate
val id: CandidateId
val name: Name
val musicStylesPreferences: List<MusicStyle>
val sportPreferences: List<Sport>
val smoking: Boolean
enum MusicStylePreference
ROCK, METAL, POP, HIPHOP

其他对象是这样的。

如果我们考虑持久化“候选人”(Candidate),我的方法如下:

*DatabaseObject*
CandidateVO
val id: String
val name: String

MusicStylePreferenceVO
val candidateId: String
val StyleName: String

当我将“候选人”对象持久化到数据库中时,我会将其持久化到适当的表格中,当我填充聚合时,我会查询这些表格。

我不确定这种方法是否正确,我也在考虑,也许像“MusicStylePreference”、“Smoking”等对象不需要保持一致性和事务性(这是聚合的一些优势),可以放在“候选人”聚合之外。

你认为呢?

我感谢任何回答。谢谢 有一个聚合,其字段与数据库对象不同,这样做是否正确?

英文:

I'm trying to apply DDD and I have some doubts. I'm reading Scott Millet DDD book and I've just read Aggregates chapters.

Imagine a simple domain model like this:

class Candidate
val id: CandidateId
val name:Name
val musicStylesPreferences: List&lt;MusicStyle&gt;
val sportPreferences: List&lt;Sport&gt;
val smoking: Boolean
enum MusicStylePreference
ROCK, METAL, POP, HIPHOP

The other objects are like this.

If we think about persisting the Candidate, my approach is like this:

*DatabaseObject*
CandidateVO
val id: String
val name: String


MusicStylePreferenceVO
val candidateId: String
val StyleName: String

When I persist the Candidate object in database I will persist in the appropriate table and when I hydrate the aggregate, I will query the tables.

I'm not sure if this approach is correct, I'm also thinking that maybe the objects like MusicStylePreference, Smoking etc ... doesn't need to be consistent and transactional (some of the advantages of aggregates) and can be outside of the aggregate Candidate.

What do you think?

I appreciate any answer. Thanks 有一个聚合,其字段与数据库对象不同,这样做是否正确?

答案1

得分: 4

这是正确的,甚至是必要的。聚合应该建模普遍语言,而数据库字段名称涉及持久性并具有其他独立的目标和要求。

一个刻意构造的例子:数据库可能将全名保存为“Firstname”、“LastName”,但聚合只有一个字符串FullName,这是值的串联。它不关心它们如何被持久化,甚至不关心它们被持久化在哪里。

英文:

It's correct and even necessary. The Aggregate should model the ubiquitous language while the database field names are about persistence and have other separate goals and requirements.

A contrived example; The database might save a fullname as "Firstname", "LastName" but the aggregate just has a string.FullName that's a concatenation of the values. It doesn't care how they are persisted or even where.

huangapple
  • 本文由 发表于 2020年8月21日 01:09:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63510029.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定