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() {
        this._dependencyResolver.put("persistence", new Descriptor("mygroup", "persistence", "*", "*", "1.0"));
    }
    
    configure(ConfigParams config ) {
        this._dependencyResolver.configure(config);
    }  
    
    setReferences(IReferences references) {
        this._dependencyResolver.setReferences(references);
        this._persistence = this._dependencyResolver.getOneRequired<IMyPersistence>("persistence");
    }
}

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

See IReferences

Implemented types

Constructors

DependencyResolver([ConfigParams config = null, IReferences references = null ])
  • Creates a new instance of the dependency resolver.
  • Properties

    hashCode → int
    The hash code for this object. [...]
    read-only, inherited
    runtimeType → Type
    A representation of the runtime type of the object.
    read-only, inherited

    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.
      • name the dependency name to locate.
      • required true to raise an exception when no dependencies are found.
    • Returns a list of found dependencies
    • Throws a ReferenceException of required is true and no dependencies found.
  • getOneOptional<T>(String name) → T
  • Gets one optional dependency by its name.
      • name the dependency name to locate.
    • Returns a dependency reference or null of the dependency was not found
  • 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
      • name the dependency name to locate.
    • Returns a dependency reference
    • Throws a ReferenceException if dependency was not found.
  • getOptional<T>(String name) → List<T>
  • Gets all optional dependencies by their name.
      • name the dependency name to locate.
    • Returns a list with found dependencies or empty list of no dependencies was found.
  • 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
      • name the dependency name to locate.
    • Returns a list with found dependencies.
    • Throws a ReferenceException if no dependencies were found.
  • locate(String name) → dynamic
  • Gets a dependency locator by its name.
      • name the name of the dependency to locate.
    • Returns the dependency locator or null if locator was not configured.
  • put(String name, dynamic locator) → void
  • Adds a new dependency into this resolver.
      • name the dependency's name.
      • locator the locator to find the dependency by.
  • setReferences(IReferences references) → void
  • Sets the component references. References must match configured dependencies.
      • references references to set.
  • override
    noSuchMethod(Invocation invocation) → dynamic
    Invoked when a non-existent method or property is accessed. [...]
    inherited
    toString() → String
    Returns a string representation of this object.
    inherited

    Operators

    operator ==(dynamic 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).
      • tuples a list of values where odd elements are dependency name and the following even elements are dependency locator (descriptor)
    • Returns a newly created DependencyResolver.
    • See fromTuplesArray