IDDD_Samples icon indicating copy to clipboard operation
IDDD_Samples copied to clipboard

why some entities in equals(..) use many properties and not only their ID?

Open gt4dev opened this issue 3 years ago • 3 comments

Hi

Vaughn, could you explain it? I'd appreciate ;] For example Product: uses ProductId and TentatId.

public class Product extends Entity {

    private Set<ProductBacklogItem> backlogItems;
    private String description;
    private ProductDiscussion discussion;
    private String discussionInitiationId;
    private String name;
    private ProductId productId;
    private ProductOwnerId productOwnerId;
    private TenantId tenantId;

    // ..

    @Override
    public boolean equals(Object anObject) {
        boolean equalObjects = false;

        if (anObject != null && this.getClass() == anObject.getClass()) {
            Product typedObject = (Product) anObject;
            equalObjects =
                this.tenantId().equals(typedObject.tenantId()) &&
                this.productId().equals(typedObject.productId());
        }

        return equalObjects;
    }
}

Entity is identified by its ID (ProductId) thus:

  • shouldn't ProductId be the only property used in equals(..)?
  • isn't using more properties in equals(..) breaking rule that entities are identified (compared, matched) only by ID?

Thank you GT

BTW. Great book. I'm reading it 2nd time ;]

gt4dev avatar Mar 25 '21 21:03 gt4dev

I found answer in IDDD book on page 185.

It allows to identify entities with strategy of 'late identity assignment', before main identity is assigned. It's VO-like equals. Especially useful in Sets.

gt4dev avatar Apr 10 '21 11:04 gt4dev

@gt4dev No. It's because the tenantId and productId are a composite id, but not held as one. In a SaaS with multiple tenants all entities must be "striped" with the tenantId. They are separated so the tenant is clearly identified and can be queried on its own.

VaughnVernon avatar Apr 10 '21 13:04 VaughnVernon

I know, it's just a sample but as identity is top important in entities let me ask sth.

Generalizing IDDD sample code: When I 1st time saw Thing with ThingId then I thought that ThingId is just id of thing [common sense] But it's not (as ID needs TenantId)

From my PoV clearer would be [in saasovation context]

  1. either create VO ThingId - which hides all inner elements: id of particular thing + all striping id-s [tenant, product ...] etc
  2. or rename ThingId to sth like ThingRawId or ThingUnstripedId - to explicitly express in name that it's not real id

What do think you about it, especially point 1?

IMO with "1"

  • model [and code] is simpler
  • still you can do all searches for "things" by tenant etc. as is done in current IDDD sample It's even more cleared when ID is more complex [like in eg BacklogItem where ID is build from 3 IDs].

BTW It's late answer, but in the meantime I've been sharpening my DDD skills ;] Doing study notes https://github.com/gt4dev/study-notes

Your and Evans books are big big changers. Thanks!

gt4dev avatar Jun 17 '21 19:06 gt4dev