Add dependency management classes and interfaces
- Introduced Type and NamedType classes for type management - Implemented IDependencyProvider interface for dependency provision - Updated DependencyManager with a new lifecycle type 'service' - Added methods for type comparison and URN generation in Type and NamedType
This commit is contained in:
@@ -7,6 +7,7 @@ type Registration = {
|
|||||||
/**
|
/**
|
||||||
* Lifecycle types:
|
* Lifecycle types:
|
||||||
* - 'singleton': A single shared instance cached for the lifetime of the manager
|
* - 'singleton': A single shared instance cached for the lifetime of the manager
|
||||||
|
* - 'sergice': A single shared instance cached for the lifetime of the manager
|
||||||
* - 'transient': A new instance created on each resolve (transient)
|
* - 'transient': A new instance created on each resolve (transient)
|
||||||
*/
|
*/
|
||||||
type Lifecycle = 'transient' | 'singleton';
|
type Lifecycle = 'transient' | 'singleton';
|
||||||
|
|||||||
22
src/di/NamedType.ts
Normal file
22
src/di/NamedType.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import { Type } from "./Type";
|
||||||
|
|
||||||
|
export class NamedType extends Type {
|
||||||
|
private readonly _name: string;
|
||||||
|
|
||||||
|
constructor(name: string, type: string) {
|
||||||
|
super(type);
|
||||||
|
this._name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getName(): string {
|
||||||
|
return this._name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public equals(other: NamedType): boolean {
|
||||||
|
return super.equal(other) && other instanceof NamedType && this.getName() === other.getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override toURN(): string {
|
||||||
|
return 'urn:namedtype:' + super.getType() + '#' + this._name;
|
||||||
|
}
|
||||||
|
}
|
||||||
19
src/di/Type.ts
Normal file
19
src/di/Type.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
export class Type {
|
||||||
|
private readonly _type: string;
|
||||||
|
|
||||||
|
constructor(type: string) {
|
||||||
|
this._type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getType(): string {
|
||||||
|
return this._type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public equal(other: Type): boolean {
|
||||||
|
return other instanceof Type && this.getType() === other.getType();
|
||||||
|
}
|
||||||
|
|
||||||
|
public toURN() {
|
||||||
|
return 'urn:type:' + this._type;
|
||||||
|
}
|
||||||
|
}
|
||||||
7
src/di/providers/IDependencyProvider.ts
Normal file
7
src/di/providers/IDependencyProvider.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import type { Type } from "../Type";
|
||||||
|
import type { NamedType } from "../NamedType";
|
||||||
|
|
||||||
|
export interface IDependencyProvider {
|
||||||
|
canProvide(type: Type | NamedType): boolean;
|
||||||
|
provide(type: Type | NamedType): unknown;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user