lundi 11 septembre 2017

How to abstract Symfony 3 Entity logic?

Repository is not the correct place for it. As it says in the Symfony documentation, methods containing your query logic can then be stored in Repository class (http://ift.tt/2f0TzOW).

So, where you should stored Entity methods like create, update or delete to reuse it in other parts of the code?

For exemple, I want to reuse Product create logic:

$em = $this->getDoctrine()->getManager();

$product = new Product();
$product->setName('Keyboard');

$em->persist($product);
$em->flush();

I agree with this answer, http://ift.tt/2wVfLyp, to move this logic into a service.

My questions are:

  • Should I create a service (ProductManager) that contains all three methods (create, update, delete)? Or should I create three different services (ProductCreate, ProductUpdate, ProductDelete) to comply the Single Responsibility Principle?
  • Should I create a service for each Entity (ProductService, ItemService, etc.) or should I create a common service (EntityManager)?
  • Is there any other better solution?

Aucun commentaire:

Enregistrer un commentaire