Add Dependency Injection example and core interfaces. Implement Inject class for managing scopes and instances, along with InstanceProvider, IntegerConstantProvider, and StringConstantProvider for dependency resolution.
This commit is contained in:
33
src/di/IntegerConstantProvider.ts
Normal file
33
src/di/IntegerConstantProvider.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* Copyright 2025 Phenix Real Time Solutions, Inc. Confidential and Proprietary. All Rights Reserved.
|
||||
*/
|
||||
|
||||
import type {IDependencyProvider} from './IDependencyProvider';
|
||||
import {Type} from './Type';
|
||||
import {NamedType} from './NamedType';
|
||||
|
||||
/**
|
||||
* A dependency provider for integer constants.
|
||||
* Uses NamedType where the name is parsed as an integer.
|
||||
*/
|
||||
export class IntegerConstantProvider implements IDependencyProvider {
|
||||
public canProvide(type: Type): boolean {
|
||||
if (!(type instanceof Type)) {
|
||||
throw new Error('Must provide a valid type');
|
||||
}
|
||||
|
||||
return type instanceof NamedType && type.getType() === 'di/IntegerConstantProvider';
|
||||
}
|
||||
|
||||
public async provide(type: Type): Promise<number> {
|
||||
if (!(type instanceof NamedType)) {
|
||||
throw new Error('Must provide a NamedType');
|
||||
}
|
||||
|
||||
return parseInt(type.getName(), 10);
|
||||
}
|
||||
|
||||
public toString(): string {
|
||||
return 'IntegerConstantProvider';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user