Passing ColdSpring in as an argument?

8 views
Skip to first unread message

Nolan Erck

unread,
Apr 8, 2010, 8:42:12 PM4/8/10
to cfc...@googlegroups.com
I've got a handful of Service Layer CFCs that all take your pretty standard set of arguments on the init() method:

<cfargument name="userDAO" type="model.User.userDAO" required="true" />
<cfargument name="userGateway" type="model.User.userGateway" required="true" />

Once in a while I'll find that I'm in (for example) the UserService, and I want to fire off a method in one of the *other* Service CFCs.  Since all of them are wired up via ColdSpring, I could just do something like so, inside each Service CFC:

<cfset foo = application.coldSpring.getBean( "someOtherService" ) />

....but that's breaking encapsulation.  Is there a was pass IN a reference to ColdSpring via the init() arguments?  This strikes me as probably a circular logic problem (how can ColdSpring be passed in as an argument if it hasn't finished initializing itself yet?), but I'm hoping there is a workaround.  Does that make sense? 

How do the rest of you access ColdSpring from within a Service Layer that itself was instantiated via ColdSpring?

Thanks,
Nolan



sipa...@gmail.com

unread,
Apr 8, 2010, 8:47:25 PM4/8/10
to cfc...@googlegroups.com
Nolan,

I'd lean away from passing in Coldspring into your model.

I know it might seem like an OK shortcut, but it is limiting, brittle and breaks encapsulation.

Things should only know what they need to know to get the job done. Being ColdSpring aware seems to be a bit much.

By being explicit and configuring your model with exactly what it needs will leave you a great application blueprint to reference as you grow and maintain your application.


At all costs, strive to leave these blueprints as detailed as possible and avoid shortcuts, because they are rarely as short as they seem, and they have pointy teeth :)

DW

Sent from my Verizon Wireless BlackBerry


From: Nolan Erck <nolan...@gmail.com>
Date: Thu, 8 Apr 2010 17:42:12 -0700
Subject: [CFCDEV] Passing ColdSpring in as an argument?
--
You received this message because you are subscribed to the Google Groups "CFCDev" group.
To post to this group, send email to cfc...@googlegroups.com.
To unsubscribe from this group, send email to cfcdev+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cfcdev?hl=en.

Kevin Pepperman

unread,
Apr 8, 2010, 8:47:53 PM4/8/10
to cfc...@googlegroups.com
Just add a setBeanFactory() method to your bean and ColdSpring will inject the reference to itself.



--
/Kevin Pepperman

"They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety." - Benjamin Franklin

Matt Quackenbush

unread,
Apr 8, 2010, 8:49:29 PM4/8/10
to cfc...@googlegroups.com
Well, to answer your specific question, you pass ColdSpring in by having a public setter for BeanFactory.

<cffunction name="setBeanFactory" access="public">
     <cfargument name="beanFactory" required="yes" type="coldspring.beans.DefaultXmlBeanFactory" />
     <cfset instance.beanFactory = arguments.beanFactory />
</cffunction>

However, why not just simply wire the other service(s) in?  That would be a much better solution, imo.

John Whish

unread,
Apr 9, 2010, 3:46:08 AM4/9/10
to cfc...@googlegroups.com
Just a quick point on using setBeanFactory, is that your argument must have a type of "coldspring.beans.DefaultXmlBeanFactory" otherwise ColdSpring will ignore it when it is checking the metadata and won't inject.

Sean Corfield

unread,
Apr 9, 2010, 11:00:01 PM4/9/10
to cfc...@googlegroups.com
Why not use ColdSpring to inject the services you need?

Add setFooService() to your CFC and <property> to the ColdSpring
definition for that service.

Dependency Injection is what ColdSpring's all about.

Unless you are dynamically fetching services from ColdSpring, you
should never need a reference to it in your CFCs.

Sean

Reply all
Reply to author
Forward
0 new messages