Registry
About
A well known object (known by all), useful for locating other objects/services.
Registry Pattern is very important for Dependency Injection.
Serves basically as a Dependency pool.
Implementing using Singleton
export default class Registry {
private dependencies: {[name: string]: any} = {};
private static instance: Registry;
private constructor() {}
provide(name: string, dependency: any) {
this.dependencies[name] = dependency;
}
inject(name: string) {
return this.dependencies[name];
}
static getInstance() {
if (!Registry.instance) Registry.instance = new Registry();
return Registry.instance;
}
}Use it anywhere on the code to, provide (create) and inject (get) other class instances (dependencies).
Example (without decorators)
decorators)This
Will be substituted by
In the main.ts we instanciate and provide to the Registry all the classes we want.
At the Signup.ts for example we will grab the UserDAO instance created in the main.ts. And as you can see, no classes had to be passed to the constructor as parameter.
Example (with decorators) (in typescript)
decorators) (in typescript)The same example above, will be substituted by:
Last updated