Created
December 13, 2022 02:07
-
-
Save gleydson/a5df46708404605ed5a2fc5cb6131ac6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type Class<T = unknown> = new (...args: unknown[]) => T | |
class DependencyManager { | |
// eslint-disable-next-line no-use-before-define | |
private static instance: DependencyManager | |
public deps: Record<string, Class> = {} | |
private constructor() {} | |
public static getInstance(): DependencyManager { | |
if (!DependencyManager.instance) { | |
DependencyManager.instance = new DependencyManager() | |
} | |
return DependencyManager.instance | |
} | |
public get(key: string): Class { | |
const matches = this.deps[key] | |
if (!matches) { | |
throw new Error('No Matches Found') | |
} | |
return matches | |
} | |
public set(key: string, dep: Class): void { | |
if (this.deps[key]) { | |
return | |
} | |
this.deps[key] = dep | |
} | |
} | |
export function Param(key: string): ParameterDecorator { | |
return function (target, propertyKey) { | |
const dependencyManager = DependencyManager.getInstance() | |
Object.defineProperty(target, propertyKey, { | |
get: () => dependencyManager.get(key), | |
enumerable: true, | |
configurable: true | |
}) | |
} | |
} | |
function injectable(key: string): Function { | |
const dependencyManager = DependencyManager.getInstance() | |
console.log('dependencyManager', dependencyManager.deps) | |
return function (InjectableClass: new () => any): void { | |
dependencyManager.set(key, new InjectableClass()) | |
} | |
} | |
export const RepositoryInjectable = injectable | |
export const UseCaseInjectable = injectable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment