RE: [coldbox:3214] Re: Auto Wiring Transients in Service Layer

But if it's working for you, I'm just curious on an academic level
regarding the 'how'. :slight_smile:

It might *appear* to work on your dev box if you have model caching
turned off since your singletons are re-created (and re-wired) on every
page request. Production, of course, would have model caching turned



You’re right, I wasn’t taking into account production settings, just development. What Brad was saying is that User.cfc is cached, not because of a setting in User.cfc, but b/c UserService.cfc is cached as a singleton, thus, User.cfc is loaded into UserService.cfc’s instance scope, via autowire, when it’s created.

BeanFactory seems the way to go here for creating new objects.

I apologize for any confusion I may have caused.

Now, this brings up a question I have - How does it work when Even Handler caching is turned on?



What is the advantage of using the BeanFactory over using CreateObject
for transients?

I use CB's DI framework to do model integration. In my Service layer,
I inject my DAO and Gateway and any other singletons I need, but I
don't inject the beans themselves and use CreateObject for the beans
instead. So if I had a UserService, I'd have something like this:

<cfcomponent name="UserService" output="false" cache="true" cachetimeout="30">
<cfproperty name="UserGateway" type="model" scope="instance" />
<cfproperty name="UserDAO" type="model" scope="instance" />

<cffunction name="createUser" access="public" output="false" returntype="User">
    <cfargument name="id" type="numeric" required="false" />
    <cfargument name="uuid" type="string" required="false" />
    <cfargument name="first_name" type="string" required="false" />
    <cfargument name="last_name" type="string" required="false" />
    <cfset var User =
createObject("component","User").init(argumentCollection=arguments) />
    <cfreturn User />

  <cffunction name="getUser" access="public" output="false" returntype="User">
    <cfargument name="id" type="numeric" required="true" />
    <cfset var User = createUser(argumentCollection=arguments) />
    <cfset />
    <cfreturn User />

Since I'm using CreateObject and my bean doesn't have any caching on
it, it should create a new transient every time. Thus far my approach
seems to work. What, then, do you think the advantage of using the
BeanFactory would be?


Some benefits for me have been that I can inject the transient using
an alias. That ensures it will still work when I have had to change
the locations of the physical files.

Additionally, I can still use coldbox model integration in transients
whereas createobject would break that.

- Gabriel