const mixinafIoc::Scope
afIoc::Scope
@
Js
(Service) - Creates and manages service instances, and performs dependency injection. Scopes may also create child scopes.
Scopes may be dependency injected. Use standard injection to receive the Scope used to create the class instance.
@Inject private Scope scope
Or to always receive the current active Scope, use a Lazy Func.
@Inject private |->Scope| scope
- build
abstract Obj build(Type type, Obj?[]? ctorArgs := null, [Field:Obj?]? fieldVals := null)
Autobuilds an instance of the given type. Autobuilding performs the following:
- creates an instance via the ctor marked with
@Inject
or the best fitting ctor with the most parameters - inject dependencies into fields (of all visibilities)
- calls any method on the class annotated with
@PostInjection
ctorArgs
(if provided) will be passed as arguments to the constructor. Constructor parameters should be defined in the following order:new make(<config>, <ctorArgs>, <dependencies>, <it-block>) { ... }
Note that
fieldVals
are set by an it-block function, should the ctor define one.- creates an instance via the ctor marked with
- callFunc
abstract Obj? callFunc(Func func, Obj?[]? args := null)
Calls the given func. Any func arguments not given are resolved as dependencies. Func parameters should be defined in the following order:
|<args>, <dependencies>, <default params>| { ... }
Note that nullable and default parameters are treated as optional dependencies.
Returns the result of calling the func.
- callMethod
abstract Obj? callMethod(Method method, Obj? instance, Obj?[]? args := null)
Calls the given method. Any method arguments not given are resolved as dependencies.
instance
may benull
if calling a static method. Method parameters should be defined in the following order:Void myMethod(<args>, <dependencies>, <default params>) { ... }
Note that nullable and default parameters are treated as optional dependencies.
Returns the result of calling the method.
- createChild
abstract Scope? createChild(Str scopeId, |Scope? f := null)
Creates a nested child scope and makes it available to the given function, which is called straight away. The child scope also becomes the active scope for the duration of the function.
scope.createChild("childScopeId") |Scope childScope| { ... }
If a function is passed in then
null
is returned because the scope is not valid outside of the function.Advanced users may create non-active scopes by not passing in a function and using the returned scope. Non-active scopes must be manually destroyed.
myScope := scope.createChild("myScope") ... use myScope ... myScope.destroy
- destroy
abstract Void destroy()
Destroys this scope and releases references to any services created. Calls any scope destroy hooks.
- id
abstract Str id()
Returns the unique
id
of this Scope.- inject
Injects services and dependencies into fields of all visibilities and calls any method on the class annotated with
@PostInjection
.Returns the object passed in for method chaining.
- isThreaded
abstract Bool isThreaded()
Returns
true
if this scope is threaded and may hold non-const services.- jailBreak
abstract This jailBreak()
Jail breaks an active scope so it may be used from outside its closure.
childScope := (Scope?) null scope.createChild("childScopeId") |childScopeInClosure| { childScope = childScopeInClosure.jailbreak } ... use childScope here ... childScope.serviceByType(...) childScope.destroy
Once jailbroken, you are responsible for calling
destroy()
.- parent
abstract Scope? parent()
Returns the parent scope.
- registry
abstract Registry registry()
Returns the registry instance this scope belongs to.
- serviceById
abstract Obj? serviceById(Str serviceId, Bool checked := true)
Resolves a service by its ID. Throws
IocErr
if the service is not found, unlesschecked
isfalse
.- serviceByType
abstract Obj? serviceByType(Type serviceType, Bool checked := true)
Resolves a service by its Type. Throws
IocErr
if the service is not found, unlesschecked
isfalse
.