Here is some reading on what a service is and how it plays a role as a “model” in your application.
http://www.aliaspooryorik.com/blog/index.cfm/e/posts.details/post/oop-terminology-part-viii-service-layer-156
At face value, it might seem as though creating a service and injecting that into your view is basically the same thing as creating a helper and including it, but that would be like comparing a bicycle to a Ducati. In reality, a service wouldn’t even contain a cfquery, but would instead be the API or gateway to your business logic that would in turn gather data from the appropriate bean or DAO. That being said, I don’t want to belabor you with intricacies you aren’t ready for or simply don’t care about right now.
My recommendation would be to
- Take your poolingpartners CFC you created and renamed it something like PoolingPartnersService, leaving the singleton annotation in place.
- Next, var scope your “count” variable. Singletons need to be thread safe so multiple requests can concurrently execute the same method without bleeding over variables.
- Now, drop the CFC into the “models” folder in the root of your app. The models folder is the default scan location for WireBox, which is the DI/ioc object factory in ColdBox.
- Then, in the handler that needs access you can add this property to the top of the handler (inside the cfcomponent tag)
This will tell WireBox to create an instance of your new service and inject it into the variables scope of your handler. - Now, in your action (method) of the handler, you can pass along the service to the view with the following code:
- In your view, you can reference #rc.PoolingPartnersService.getPartners()#
An alternative approach if you don’t need the service for any reason in the handler and just for the view, would be to skip steps 4 and 5 and simply call
<cfset PoolingPartnersService = getModel(“PoolingPartnersService”)>
at the top of your view. This will directly ask WireBox for the service instead of placing a permanent reference in your handler. (handlers are singletons too)
Let me know if I need to explain any of that further.
Here’s some more reading on WireBox:
http://wiki.coldbox.org/wiki/WireBox.cfm
and models integration into ColdBox
http://wiki.coldbox.org/wiki/Models.cfm
Thanks!
~Brad
ColdBox Platform Evangelist
Ortus Solutions, Corp
E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com