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:
1
/**
2
* Copyright Since 2005 ColdBox Framework by Luis Majano and Ortus Solutions, Corp
3
* www.ortussolutions.com
4
* ---
5
* A scope that leverages the request scope
6
*
7
* @see coldbox.system.ioc.scopes.IScope
8
**/
9
component accessors="true" {
10
11
/**
12
* Injector linkage
13
*/
14
property name="injector";
15
16
/**
17
* Log Reference
18
*/
19
property name="log";
20
21
/**
22
* Configure the scope for operation and returns itself
23
*
24
* @injector The linked WireBox injector
25
* @injector.doc_generic coldbox.system.ioc.Injector
26
*
27
* @return coldbox.system.ioc.scopes.IScope
28
*/
29
function init( required injector ){
30
variables.injector = arguments.injector;
31
variables.log = arguments.injector.getLogBox().getLogger( this );
32
return this;
33
}
34
35
/**
36
* Retrieve an object from scope or create it if not found in scope
37
*
38
* @mapping The linked WireBox injector
39
* @mapping.doc_generic coldbox.system.ioc.config.Mapping
40
* @initArguments The constructor struct of arguments to passthrough to initialization
41
*/
42
function getFromScope( required mapping, struct initArguments ){
43
var cacheKey = "wirebox:#arguments.mapping.getName()#";
44
45
// Check if already in request scope
46
if ( NOT structKeyExists( request, cacheKey ) ) {
47
// some nice debug info.
48
if ( variables.log.canDebug() ) {
49
variables.log.debug(
50
"Object: (#arguments.mapping.getName()#) not found in request scope, beginning construction."
51
);
52
}
53
54
// construct it and store it, to satisfy circular dependencies
55
var target = variables.injector.buildInstance( arguments.mapping, arguments.initArguments );
56
request[ cacheKey ] = target;
57
58
// wire it
59
variables.injector.autowire( target = target, mapping = arguments.mapping );
60
61
// log it
62
if ( variables.log.canDebug() ) {
63
variables.log.debug(
64
"Object: (#arguments.mapping.getName()#) constructed and stored in Request scope."
65
);
66
}
67
68
return target;
69
}
70
71
return request[ cacheKey ];
72
}
73
74
75
/**
76
* Indicates whether an object exists in scope
77
*
78
* @mapping The linked WireBox injector
79
* @mapping.doc_generic coldbox.system.ioc.config.Mapping
80
*
81
* @return coldbox.system.ioc.scopes.IScope
82
*/
83
boolean function exists( required mapping ){
84
var cacheKey = "wirebox:#arguments.mapping.getName()#";
85
return structKeyExists( request, cacheKey );
86
}
87
88
}
89
Copied!
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.
Copy link
Edit on GitHub