开发者

Using value object as identifier in entity

开发者 https://www.devze.com 2023-02-07 02:50 出处:网络
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

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 TrackingI开发者_运维技巧d implements ValueObject<TrackingId> {

  private String id;

  /**
   * Constructor.
   *
   * @param id Id string.
   */
  public TrackingId(final String id) {
    Validate.notNull(id);
    this.id = id;
  }


A couple of things that would achieve:

  • Encapsulates the logic that the Tracking ID should not be null
  • Encapsulates the logic that the Tracking ID should not change once set.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号