Data validation at the level of business logic of the program

Data validation at the level of business logic of the program

This article continues the topic of the article “When neither there nor here, or in search of the optimal boundary of the Domain layer”.

In many business applications, a standard requirement is put forward for the User entity: “When registering a user, it is necessary to check that the recorded email is unique, no other user has such an email.”

Such a task is extremely typical and therefore should have a typical solution. Next, we consider a solution based on a three-layer architecture, in which each layer consists of three sublayers. Such an architecture was described in the article “An example of a description of a multi-layer architecture based on the use of sets of sub-layers and a hierarchy of data models”.

As an example, consider a web service that receives a request to create a new user and store their data in the database.
Upon receiving a request, the web service functionality deserializes user data into a UserDTO type object. In this object, the Email field contains the email address of the new user. This e-mail address must be unique at the level of the corresponding field of the Users table, in which the application user data is stored in the database.

The scheme of the program for the use case of creating a new user is presented in the figure.

An external request to the web service to create a new user is processed in the facade layer, and then the application service functionality is called, which is the facade of the business logic of the application (logic layer).

The use case of creating a new user is implemented in the form of two calls to the logic layer functionality.

  1. Application service calls the functionality of the EmailValidator type object to validate the e-mail address of a new application user.

  2. If the validation was successful, the application service calls the persistence service function to insert the new user’s data into the database.

The EmailValidator type object implements the following functionality.

  1. Validates the value from the email field of a UserDTO object against the email address format.

  2. Calls the persistence service functionality to form a request to the database to check the uniqueness of the value of the Email field of the UserDTO type object at the level of the corresponding field of the Users table, in which the data of the program users is stored in the database.

Related posts