Best way to use service in entity

42 views
Skip to first unread message

nicolas potier

unread,
Jan 21, 2013, 1:57:10 AM1/21/13
to pommp...@googlegroups.com
Hi,

I'm working on a Symfony2 projet with Pomm and PommBundle. I've reached a point where I will need to have access to a service inside a custom method declared inside my entity class. This entity is used in multiples controllers, so i would(nt like to inject the service inside each method of each controller. I would prefer doing it before.

What should be the cleaner way to do it ?

Thank you for your help :)

Grégoire Hubert

unread,
Jan 21, 2013, 3:50:50 AM1/21/13
to pommp...@googlegroups.com
Hi Nicolas,

Entities are dumb schemaless instances. This is the very big difference with ORMs and Pomm. If you need to inject more data when creating an entity, I would suggest you do that in a Map's method instead so you could optimize the number of queries issued.

What about if you need to do that on a collection of objects ? Well, this is often done either using SQL or using collection filters to ovoid having to fetch all the objects and inject the extra data in them.

This really depends on the case you are facing.

Cheers,
--
Grégoire HUBERT
Pomm non ORM http://pomm.coolkeums.org

Grégoire Hubert

unread,
Jan 22, 2013, 5:15:28 AM1/22/13
to pommp...@googlegroups.com
Do you mean sending an email or something like that ?

Nicolas Potier

unread,
Jan 22, 2013, 7:39:44 AM1/22/13
to pommp...@googlegroups.com
Hi Grégoire,

Thanks for the reply. Here is my situation.

My mission consists to rewrite a Web application with Symfony2 and Pomm. In a first time, the old (Legacy) and the new application will live together, and we try to reuse some logic of the Legacy application in the new application (with pseudo webservices calls).

In order to do that, I've created a Legacy Service. This service is able to call old pieces of code of the Legacy application.

Here is an example of the process that I would like to have :
  1. The controller load a HumanMap (instance of BaseObjectMap)
  2. The HumanMap is used to get a Collection of Humans. Let's say that each Human has a picture attribute.
  3. The human Collection is transmitted to the view, that will try to get access to picture thumbnails.
The legacy framework contains the method and the logic to locate or generate thumbnail pictures.
I would like to create a method called getThumbnails() in my Humain class, that would rely on this Legacy method, so I need to have access to my Legacy Service inside my Human Class. 
Because access to Human Objects is often made in differents Controllers, I would like to avoid repeated pieces of code in my Controllers (load Human, inject Legacy Service).

Thank you for your help, if you have any :)

Nicolas.

2013/1/22 Grégoire Hubert <huber...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "PommProject" group.
Visit this group at http://groups.google.com/group/pommproject?hl=en.
 
 



--
Nicolas Potier 
ACSEO - Conseil, Innovation, Expertise en Nouvelles Technologies
tél : 06.75.13.86.49 | email : nicolas...@acseo-conseil.fr

Grégoire Hubert

unread,
Jan 22, 2013, 7:53:07 AM1/22/13
to pommp...@googlegroups.com
On Tue, Jan 22, 2013 at 1:39 PM, Nicolas Potier <nicolas...@acseo-conseil.fr> wrote:
Hi Grégoire,

Thanks for the reply. Here is my situation.

[...] 
The legacy framework contains the method and the logic to locate or generate thumbnail pictures.
I would like to create a method called getThumbnails() in my Humain class, that would rely on this Legacy method, so I need to have access to my Legacy Service inside my Human Class. 
Because access to Human Objects is often made in differents Controllers, I would like to avoid repeated pieces of code in my Controllers (load Human, inject Legacy Service).


Services held in the dependency injection container are mostly here to be used in the controllers and may be injected in some libraries. Unless your service has a lot of dependencies, I would suggest you move that logic in an external library you can call whereever you want. 
Reply all
Reply to author
Forward
0 new messages