Skip to content

Instantly share code, notes, and snippets.

@humodz
Created April 17, 2024 16:30
Show Gist options
  • Save humodz/364f0046eaae1aa724767da934719f64 to your computer and use it in GitHub Desktop.
Save humodz/364f0046eaae1aa724767da934719f64 to your computer and use it in GitHub Desktop.
import 'reflect-metadata';
import { Container, inject, injectable } from "inversify";
@injectable()
class Database {
constructor() {
console.log('new Database');
}
}
@injectable()
class Service1 {
constructor(
@inject(Database) public database: Database,
) {
console.log('new Service1');
}
}
@injectable()
class Service2 {
constructor(
@inject(Service1) public service1: Service1,
) {
console.log('new Service2');
}
}
async function main() {
const container = new Container({ defaultScope: 'Request' });
// container.bind(Database).toSelf();
container.bind(Database).toDynamicValue(async () => {
console.log('connecting to db...');
return new Database();
});
container.bind(Service1).toSelf();
container.bind(Service2).toSelf();
container.bind('services').toService(Service1);
container.bind('services').toService(Service2);
console.log(await container.getAllAsync('services'));
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment