使用值对象作为实体中的标识符
在查看示例 DDD 项目上的 Evans 项目时,我注意到在 Cargo
实体中,Evans 使用了 tracknumber,它是一个值对象。为什么他不选择普通的string tracknumber
而是选择值对象作为身份?这是埃文斯的片段:
public class Cargo implements Entity<Cargo> {
private TrackingId trackingId
}
public final class TrackingId implements ValueObject<TrackingId> {
private String id;
/**
* Constructor.
*
* @param id Id string.
*/
public TrackingId(final String id) {
Validate.notNull(id);
this.id = id;
}
While viewing Evans' project on sample DDD project, I notice that in the Cargo
entity, Evans uses tracknumber which is an value object. Why he didn't chooses plain string tracknumber
instead chooses value object for identity? Here is snippet from Evans:
public class Cargo implements Entity<Cargo> {
private TrackingId trackingId
}
public final class TrackingId implements ValueObject<TrackingId> {
private String id;
/**
* Constructor.
*
* @param id Id string.
*/
public TrackingId(final String id) {
Validate.notNull(id);
this.id = id;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以实现以下几点:
对于普通字符串,Cargo 对象必须了解这些规则。使用值对象方法意味着 TrackingId 维护有关其自身的这些规则。
A couple of things that would achieve:
With a plain string, the Cargo object would have to be aware of these rules. Using the Value Object approach means the TrackingId maintains these rules about itself.