Created
April 17, 2024 16:30
-
-
Save humodz/364f0046eaae1aa724767da934719f64 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
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