-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
controller and handler registering by decorator
- Loading branch information
1 parent
30d820d
commit a031d60
Showing
15 changed files
with
104 additions
and
41 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,32 @@ | ||
import { AHException } from "../framework/features/anthill-exception"; | ||
import { AHDependencyContainerMap } from "./models/dependency-container-map"; | ||
|
||
|
||
class DependencyContainer { | ||
private instances: Map<string, any> = new Map(); | ||
export class AHDependencyContainer { | ||
private container: AHDependencyContainerMap = {}; | ||
|
||
// Register a class constructor with a unique identifier | ||
register<T>(identifier: string, constructor: new () => T) { | ||
if (!this.instances.has(identifier)) { | ||
this.instances.set(identifier, constructor); | ||
console.log("registering " + identifier); | ||
|
||
if (!Object.keys(this.container).includes(identifier)) { | ||
this.container[identifier] = { | ||
constructor: constructor, | ||
instance: null, | ||
}; | ||
} | ||
} | ||
|
||
// Resolve an instance by its identifier | ||
resolve<T>(identifier: string): T { | ||
const constructor = this.instances.get(identifier); | ||
console.log("resolving " + identifier); | ||
|
||
if (!Object.keys(this.container).includes(identifier)) { | ||
throw new AHException(`Dependency ${identifier} has never been registered`); | ||
} | ||
|
||
if (!constructor) { | ||
throw new AHException(`Dependency not registered: ${identifier}`); | ||
if (!this.container[identifier].instance) { | ||
this.container[identifier].instance = new this.container[identifier].constructor(); | ||
} | ||
|
||
return new constructor(); | ||
return this.container[identifier].instance; | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface AHDependencyContainerMap { | ||
[key: string]: { | ||
constructor: new () => any, | ||
instance: any | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,14 @@ | ||
/* import { AHRestHandlerOverridableConfig } from "../models/handler/rest-handler-overridable-config"; | ||
type Constructor<T = {}> = new (...args: any[]) => T; | ||
import { Anthill } from "../features/anthill"; | ||
import { AHRestHandlerOverridableConfig } from "../models/handler/rest-handler-overridable-config"; | ||
|
||
export function RestController(restControllerOptions: AHRestHandlerOverridableConfig): any { | ||
return <Class extends Constructor>( | ||
Value: Class, | ||
context: ClassDecoratorContext<Class> | ||
return <T extends { new (...args: any[]): T; _restHandlerConfig: AHRestHandlerOverridableConfig }>( | ||
target: T, | ||
context: ClassDecoratorContext<T>, | ||
) => { | ||
const _this = this; | ||
return class extends Value { | ||
constructor(...args: any[]) { | ||
super(...args); | ||
_this.instances.add(this); | ||
} | ||
}; | ||
Anthill.getInstance()._dependencyContainer.register<T>(context.name, target); | ||
Anthill.getInstance()._dependencyContainer.resolve<T>(context.name)._restHandlerConfig = restControllerOptions; | ||
|
||
return target; | ||
}; | ||
} */ | ||
} |
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
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
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
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
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
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
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
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
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
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