2

In DDD, the Application layer is supposed to just perform coordination tasks, whereas the Domain layer is responsible of validating the business rules.

My question is about validating the domain object properties. For example, I need to validate that a required property has some value in it before persisting it to the database through repositories.

In terms of DDD, is it acceptable to perform this sort of property validation in the Application layer?

user11081980
  • 3,059
  • 4
  • 30
  • 48

3 Answers3

4

Kinds of validation

In the situation you describe, there are two different validation steps that you need to consider separately:

  • Input validation. This is the responsibility of an app service. The goal is to ensure that no garbage or harmful data enters the system.
  • Protecting model invariants. This is your domain logic. Whenever something in the domain changes, you need to make sure that the changes are valid within your domain, i.e. all invariants still hold.

Validating domain invariants as part of an app service

Note that sometimes you also want to validate domain invariants in an app service. This could be necessary if you need to communicate invariant violations back to the client. Doing this in the domain would make your domain logic client-specific, which is not what you want.

In this situation, you need to take care that the domain logic does not leak into the app service. One way to overcome this problem and at the same time make a business rule accessible to both the domain and the app service is the Specification Pattern.

Here is an answer of mine to another question that shows an example implementation for the specification pattern.

Community
  • 1
  • 1
theDmi
  • 17,546
  • 6
  • 71
  • 138
3

You can validate incoming data in your ui layer. For example you can you symfony forms validation or just check for necessary data inside your layer with Rest.

What about Domain Layer, it depends.
You didn't precise what kind of domain object it is.

Mostly you do such kind of validation by creating Value Object, with creation logic inside. For example Email Value Object, you can't create wrong one, otherwise it will throw exception.

Aggregates can perform validation before executing method and it's called invariants. For example, user has method becomeVIP, inside a method there is constraint, that only user with name 'Andrew', can become a VIP. So you don't do validation after the action, but before the action. You don't let your aggregate go into wrong state.

If you have logic, which is not correlated with aggregate you put it in domain service, for example email uniqueness check.

Dariss
  • 1,258
  • 1
  • 12
  • 27
1

Rather than "validating hat a required property has some value in it" at the periphery of the Domain, I prefer to make sure that it can never become null in the Domain the first place.

You can do that by forcing consumers of the constructors, factories and methods of that entity to always pass a value for the property.

That being said, you can also enforce it at the Application level and in the Presentation layer (most web application frameworks provide convenient ways of checking it these days). Better 2 or 3 verifications than one. But the domain should be the primary source of consistency.

guillaume31
  • 13,738
  • 1
  • 32
  • 51