DependencyResolver class

Helper class for resolving component dependencies.

The resolver is configured to resolve named dependencies by specific locator. During deployment the dependency locator can be changed.

This mechanism can be used to clarify specific dependency among several alternatives. Typically components are configured to retrieve the first dependency that matches logical group, type and version. But if container contains more than one instance and resolution has to be specific about those instances, they can be given a unique name and dependency resolvers can be reconfigured to retrieve dependencies by their name.

Configuration parameters

dependencies:

  • dependency name 1: Dependency 1 locator (descriptor)
  • ...
  • dependency name N: Dependency N locator (descriptor)

References

References must match configured dependencies.

Example

class MyComponent implements IConfigurable, IReferenceable {
    DependencyResolver _dependencyResolver  = new DependencyResolver();
    IMyPersistence _persistence ;
    ...

     MyComponent() {
        _dependencyResolver.put('persistence', new Descriptor('mygroup', 'persistence', '*', '*', '1.0'));
    }

    configure(ConfigParams config ) {
        _dependencyResolver.configure(config);
    }

    setReferences(IReferences references) {
        _dependencyResolver.setReferences(references);
        _persistence = _dependencyResolver.getOneRequired<IMyPersistence>('persistence');
    }
}

// Create mycomponent and set specific dependency out of many
var component =  MyComponent();
component.configure(ConfigParams.fromTuples([
    'dependencies.persistence', 'mygroup:persistence:*:persistence2:1.0'
// Override default persistence dependency
]));
component.setReferences(References.fromTuples([
     Descriptor('mygroup','persistence','*','persistence1','1.0'),  MyPersistence(),
     Descriptor('mygroup','persistence','*','persistence2','1.0'),  MyPersistence()
// This dependency shall be set
]));

See IReferences

Implemented types

Constructors

DependencyResolver([ConfigParams? config, IReferences? references])
Creates a new instance of the dependency resolver.

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

configure(ConfigParams config) → void
Configures the component with specified parameters.
override
find<T>(String name, bool required) List<T>
Finds all matching dependencies by their name.
getOneOptional<T>(String name) → T?
Gets one optional dependency by its name.
getOneRequired<T>(String name) → T
Gets one required dependency by its name. At least one dependency must present. If the dependency was found it throws a ReferenceException
getOptional<T>(String name) List<T>
Gets all optional dependencies by their name.
getRequired<T>(String name) List<T>
Gets all required dependencies by their name. At least one dependency must be present. If no dependencies was found it throws a ReferenceException
locate(String? name) → dynamic
Gets a dependency locator by its name.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
put(String name, dynamic locator) → void
Adds a new dependency into this resolver.
setReferences(IReferences references) → void
Sets the component references. References must match configured dependencies.
override
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

fromTuples(List tuples) DependencyResolver
Creates a new DependencyResolver from a list of key-value pairs called tuples where key is dependency name and value the depedency locator (descriptor).