dev-mastery / comments-api

MIT License
1.92k stars 649 forks source link

Is Id considered to be in the framework layer? #83

Open gradywetherbee opened 11 months ago

gradywetherbee commented 11 months ago

I see that the factory builders for entities like comment accept Id as an argument. Is the implementation of Id in the Id folder considered part of the "framework" layer in clean architecture because it's relying on an external dependency (cuid) directly?

How should I be thinking about this?

ap0h commented 2 months ago

TL;DR: The dependency on a third-party library places the implementation in the infrastructure layer, while the interface remains in the domain layer.

If we translate this:

const Id = Object.freeze({
  makeId: cuid,
  isValidId: cuid.isCuid
})

into

interface Id {
    makeId(): string
    isValidId(id: string): boolean
}

The interface itself is dictated by the Domain Layer, specifically by the Entities in this case, but it can also be dictated by the use-cases. Either way, the Id object is a part of the domain logic, as it provides a fundamental service (ID generation and validation) that is used across the domain.

The fact that the interface implementation depends on a 3rd party library is not crucial here. While the implementation itself belongs to the infrastructure (framework) layer, the important point is that entities work with something that fulfills the Id interface.