Auto Wiring Transients in Service Layer

Hi,

Is is possible to use ColdBox as a Transient Factory?

I looked at the example on the cbModelGuid wiki page [http://
ortus.svnrepository.com/coldbox/trac.cgi/wiki/cbModelGuide] and it is
creating the User with a CreateObject function.

I know that I could use the IoC plugin and Lightwire as a Transient
Factory, but can you use the GetModel("User") to do something like:

<cfcomponent name="UserService" output="false" cache="true"
cacheTimeout="0">

  <!--- Dependencies --->
  <cfproperty name="SessionStorage"
type="coldbox:plugin:sessionstorage" scope="instance" />
  <cfproperty name="HandlerService" type="coldbox:handlerService"
scope="instance" />

  <cfscript>
  instance = structnew();
  </cfscript>

  <cffunction name="init" output="false" returntype="UserService">
    <cfreturn this>
  </cffunction>

  <cffunction name="getUserSession" output="false" access="public"
returntype="User"
                        hint="This method checks if a user is in an
authorized session, else it returns the default user object.">
        <cfscript>
                var oUser = "";

                //Is user in session
                if ( instance.sessionstorage.exists( 'CurrentUser' ) )
{
                        oUser = instance.sessionstorage.getVar
( 'CurrentUser' );
                }
                else{
                        oUser = instance.HandlerService.GetModel
("User");
                }

                /* Return User Object */
                return oUser;
        </cfscript>
  </cffunction>

</cfcomponent>

Thanks!

I think I've answered my own question. You can do it by injecting the
BeanFactory plugin.

<cfproperty name="BeanFactory" type="coldbox:plugin:BeanFactory"
scope="instance" />

Neat!

John,

As long as you don’t specify the cache=“true” meta data in your model objects, a transient will always be created.

Let’s say I have a User.cfc:

And in my handler, I want to get it:

In this case, rc.user1 and rc.user2 both represent different objects. (When you dump them, rc.user2 will have empty properties)

But, it if I add cache=“true” to my User.cfc:

User.cfc then becomes a singleton and rc.user1 and rc.user2 reference the same object.

Looking at your code, you probably don’t need to wire the Handler Service, instead, just add

and simply do
else{
oUser = instance.User

HTH,
Dutch

Hi John,

<!--- Transient: Used on demand --->
<cfcomponent name="UserGroup">

The coldbox provide three different scopes for DSL "instance,
variables, this"

Thanks
Sana

Dutch,

I use the beanfactory to handle transients because I experience the
opposite if I use your method. If I use your method with production
settings, the object is cached even though I have 'cache="false"' in
it.

Again, I use the beanfactory so I'm not currently experiencing a
problem but if your method is working for you with production
settings, I'll have to look into why it doesn't work with my settings.
Interesting....

- Gabriel