Listeners calling more than 1 service?

0 views
Skip to first unread message

Matthew

unread,
Sep 15, 2009, 7:12:48 AM9/15/09
to Mach-II for CFML
Hi guys

I'm using MachII + ColdSpring + Transfer.

Do people generally take the following pattern when saving form data
to a database. Say we are updating a User
*** Listener Code (version 1) ***
var oUser = getUserService().getUser(event.getArg('user_id'));
oUser.setFirstName(event.getArg('first_name'));
getUserService().saveUser(oUser);

So what about if a User has a m2m relationship with a PhotoGallery;
1) Do you call the PhotoGalleryService straight from the UserListener?
2) Would the PhotoGalleryService be dependency injected with
ColdSpring?
3) So each object which is related to a User is therefore dependent,
which means the service for each of these dependencies needs to be
injected into the UserListener?
*** Listener code (version 2) ***
var oUser = getUserService().getUser(event.getArg('user_id'));
var oPhotoGallery = getPhotoGalleryService().getGaller(event.getArg
('gallery_id'));
oUser.setFirstName(event.getArg('first_name'));
oUser.setGallery(oPhotoGallery);
getUserService().saveUser(oUser);

Cheers
Matthew

jlcox

unread,
Sep 15, 2009, 8:53:39 AM9/15/09
to Mach-II for CFML
I'd call the PhotoGallery service from the UserService (or v/v).

Matthew Woodward

unread,
Sep 15, 2009, 10:08:22 AM9/15/09
to mach-ii-for...@googlegroups.com
jlcox wrote:
> I'd call the PhotoGallery service from the UserService (or v/v).
>

I'd do the same. The question I always ask myself is if what I'm dealing
with in my listener is more business logic or application flow logic.
Having to persist one-to-many or many-to-many relationships is
definitely part of the business logic in my mind, so that belongs in the
service, not in the listener.

--
Matthew Woodward
ma...@mattwoodward.com
http://mpwoodward.posterous.com
identi.ca/Twitter: @mpwoodward

Please do not send me proprietary file formats such as Word, PowerPoint, etc. as attachments.
http://www.gnu.org/philosophy/no-word-attachments.html

Matthew

unread,
Sep 15, 2009, 7:53:19 PM9/15/09
to Mach-II for CFML
@Matthew, @ jlcox: Thanks for the reply guys. Makes sense. So how
would you script this? Would you have a 'getGallery' method on the
User service or would you pass the 'saveUser' method the 'gallery_id'
attribute and it does the rest:
*** Listener code (version 3) ***
var oUser = getUserService().getUser(event.getArg('user_id'));
var oPhotoGallery = getUserService().getGallery(event.getArg
('gallery_id')); // *** THIS WOULD CALL THE PHOTO GALLERY SERVICE FROM
THE USER SERVICE***
oUser.setFirstName(event.getArg('first_name'));
oUser.setGallery(oPhotoGallery);
getUserService().saveUser(oUser);
*** Listener code (version 4) ***
var oUser = getUserService().getUser(event.getArg('user_id'));
oUser.setFirstName(event.getArg('first_name'));
oUser.setGallery(oPhotoGallery);
getUserService().saveUser(oUser,event.getArg('gallery_id')); // ***
HERE I AM PASSING IN THE GALLERY ID FOR THE SAVEUSER METHOD TO GET AND
ASSIGN THE PHOTO GALLERY OBJECT ***

I don't really like either solution because the 1st option requires a
duplicate getGallery method and the 2nd option doesn't scale very well
i.e. as you get more dependencies you have to pass the 'saveUser'
method more and more id's of the dependent objects.

Cheers
Matthew

On Sep 16, 12:08 am, Matthew Woodward <m...@mattwoodward.com> wrote:
> jlcox wrote:
> > I'd call the PhotoGallery service from the UserService (or v/v).
>
> I'd do the same. The question I always ask myself is if what I'm dealing
> with in my listener is more business logic or application flow logic.
> Having to persist one-to-many or many-to-many relationships is
> definitely part of the business logic in my mind, so that belongs in the
> service, not in the listener.
>
> --
> Matthew Woodward
> m...@mattwoodward.comhttp://mpwoodward.posterous.com

Matthew

unread,
Sep 15, 2009, 8:06:02 PM9/15/09
to Mach-II for CFML
Further to my last post: would it be even better to take a totally
different approach and pass all form/event data through to the service
layer as a Struct so that it has all the population logic?

*** Listener code (version 5) ***
var sData = StructNew();

sData['userId] = event.getArg('user_id');
sData['firstName] = event.getArg('first_name');
sData['galleryId] = event.getArg('gallery_id');

getUserService().saveUser(sData);

Cheers
Matthew

jlcox

unread,
Sep 16, 2009, 8:40:35 AM9/16/09
to Mach-II for CFML
Use ColdSpring to inject the PhotoGalleryService into your
UserService. It looks like you're maybe trying to do too much in your
listener. Your listener *should* be fairly simple, just passing the
necessary event args into the service. Let the service itself do the
heavy lifting.
Reply all
Reply to author
Forward
0 new messages