pycabook / rentomatic

A demo implementation of a clean architecture in Python.
MIT License
253 stars 88 forks source link

Room domain model: value or entity? #14

Open 4e1e0603 opened 3 years ago

4e1e0603 commented 3 years ago

If I understand it well, the Room domain model is what Domain-Driven Design (DDD) call Entity / Aggregate root entity, I think it would be better to compare rooms by id only because e.g. the price can change but the room is the same. At this moment the room is represented as a value object (DDD). I would model it like this:

Size =  NewType("Size", int)
Price = NewType("Price", Decimal)
Address = NewType("Address", str)
Description = NewType("Description", str)

class Room:
    """
    An aggregate root entity.
    """
    def __init__(self, id: UUID, size: Size, price: Price, address: Address, description: Description) -> None:
        self.id = id
        self.size = size
        self.price = price
        self.address = address
        self.description = description

    def __eq__(self, that) -> bool:
        return all((isinstance(that, type(self)), self.id == that.id))

    def __hash__(self) -> int:
        return hash((type(self), self.id))

    @classmethod
    def make(cls, id: UUID, size: Size, price: Price, address: Address, description: Description) -> Room:
        return cls(id, size, price, address, description)

Is there some channel where this architectural decisions can be discussed?

All the best, David.

lgiordani commented 3 years ago

David, thanks for the comment. What you say make definitely sense. I'm preparing version 2.0.2 of the book with some fixes, so I will definitely have a look at this. If it doesn't make it in 2.0.2 I will however use it in a future version as I agree that comparing IDs is the best strategy. Thanks!