Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony fetch/persist entity-oriented service naming conventions and best practice

A Symfony2 application typically has a collection of entities. A Doctrine EntityManager is commonly used to fetch and persist these entities.

Entities are used in multiple places throughout the application; for many entities, and per entity, it makes sense to wrap up the handling of a given entity plus fetching/persisting in a service.

For example, for a User entity there may be a UserService with fetchUser($user_id) and persistUser(User $user) methods (or maybe just fetch() and persist() methods, this is an example only).

An application can end up with many entity-oriented services for the fetching and persisting of entities. Such services will be similar in their interfaces, differing in the type of entity handled.

That an application can contain many entity-oriented services seems a common situation. It follows that the matter of naming and architecting such services is a common problem.

For a new application to require the creation of, for example, a base EntityService and child UserService, WidgetService and ProductService classes feels repetitive, as in the means for dealing with such aspects should be a solved problem.

  • Are there best practices for introducing such entity-management related services into a Symfony application?

    This feels like it should be a solved problem, with perhaps a well-trod design pattern to follow.

  • Are there naming conventions that would be advisable to follow?

    I've observed, in different applications, both `UserManager` and `UserService` chosen as service names. Are there prevailing conventions?

like image 601
Jon Cram Avatar asked Nov 13 '22 02:11

Jon Cram


1 Answers

As for the naming, there are no conventions that I know of for such situations, but there is the Doctrine\ORM\EntityManager class which manages entity, so I would use Manager instead of Service.

Now, talking about entity management services: do you really need so many services? If the UserService::persistUser() method does nothing more than just persisting the user, why are you using a custom service and not the default EntityManager?

Also, you shouldn't put repository and persisting methods all together in the same class. Isn't it easier to use a custom repository for the entity?

like image 100
Alessandro Desantis Avatar answered Dec 10 '22 06:12

Alessandro Desantis