DependencyResolver

Helper class for resolving component dependencies.

Inherits: IReferenceable, IReconfigurable

Description

The DependencyResolver is a helper class that allows you to resolve component dependencies. It is configured to resolve named dependencies by a specific locator.

Important points

  • During deployment the dependency locator can be changed. This mechanism can be used to clarify a specific dependency among several alternatives. Typically components are configured to retrieve the first dependency that matches a logical group, type and version. However, if the container contains more than one instance and the resolution has to be specific about those instances, they can be given a unique name and the dependency resolvers can be reconfigured to retrieve dependencies according to 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.

Constructors

Creates a new instance of the dependency resolver.

public DependencyResolver(ConfigParams config)

  • config: ConfigParams - (optional) default configuration where key is a dependency name and value is its locator (descriptor)

Creates a new instance of the dependency resolver.

public DependencyResolver()

Instance methods

configure

Configures the component with specified parameters.

public void Configure(config: ConfigParams)

Find

Finds all matching dependencies by their name.

public List<object> Find(string name, bool required)

  • name: string - dependency name to locate.
  • required: bool - true to raise an exception when no dependencies are found.
  • returns: List<object> - list of found dependencies

Find

Finds all matching dependencies by their name and matching to the specified type. T - class type

public List<T> find<T>(string name, bool required)

  • name: string - dependency name to locate.
  • required: bool - true to raise an exception when no dependencies are found.
  • returns: List<T> - list of found dependencies

GetOneOptional

Gets one optional dependency by its name.

public object GetOneOptional(name: string)

  • name: string - dependency name to locate.
  • returns: object - dependency reference or null if the dependency was not found

GetOneOptional

Gets one optional dependency by its name and matching to the specified type. T - class type

public T GetOneOptional<T>(string name)

  • name: string - dependency name to locate.
  • returns: T - dependency reference or null if the dependency was not found

GetOneRequired

Gets one required dependency by its name. At least one dependency must be present. If the dependency was found, it throws a ReferenceException

public object GetOneRequired<T>(string name)

  • name: string - dependency name to locate.
  • returns: object - dependency reference

GetOneRequired

Gets one required dependency by its name and matching to the specified type. At least one dependency must be present. If the dependency was found, it throws a ReferenceException T - the class type.

public T GetOneRequired<T>(string name)

  • name: string - dependency name to locate.
  • returns: T - dependency reference

GetOptional

Gets all optional dependencies by their name.

public List<T> GetOptional(string name)

  • name: string - dependency name to locate.
  • returns: List<T> - list with found dependencies or empty list if no dependency was found.

GetOptional

Gets all optional dependencies by their name. T - the class type.

public List<T> GetOptional<T>(string name)

  • name: string - the dependency name to locate.
  • returns: List<T> - list with found dependencies or empty list of no dependency was found.

GetRequired

Gets all required dependencies by their name. At least one dependency must present. If no dependency was found, it throws a ReferenceException.

public List<object> GetRequired(string name)

  • name: string - dependency name to locate.
  • returns: List<T> - list with found dependencies.

GetRequired

Gets all required dependencies by their name. At least one dependency must be present. If no dependency was found it throws a ReferenceException. T - class type

public List<T> GetRequired<T>(string name)

  • name: string - dependency name to locate.
  • returns: List<T> - list with found dependencies.

Put

Adds a new dependency into this resolver.

public void Put(string name, object locator)

  • name: string - dependency’s name.
  • locator: object - locator used to find the dependency.

SetReferences

Sets the component references. References must match configured dependencies.

public void SetReferences(IReferences references)

Static methods

FromTuples

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

public static DependencyResolver FromTuples(params object[] tuples)

  • tuples: object[] - list of values where odd elements are dependency names and the following even elements are dependency locators (descriptor)
  • returns: DependencyResolver - newly created DependencyResolver.

Examples

class MyComponent: IConfigurable, IReferenceable 
{
    private DependencyResolver _dependencyResolver = new DependencyResolver();
    private IMyPersistence _persistence;
    ...
    
    public MyComponent()
    {
        this._dependencyResolver.Put("persistence", new Descriptor("mygroup", "persistence", "*", "*", "1.0"));
    }
    
    public void Configure(ConfigParams config)
    {
        this._dependencyResolver.Configure(config);
    }
    
    public void 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 also