Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
gt4dev opened this issue Mar 25, 2021 · 3 comments
Open

Comments

@gt4dev
Copy link

gt4dev commented Mar 25, 2021

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
Copy link
Author

gt4dev commented Apr 10, 2021

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.

@VaughnVernon
Copy link
Owner

@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.

@gt4dev
Copy link
Author

gt4dev commented Jun 17, 2021

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants