Danil
Danil

Reputation: 1893

validation of persistent ignorant domain objects

I want to know how to check if Domain Object is in right state while saving it. If Object has persistent information - it is clear as for instance I can check Order's amount in its Save method. However persistent ignorant objects don't have Save method. They just have data and behavior. Should I rely on developers' accuracy or I need to check all entities graphs somehow before saving it in database?

Upvotes: 0

Views: 324

Answers (1)

sfinnie
sfinnie

Reputation: 9952

Perhaps I'm not understanding you right.

I'm not sure how you get an "ignorant" object. That seems to run against the core tenet of encapsulation. The only way to modify an object's state should be through its interface. It's the interface's responsibility to ensure any updates to state are valid. So, to your example, you shouldn't need to check the amount in the save() operation (assuming here "save" means persist). It should be updated as a consequence of e.g. calling addProduct() (or something similar - i.e. adding item to order updates amount).

Whether an object is persistent or transitory is irrelevant - it should always encapsulate state update through its interface.

Hope that helps - apologies if I've misunderstood.

Upvotes: 1

Related Questions