T
T
tukreb2020-07-24 16:12:52
PHP
tukreb, 2020-07-24 16:12:52

How to optimize complex checks in Entity?

There is an Entity with the add()
method In this method, there is a complex check that pulls objects from different Entities through Doctrine. On pure SQL, of course, you can make faster queries, but this cannot be done in Entity.
If you put this in a service, then the add() method in the Entity is left without checks, and someone or I can accidentally call it bypassing the service.
How do they act in such situations? Do they inject the required services through private constructors into Enity and then call them in the required method? Or write static service methods? But if you believe this https://stackoverflow.com/questions/10330704/symfo... then you can't do that.

In general, what is the best practice here?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-07-24
@tukreb

Alternatively, you can only move the check to a separate service in the Domain Layer, and pass this service as an argument to the add () method of your entity. If this service throws an exception, then the add method stops executing. In the service, pass in the constructor a repository or another object that will load the data necessary for verification.
Next, move the call to the add () method to the service at the Application Layer (Application Layer), as you wrote, in which and pass the previously created verification service to the add () method.
In this implementation, you will always need this domain service, and you will not be able to call the add() method without it.
Example:

class Entity
{
  public function add(ComplexEntityValidator $validator) {
    $validator->validate();
  }
}

class ComplexEntityValidator
{
  public function __construct(EntityRepository $repository) {

  }

  public function validate() {
    // проверка
  }
}

class EntityApplicationService
{
  public function __construct(ComplexEntityValidator $validator) {

  }

  public function __invoke() {
    $entity = new Entity();
    $entity->add($validator);
    // сохранение сущности
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question