我正在创建一个 RESTful Web 服务,它从 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;
}
如您所见,有两个重要的对象:repositoryDetailsInternal
和 repositoryDetailsOutput
。 repositoryDetailsInternal
是来自 Github 的已解析响应数据,repositoryDetailsOutput
是我的 Web 服务返回的输出对象。
我的问题是:这些对象分别是什么 - 实体还是值对象?
我倾向于将 repositoryDetailsInternal
称为实体,并将 repositoryDetailsOutput
称为值对象,但我想对此有一些第二意见。
repositoryDetailsInternal 可以随着时间的推移而更改(观星者数量可以增加或减少),但另一方面,在我的应用程序中该对象是不可变的。
编辑:我还应该提到 repositoryDetailsInternal
使用所有者和存储库名称进行缓存,因此它可能被视为身份:
@Cacheable(REPO_DETAILS_CACHE)
public RepositoryDetailsInternal getRepositoryDetails(String owner, String repositoryName) throws TimeoutException {...}
repositoryDetailsOutput
也是不可变的,看起来像一个值对象,因为它代表了一种存储库状态的快照。
最佳答案
据我所知,这两个对象都是值对象
,即使网络服务返回的值可能会有所不同,因为它们没有身份
.
我可以想象一个更复杂的场景,其中它们可以成为实体
,但我不知道这是否是您的情况(即您可以存储项目所拥有的恒星时间的演变) .
关于java - 实体与值对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42726581/