Last active
December 15, 2015 16:47
-
-
Save cameroncf/4cf1c83e73efbb9246c9 to your computer and use it in GitHub Desktop.
Using a Singleton Factory to front DI/1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component | |
extends = 'libs.framework.one' | |
{ | |
/* other content trimmed */ | |
/* gets a cached singleton */ | |
public any function getService( required string serviceName ) { | |
return createObject('component','model.SingletonFactory').getObject( arguments.serviceName ); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component { | |
/* all TRANSIENTS extend this */ | |
/* gets a cached singleton */ | |
public any function getService( required string serviceName ) { | |
return createObject('component','model.SingletonFactory').getObject( arguments.serviceName ); | |
} | |
public any function getBeanFactory() { | |
return application[ request.framework_applicationKey ].factory; // getting the FW/1 beanfactory (cheating? yes - but this works like a charm) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component | |
{ | |
public SingletonFactory function init() { return this; } | |
public any function getObject(required String objectName) { | |
// CREATE IF NEEDED | |
if ( !structKeyExists(application,'singletonFactory') or | |
!structKeyExists(application.singletonFactory, arguments.objectName) ) { | |
lock timeout = '10' | |
throwontimeout = 'No' | |
name = 'SingletonFactory_#arguments.objectName#' | |
type = 'EXCLUSIVE' { | |
if ( !structKeyExists(application,'singletonFactory') or | |
!structKeyExists(application.singletonFactory, arguments.objectName) ) { | |
if (not structKeyExists(application,'singletonFactory')) { | |
application.singletonFactory = structNew(); | |
} | |
if (not structKeyExists(application.singletonFactory, arguments.objectName)) { | |
application.singletonFactory[arguments.objectName] = getBeanFactory().getBean( arguments.objectName ); | |
} | |
} | |
} // lock | |
} // if | |
// RETURN CACHED BEAN | |
lock timeout = '10' | |
throwontimeout = 'No' | |
name = 'SingletonFactory' | |
type = 'READONLY' { | |
if ( structKeyExists(application,'singletonFactory') and | |
structKeyExists(application.singletonFactory, arguments.objectName) ) { | |
return application.singletonFactory[arguments.objectName]; | |
} else { | |
throw 'Bean #arguments.objectName# does not exist in the singleton factory.'; | |
} | |
} | |
} | |
public void function reset(String objectName) { | |
structDelete(application,'singletonFactory'); | |
} | |
private any function getBeanFactory() { | |
return application[ request.framework_applicationKey ].factory; // getting the FW/1 beanfactory (cheating? yes - but this works like a charm) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment