mixinafIoc::Configuration

afIoc::Configuration

@Js

Use to add create and override service configuration contributions.

Every service may receive a list or ordered map of values; called its configuration. Any (external) module may contribute to this using in a configuration method.

The service defines the type of contribution by declaring a parameterised list or map in its ctor or builder method. Contributions must be compatible with this type. Example, the service SeaLions may define a configuration map of [Str:Penguin] with the following ctor:

class SeaLion {
    new make(Str:Penguin foodGroups) {
        ...
    }
}

Contribute to the SeaLion service in the AppModule:

const class AppModule {
    Void defineServices(RegistryBuilder bob) {
        bob.addService(SeaLion#)
    }

    @Contribute { serviceType=SeaLion# }
    Void contributeSeaLion(Configuration config) {
        config["kevin"] = Penguin()
    }
} 

Or use RegistryBuilder:

bob := RegistryBuilder()
bob.addService(
bob.contributeToServiceType(SeaLion#) |Configuration config| {
    config["kevin"] = Penguin()
}
add

Source

abstract Constraints add(Obj value)

Adds a value to the service configuration with optional ordering constraints.

Because the keys of added values are unknown, they cannot be overridden. For that reason it is advised to use set() instead.

value is coerced to the service's contribution type.

config.add(value)
addPlaceholder

Source

abstract Constraints addPlaceholder(Str key)

Adds a placeholder. Placeholders are empty configurations used to aid the ordering of actual values:

config.placeholder("end")
config.set("foo", val1).before("end")
config.set("bar", val2).after("end")

While not very useful in the same contribution method, they become very powerful when used across multiple modules and pods.

Placeholders do not appear in the the resulting configuration and are never seen by the end service.

build

Source

Obj build(Type type, Obj?[]? ctorArgs := null, [Field:Obj?]? fieldVals := null)

Convenience method for Scope.build; builds an instance of the given Type injecting in all dependencies.

inOrder

Source

abstract Constraints inOrder(|This f)

Defines a block where defined contributions are kept in the same order. The block itself may be ordered before and / or after other contributions:

config.inOrder { 
    config["b-1"] = 1
    config["b-2"] = 1
    config.addPlaceholder("separator")
    config["b-3"] = 1
}.before("c-1").after("a-1")
overrideValue

Source

abstract Constraints overrideValue(Obj existingKey, Obj? newValue, Obj? newKey := null)

Overrides and replaces a contributed value. The existing key must exist.

Note that when overriding, all existing ordering constraints are cleared and must be re-set.

existingKey is the id / key of the value to be replaced. It may have been initially provided by set() or have be the newKey of a previous override.

newKey does not appear in the the resulting configuration and is never seen by the end service. It is only used as reference to this override, so this override itself may be overridden. 3rd party libraries, when overriding, should always supply a newKey. newKey may be any Obj instance but sane and intelligent people will always pass in a Str.

newValue is coerced to the service's contribution type.

remove

Source

abstract Void remove(Obj existingKey, Obj? newKey := null)

A special kind of override whereby, should this be the last override applied, the value is removed from the configuration.

existingKey is the id / key of the value to be replaced. It may have been initially provided by set() or have be the newKey of a previous override.

newKey does not appear in the the resulting configuration and is never seen by the end service. It is only used as reference to this override, so this override itself may be overridden. 3rd party libraries, when overriding, should always supply a newKey. newKey may be any Obj instance but sane and intelligent people will always pass in a Str.

scope

Source

abstract Scope scope()

Returns the active scope.

set

Source

@Operator
abstract Constraints set(Obj key, Obj? value)

Sets a key / value pair to the service configuration with optional ordering constraints.

If the end service configuration is a List, then the keys are discarded and only the values passed use. In this case, typically Str keys are used for ease of use when overriding / adding constraints.

Configuration contributions are ordered across modules.

key and value are coerced to the service's contribution type.

config.set("key", value)

config["key"] = value