I heard a couple of times that putting business logic in database models is bad down the road.
I just read this in blog of a Rackspace employee: The Django ORM also tended to have us accumulate large amounts of business logic on the model objects, which made building strong service contracts even harder.
But it seems logical to me that if you have an Invoice
model to encapsulate a lot of invoicing logic there. Seems very similar to what we've been taught in OOP. Could you show me how this is wrong? Please include a practical example if possible.
Benefit of "put logic in models"
What I see as a big benefit of putting logic in models is that users of the models don't have to worry about how they can use stuff, they just use it and it works, can't break anything.
Let me elaborate with an example:
So if you don't put logic in models, then you probably have this code elsewhere, let's say it's in api
. If you want to change a state of an Invoice
, what do you do? Do you do invoice.state = new_state
or do you read the api
code to see if there's some method there that changes the state?
This is confusing and error prone. How do you fix this issue then?