Pip-Services-Commons-Dotnet
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
PipServices3.Commons.Refer.DependencyResolver Class Reference

Helper class for resolving component dependencies. More...

Inheritance diagram for PipServices3.Commons.Refer.DependencyResolver:
PipServices3.Commons.Refer.IReferenceable PipServices3.Commons.Config.IReconfigurable PipServices3.Commons.Config.IConfigurable

Public Member Functions

 DependencyResolver ()
 Creates a new instance of the dependency resolver. More...
 
 DependencyResolver (ConfigParams config)
 Creates a new instance of the dependency resolver. More...
 
void Configure (ConfigParams config)
 Configures the component with specified parameters. More...
 
void SetReferences (IReferences references)
 Sets the component references More...
 
void Put (string name, object locator)
 Adds a new dependency into this resolver. More...
 
List< object > GetOptional (string name)
 Gets all optional dependencies by their name. More...
 
List< T > GetOptional< T > (string name)
 Gets all optional dependencies by their name. More...
 
List< object > GetRequired (string name)
 Gets all required dependencies by their name. At least one dependency must present.If no dependencies was found it throws a ReferenceException More...
 
List< T > GetRequired< T > (string name)
 Gets all required dependencies by their name. At least one dependency must present.If no dependencies was found it throws a ReferenceException More...
 
object GetOneOptional (string name)
 Gets one optional dependency by its name. More...
 
GetOneOptional< T > (string name)
 Gets one optional dependency by its name and matching to the specified type. More...
 
object GetOneRequired (string name)
 Gets one required dependency by its name. At least one dependency must present.If the dependency was found it throws a ReferenceException More...
 
GetOneRequired< T > (string name)
 Gets one required dependency by its name and matching to the specified type. At least one dependency must present.If the dependency was found it throws a ReferenceException More...
 
List< object > Find (string name, bool required)
 Finds all matching dependencies by their name. More...
 
List< T > Find< T > (string name, bool required)
 Finds all matching dependencies by their name and matching to the specified type. More...
 

Static Public Member Functions

static DependencyResolver FromTuples (params object[] tuples)
 Creates a new DependencyResolver from a list of key-value pairs called tuples where key is dependency name and value the depedency locator(descriptor). More...
 

Private Member Functions

object Locate (string name)
 Gets a dependency locator by its name. More...
 

Private Attributes

Dictionary< string, object > _dependencies = new Dictionary<string, object>()
 
IReferences _references
 

Detailed Description

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:

References:

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 IReferences

Constructor & Destructor Documentation

◆ DependencyResolver() [1/2]

PipServices3.Commons.Refer.DependencyResolver.DependencyResolver ( )

Creates a new instance of the dependency resolver.

◆ DependencyResolver() [2/2]

PipServices3.Commons.Refer.DependencyResolver.DependencyResolver ( ConfigParams  config)

Creates a new instance of the dependency resolver.

Parameters
config(optional) default configuration where key is dependency name and value is locator(descriptor)

See ConfigParams, Configure(ConfigParams), IReferences, SetReferences(IReferences)

Member Function Documentation

◆ Configure()

void PipServices3.Commons.Refer.DependencyResolver.Configure ( ConfigParams  config)

Configures the component with specified parameters.

Parameters
configconfiguration parameters to set.

See ConfigParams

Implements PipServices3.Commons.Config.IConfigurable.

◆ Find()

List<object> PipServices3.Commons.Refer.DependencyResolver.Find ( string  name,
bool  required 
)

Finds all matching dependencies by their name.

Parameters
namethe dependency name to locate.
requiredtrue to raise an exception when no dependencies are found.
Returns
a list of found dependencies

◆ Find< T >()

List<T> PipServices3.Commons.Refer.DependencyResolver.Find< T > ( string  name,
bool  required 
)

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

Template Parameters
Tthe class type
Parameters
namethe dependency name to locate.
requiredtrue to raise an exception when no dependencies are found.
Returns
a list of found dependencies

◆ FromTuples()

static DependencyResolver PipServices3.Commons.Refer.DependencyResolver.FromTuples ( params object []  tuples)
static

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

Parameters
tuplesa list of values where odd elements are dependency name and the following even elements are dependency locator(descriptor)
Returns
a newly created DependencyResolver.

◆ GetOneOptional()

object PipServices3.Commons.Refer.DependencyResolver.GetOneOptional ( string  name)

Gets one optional dependency by its name.

Parameters
namethe dependency name to locate.
Returns
a dependency reference or null of the dependency was not found

◆ GetOneOptional< T >()

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

Template Parameters
Tthe class type
Parameters
namethe dependency name to locate.
Returns
a dependency reference or null of the dependency was not found

◆ GetOneRequired()

object PipServices3.Commons.Refer.DependencyResolver.GetOneRequired ( string  name)

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

Parameters
namethe dependency name to locate.
Returns
a dependency reference

◆ GetOneRequired< T >()

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

Template Parameters
Tthe class type
Parameters
namethe dependency name to locate.
Returns
a dependency reference

◆ GetOptional()

List<object> PipServices3.Commons.Refer.DependencyResolver.GetOptional ( string  name)

Gets all optional dependencies by their name.

Parameters
namethe dependency name to locate.
Returns
a list with found dependencies or empty list of no dependencies was found.

◆ GetOptional< T >()

Gets all optional dependencies by their name.

Template Parameters
Tthe class type
Parameters
namethe dependency name to locate.
Returns
a list with found dependencies or empty list of no dependencies was found.

◆ GetRequired()

List<object> PipServices3.Commons.Refer.DependencyResolver.GetRequired ( string  name)

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

Parameters
namethe dependency name to locate.
Returns
a list with found dependencies.

◆ GetRequired< T >()

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

Template Parameters
Tthe class type
Parameters
namethe dependency name to locate.
Returns
a list with found dependencies.

◆ Locate()

object PipServices3.Commons.Refer.DependencyResolver.Locate ( string  name)
private

Gets a dependency locator by its name.

Parameters
namethe name of the dependency to locate.
Returns
the dependency locator or null if locator was not configured.

◆ Put()

void PipServices3.Commons.Refer.DependencyResolver.Put ( string  name,
object  locator 
)

Adds a new dependency into this resolver.

Parameters
namethe dependency's name.
locatorthe locator to find the dependency by.

◆ SetReferences()

void PipServices3.Commons.Refer.DependencyResolver.SetReferences ( IReferences  references)

Sets the component references

Parameters
referencesreferences to set.

Implements PipServices3.Commons.Refer.IReferenceable.


The documentation for this class was generated from the following file: