All pages
Powered by GitBook
1 of 4

Loading...

Loading...

Loading...

Loading...

Custom Scopes

WireBox allows you to create your own object persistence scopes so you can have full control on their lifecycle. This is easily done in the following process:

  1. Create a CFC that implements wirebox.system.ioc.scopes.IScope

  2. Register your custom scope in your configuration binder

You can create your own persistence scope or if you are getting funky, override the internal persistence scopes with your own logic.

Registering a Custom Scope

customScopes = {
    ortus = "path.model.dsl.OrtusScope"
};

or
mapScope("ortus","path.model.dsl.OrtusScope");

Now I can use the ortus scope in my mappings DSL and even my annotations, isn't that cool!

component scope="ortus"{

}

// map it
map("Luis")
    .to("model.path.LuisService")
    .into("Ortus");

The Scope Interface

The scope interface can be found here: coldbox.system.ioc.scopes.IScope.

Please note that you DO NOT need to add the implements to your code. We actually highly suggest you don't. There are many issues with interfaces yet in multiple CFML engines. So we do runtime checks for it, instead at compile time.

/**
 * Copyright Since 2005 ColdBox Framework by Luis Majano and Ortus Solutions, Corp
 * www.ortussolutions.com
 * ---
 * The main interface to produce WireBox storage scopes
 **/
interface {

	/**
	 * Configure the scope for operation and returns itself
	 *
	 * @injector             The linked WireBox injector
	 * @injector.doc_generic coldbox.system.ioc.Injector
	 *
	 * @return coldbox.system.ioc.scopes.IScope
	 */
	function init( required injector );

	/**
	 * Retrieve an object from scope or create it if not found in scope
	 *
	 * @mapping             The linked WireBox injector
	 * @mapping.doc_generic coldbox.system.ioc.config.Mapping
	 * @initArguments       The constructor struct of arguments to passthrough to initialization
	 */
	function getFromScope( required mapping, struct initArguments );


	/**
	 * Indicates whether an object exists in scope
	 *
	 * @mapping             The linked WireBox injector
	 * @mapping.doc_generic coldbox.system.ioc.config.Mapping
	 *
	 * @return coldbox.system.ioc.scopes.IScope
	 */
	boolean function exists( required mapping );

}

Scoping Process

The scoping process must be done by using some of the referenced injector's methods:

  • buildInstance(mapping, initArguments)

  • autowire()

These methods must be called sequentially in order to avoid circular reference locks. The first method buildInstance is used to construct and initialize an object instance. The autowire method is used then to process DI and AOP on the targeted object. Let's look at the RequestScope object:

Caution Always make sure that you use the buildInstance method and then store the results in the scope before wiring is done to avoid endless loops errors.

/**
 * Copyright Since 2005 ColdBox Framework by Luis Majano and Ortus Solutions, Corp
 * www.ortussolutions.com
 * ---
 * A scope that leverages the request scope
 *
 * @see coldbox.system.ioc.scopes.IScope
 **/
component accessors="true" {

	/**
	 * Injector linkage
	 */
	property name="injector";

	/**
	 * Log Reference
	 */
	property name="log";

	/**
	 * Configure the scope for operation and returns itself
	 *
	 * @injector             The linked WireBox injector
	 * @injector.doc_generic coldbox.system.ioc.Injector
	 *
	 * @return coldbox.system.ioc.scopes.IScope
	 */
	function init( required injector ){
		variables.injector = arguments.injector;
		variables.log      = arguments.injector.getLogBox().getLogger( this );
		return this;
	}

	/**
	 * Retrieve an object from scope or create it if not found in scope
	 *
	 * @mapping             The linked WireBox injector
	 * @mapping.doc_generic coldbox.system.ioc.config.Mapping
	 * @initArguments       The constructor struct of arguments to passthrough to initialization
	 */
	function getFromScope( required mapping, struct initArguments ){
		var cacheKey = "wirebox:#arguments.mapping.getName()#";

		// Check if already in request scope
		if ( NOT structKeyExists( request, cacheKey ) ) {
			// some nice debug info.
			if ( variables.log.canDebug() ) {
				variables.log.debug(
					"Object: (#arguments.mapping.getName()#) not found in request scope, beginning construction."
				);
			}

			// construct it and store it, to satisfy circular dependencies
			var target          = variables.injector.buildInstance( arguments.mapping, arguments.initArguments );
			request[ cacheKey ] = target;

			// wire it
			variables.injector.autowire( target = target, mapping = arguments.mapping );

			// log it
			if ( variables.log.canDebug() ) {
				variables.log.debug(
					"Object: (#arguments.mapping.getName()#) constructed and stored in Request scope."
				);
			}

			return target;
		}

		return request[ cacheKey ];
	}


	/**
	 * Indicates whether an object exists in scope
	 *
	 * @mapping             The linked WireBox injector
	 * @mapping.doc_generic coldbox.system.ioc.config.Mapping
	 *
	 * @return coldbox.system.ioc.scopes.IScope
	 */
	boolean function exists( required mapping ){
		var cacheKey = "wirebox:#arguments.mapping.getName()#";
		return structKeyExists( request, cacheKey );
	}

}