quicksuite-gb-microservice
v1.0.0
Published
https://github.com/microsoft/tsyringe
Downloads
1
Readme
Dependency injection
https://github.com/microsoft/tsyringe
register.service.ts
@singleton()
export class RegistryService {
private registry: IApiBase[];
constructor() {
this.registry = [];
}
public init() {
// resolve all instances of controllers here
this.registry = [
container.resolve(Controller1Api),
container.resolve(Controller2Api),
];
}
public registerModels() {
// register mongoose models
container.register<Model<Territory>>('Territory', { useValue: TerritoryModel })
container.register<Model<Industry>>('Industry', { useValue: IndustryModel })
}
public connect(app: Express) {
for (const controller of this.registry) {
app.use(controller.baseRoute, controller.router);
logger.info('[RegistryService]', `Registered router ${controller.baseRoute}`);
}
}
}
server.ts
...
import { container, injectable } from "tsyringe";
...
@injectable()
class Server {
...
this.registry.registerModels()
this.registry.init();
//this.app = express();
...
// this.bindPOSIXSignals();
this.registry.connect(this.app)
index.ts
import "reflect-metadata";
import { container } from 'tsyringe';
import { Server } from './server';
//
// Run service with default configuration
const app = container.resolve(Server);
exports.default = app.up();