1

Let's say I have a User class described as follows:

record User (
  Guid Id,
  string Username,
  string Password
) 
{
  /* some methods */
};

Let's say Usernames and Passwords need at the very least 8 characters before being considered valid. Should the domain be concerned with validating that? Or the application layer?

0

2 Answers 2

0

Let's say Usernames and Passwords need at the very least 8 characters before being considered valid. Should the domain be concerned with validating that? Or the application layer?

Replacing "information represented using general purpose data structures" with "information represented using domain values" normally happens in the application layer.

Key points:

If you consider a web interface, for instance, conversion of the request-body into an internal representation of a message will normally happen at the application layer. We will have some well defined schema, that says that the password embedded in the message has to be a "string" with length between 8 and 64 (or whatever).

Having made this check, we normally replace the string type with a domain value that guarantees this constraint (the underlying data structure will normally be the same, we just hide it behind a facade with tighter constraints.

1
0

Validation has a very valuable purpose, but validation can also be obtrusive to code readability. Often, validation logic can take up more visual space and drown out the actual work to be done.

Think of it as a way to prevent dirt on your floor. Considering the effort required, it makes more sense to place a floor mat on each entry point (door) of the room, rather than having to always worry about having dirt on your feet when walking around the room.

Domains try to be as distraction-free as they can so that you can focus on the intended business logic; and therefore the domain is usually not the right place to validate your input. The application layer almost inherently exists to take care of the 'fuss' that surrounds (but is not directly part of) the core logic in your domain.

That being said, contextual scenarios can differ. For example, if part of your application's main purpose is to e.g. have the end user configure their own validation rules which you then need to apply to other operations being performed (team-oriented tools like Jira and Gitlab are great examples here); then the parsing of those validation rules could reasonably be part of your domain. But this is a very specific example where validation configuration is considered a functional requirement for the end users and not just a technical implementation detail for the developers.

In cases where the validation is an implementation detail, there are better places than the domain to put it. Also note that there is no specific singular place where all validation should occur:

  • Validation for the input of your domain and business rule validation is generally appropriate in the application layer as this covers all angles from which the domain will be used.
  • Validation for incoming web request models should happen on the web project itself.
  • It's generally speaking a nicer user experience to also duplicate your user-oriented validation in the frontend so that the user receives immediate feedback, not just after they've filled out the entire form and attempted to submit it.

Not the answer you're looking for? Browse other questions tagged or ask your own question.