【问题标题】:Entity vs ValueObject实体与值对象
【发布时间】:2017-03-10 19:47:25
【问题描述】:

我正在创建一个 RESTful 网络服务,它从 github 检索存储库详细信息并返回它们。我的代码如下所示:

@GetMapping(value = "/{owner}/{repositoryName}")
@ResponseStatus(HttpStatus.OK)
public RepositoryDetails getRepositoryDetails(@PathVariable String owner, @PathVariable String repositoryName) throws Exception {
    log.info("Github repository details request: owner: " + owner + ", repository name: " + repositoryName);
    checkParameters(owner, repositoryName);
    RepositoryDetailsInternal repositoryDetailsInternal = repoService.getRepositoryDetails(owner, repositoryName);
    RepositoryDetails repositoryDetailsOutput = new RepositoryDetails(repositoryDetailsInternal, LocaleContextHolder.getLocale());
    return repositoryDetailsOutput;
}

如您所见,有两个重要的对象:repositoryDetailsInternalrepositoryDetailsOutputrepositoryDetailsInternal 是来自 Github 的解析响应数据,repositoryDetailsOutput 是我的 web 服务返回的输出对象。
我的问题是:这些对象中的每一个是什么 - 一个实体还是一个值对象?

我倾向于将 repositoryDetailsInternal 称为实体,将 repositoryDetailsOutput 称为值对象,但我想对此提出第二意见。
一方面,repositoryDetailsInternal 可以随着时间的推移而改变(观星者的数量可以增加或减少),但另一方面,在我的应用程序中,这个对象是不可变的。
编辑:我还应该提到 repositoryDetailsInternal 使用所有者和存储库名称进行缓存,因此它可能被视为身份:

@Cacheable(REPO_DETAILS_CACHE)
    public RepositoryDetailsInternal getRepositoryDetails(String owner, String repositoryName) throws TimeoutException  {...}



repositoryDetailsOutput 也是不可变的,看起来像一个值对象,因为它代表了一种存储库状态的快照。

【问题讨论】:

  • 也许这就是你所追求的? stackoverflow.com/questions/14161753/…。恕我直言(im)可变性与“成为一个实体”是正交的,因为它们可以是可变的或不可变的(就像您可以拥有实体,例如不会改变的过去事件)。

标签: java spring rest spring-mvc domain-driven-design


【解决方案1】:

据我所知,这两个对象都是 Value Objects,即使 Web 服务返回的值可能会有所不同,因为它们没有 Identity

我可以想象一个更复杂的场景,他们可能会变成 entities,但我不知道这是否是您的情况(即您可以存储项目拥有的星星的时间演变)。

【讨论】:

  • 请查看编辑。它会改变你对这个问题的看法吗?
  • 嗯,不。 github 存储库看起来像 Entity,但在 Github Remote Bounded Context 中,您的存储库是 Value Object
  • 不管怎样,一个建议,如果您不确定,请将其设为Value Object
猜你喜欢
  • 2011-03-09
  • 2010-10-04
  • 2013-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-08
  • 2012-04-01
  • 1970-01-01
相关资源
最近更新 更多