UNPKG

6.22 kBTypeScriptView Raw
1import { ConfigParams } from '../config/ConfigParams';
2import { IReconfigurable } from '../config/IReconfigurable';
3import { IReferenceable } from './IReferenceable';
4import { IReferences } from './IReferences';
5/**
6 * Helper class for resolving component dependencies.
7 *
8 * The resolver is configured to resolve named dependencies by specific locator.
9 * During deployment the dependency locator can be changed.
10 *
11 * This mechanism can be used to clarify specific dependency among several alternatives.
12 * Typically components are configured to retrieve the first dependency that matches
13 * logical group, type and version. But if container contains more than one instance
14 * and resolution has to be specific about those instances, they can be given a unique
15 * name and dependency resolvers can be reconfigured to retrieve dependencies by their name.
16 *
17 * ### Configuration parameters ###
18 *
19 * dependencies:
20 * - [dependency name 1]: Dependency 1 locator (descriptor)
21 * - ...
22 * - [dependency name N]: Dependency N locator (descriptor)
23 *
24 * ### References ###
25 *
26 * References must match configured dependencies.
27 *
28 * ### Example ###
29 *
30 * class MyComponent: IConfigurable, IReferenceable {
31 * private _dependencyResolver: DependencyResolver = new DependencyResolver();
32 * private _persistence: IMyPersistence;
33 * ...
34 *
35 * public constructor() {
36 * this._dependencyResolver.put("persistence", new Descriptor("mygroup", "persistence", "*", "*", "1.0"));
37 * }
38 *
39 * public configure(config: ConfigParams): void {
40 * this._dependencyResolver.configure(config);
41 * }
42 *
43 * public setReferences(references: IReferences): void {
44 * this._dependencyResolver.setReferences(references);
45 * this._persistence = this._dependencyResolver.getOneRequired<IMyPersistence>("persistence");
46 * }
47 * }
48 *
49 * // Create mycomponent and set specific dependency out of many
50 * let component = new MyComponent();
51 * component.configure(ConfigParams.fromTuples(
52 * "dependencies.persistence", "mygroup:persistence:*:persistence2:1.0"
53 * // Override default persistence dependency
54 * ));
55 * component.setReferences(References.fromTuples(
56 * new Descriptor("mygroup","persistence","*","persistence1","1.0"), new MyPersistence(),
57 * new Descriptor("mygroup","persistence","*","persistence2","1.0"), new MyPersistence()
58 * // This dependency shall be set
59 * ));
60 *
61 * @see [[IReferences]]
62 */
63export declare class DependencyResolver implements IReferenceable, IReconfigurable {
64 private _dependencies;
65 private _references;
66 /**
67 * Creates a new instance of the dependency resolver.
68 *
69 * @param config (optional) default configuration where key is dependency name and value is locator (descriptor)
70 * @param references (optional) default component references
71 *
72 * @see [[ConfigParams]]
73 * @see [[configure]]
74 * @see [[IReferences]]
75 * @see [[setReferences]]
76 */
77 constructor(config?: ConfigParams, references?: IReferences);
78 /**
79 * Configures the component with specified parameters.
80 *
81 * @param config configuration parameters to set.
82 *
83 * @see [[ConfigParams]]
84 */
85 configure(config: ConfigParams): void;
86 /**
87 * Sets the component references. References must match configured dependencies.
88 *
89 * @param references references to set.
90 */
91 setReferences(references: IReferences): void;
92 /**
93 * Adds a new dependency into this resolver.
94 *
95 * @param name the dependency's name.
96 * @param locator the locator to find the dependency by.
97 */
98 put(name: string, locator: any): void;
99 /**
100 * Gets a dependency locator by its name.
101 *
102 * @param name the name of the dependency to locate.
103 * @returns the dependency locator or null if locator was not configured.
104 */
105 private locate;
106 /**
107 * Gets all optional dependencies by their name.
108 *
109 * @param name the dependency name to locate.
110 * @returns a list with found dependencies or empty list of no dependencies was found.
111 */
112 getOptional<T>(name: string): T[];
113 /**
114 * Gets all required dependencies by their name.
115 * At least one dependency must be present.
116 * If no dependencies was found it throws a [[ReferenceException]]
117 *
118 * @param name the dependency name to locate.
119 * @returns a list with found dependencies.
120 *
121 * @throws a [[ReferenceException]] if no dependencies were found.
122 */
123 getRequired<T>(name: string): T[];
124 /**
125 * Gets one optional dependency by its name.
126 *
127 * @param name the dependency name to locate.
128 * @returns a dependency reference or null of the dependency was not found
129 */
130 getOneOptional<T>(name: string): T;
131 /**
132 * Gets one required dependency by its name.
133 * At least one dependency must present.
134 * If the dependency was found it throws a [[ReferenceException]]
135 *
136 * @param name the dependency name to locate.
137 * @returns a dependency reference
138 *
139 * @throws a [[ReferenceException]] if dependency was not found.
140 */
141 getOneRequired<T>(name: string): T;
142 /**
143 * Finds all matching dependencies by their name.
144 *
145 * @param name the dependency name to locate.
146 * @param required true to raise an exception when no dependencies are found.
147 * @returns a list of found dependencies
148 *
149 * @throws a [[ReferenceException]] of required is true and no dependencies found.
150 */
151 find<T>(name: string, required: boolean): T[];
152 /**
153 * Creates a new DependencyResolver from a list of key-value pairs called tuples
154 * where key is dependency name and value the depedency locator (descriptor).
155 *
156 * @param tuples a list of values where odd elements are dependency name and the following even elements are dependency locator (descriptor)
157 * @returns a newly created DependencyResolver.
158 *
159 * @see [[fromTuplesArray]]
160 */
161 static fromTuples(...tuples: any[]): DependencyResolver;
162}