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
asActive

Source

abstract Void asActive(|Scope f)

(Advanced Use Only)

Runs the given function with this Scope as the active one. Note this method does not destroy the scope.

Under normal usage, consider using createChild(...) instead.

build

Source

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.

callFunc

Source

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

Source

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 be null 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

Source

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| {
    ...
}

When a function is passed in then null is returned because the scope would not be 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

To create an active scope that remains active outside of the closure, use jailBreak().

destroy

Source

abstract Void destroy()

(Advanced Use Only)

Destroys this scope and releases references to any services created. Calls any scope destroy hooks. Pops this scope off the active stack.

destroy() does nothing if called more than once.

id

Source

abstract Str id()

Returns the unique id of this Scope.

inheritance

Source

abstract Scope[] inheritance()

Returns a recursive list of all the Scopes this Scope inherits from. The result list always starts with this Scope itself.

syntax: fantom scope.inheritance() // --> ui, root, builtIn

inject

Source

abstract Obj inject(Obj obj)

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.

isDestroyed

Source

abstract Bool isDestroyed()

Returns true if this Scope has been destroyed.

isThreaded

Source

abstract Bool isThreaded()

Returns true if this scope is threaded and may hold non-const services.

jailBreak

Source

abstract This jailBreak()

(Advanced Use Only)

Jail breaks an active scope so it remains active outside its closure. Jail broken scopes are not destroyed so you are responsible for calling destroy() yourself.

childScope := (Scope?) null

scope.createChild("childScopeId") |childScopeInClosure| {
    childScope = childScopeInClosure.jailbreak
}

// --> "childScopeId" is still active!
echo(scope.registry.activeScope)

... use childScope here ...
childScope.serviceByType(...)

childScope.destroy

Note that jail broken scopes are only active in the current thread, but will remain the active scope even if the thread is re-entered.

parent

Source

abstract Scope? parent()

Returns the parent scope.

registry

Source

abstract Registry registry()

Returns the registry instance this scope belongs to.

serviceById

Source

abstract Obj? serviceById(Str serviceId, Bool checked := true)

Resolves a service by its ID. Throws IocErr if the service is not found, unless checked is false.

serviceByType

Source

abstract Obj? serviceByType(Type serviceType, Bool checked := true)

Resolves a service by its Type. Throws IocErr if the service is not found, unless checked is false.