phalt / django-api-domains

A pragmatic styleguide for Django API Projects
https://phalt.github.io/django-api-domains/
MIT License
705 stars 62 forks source link

[FEEDBACK] What about rich models? #29

Closed FabioRodrigues closed 3 years ago

FabioRodrigues commented 4 years ago

I've seen you based this project on DDD, I'd like to ask you what is the guidance about rich models (entities)? Shouldn't the services just orchestrate the calls? Like calling many services, external infra actions, etc? For example:

Given I want to reserve a book
When I reserve one book
And there is at least one available book
Then The available quantity of this book should be decreased by one
And I must receive an email informing I reserved it
#book_service.py
@atomic
def reserve_book(self, id: int, user_id: int) -> None:
    user = User.objects.get(id=user_id)

    book = Book.objects.get(id=id)
    book.reserve(user.email)
    book.save()

    email_client.send(user.name, user.email, "book reserved")

#models/book.py
def reserve(self, user_email: string):
    if(self.available_quantity ==  0):
        raise Exception("book not available")

    self.availabe_quantity -= 1
    self.renting_history.append(user_email, datetime.today())
FabioRodrigues commented 3 years ago

hey @phalt anything about this? Thx!

phalt commented 3 years ago

I don't know how I missed this question, apologises.

Personally (emphasis - my personal opinion) I'd have the reserve method on the service layer, and not the model layer. This would make the Model a thin model.