Home > database >  Best practices in DDD to validate an object
Best practices in DDD to validate an object

Time:09-01

Let's say I have an entity called Order. My Domain must be certain that, as it receives an Order to be included, the Client associated with is active.

What are the best practices to validate that?

In other words, what are the best practices to validate some entity related to the one that is being validated? Where should that validation be called?

CodePudding user response:

Ideally: your decision making (computation, branching, and so on) would be in the domain model. Your retrieval of the information would be in your application code, and your application code would pass the information to the domain model as an argument.

Your domain entity holds its information, and everything else it needs to know gets passed to it.

The life-isn't-always-ideal version: if proactively fetching Client::isActive is expensive, then you probably don't want to always fetch the information. So you either (a) design a protocol such that the domain model tells the application code that it needs this information, and the application code fetches the information and passes it back or (b) you pass to the domain model the capability to look up the information for itself (aka, pass a "domain service" that knows how to fetch the information on demand).

The two approaches have different tradeoffs regarding code complexity, timing, error handling.

  • Related