-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(inversify-code-examples): add container module example
- Loading branch information
1 parent
66d8736
commit 5c241d8
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
...ges/docs/tools/inversify-code-examples/src/examples/containerModuleApiExample.int.spec.ts
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,10 @@ | ||
import { describe, expect, it } from '@jest/globals'; | ||
|
||
import { Ninja, ninja, Shuriken } from './containerModuleApiExample'; | ||
|
||
describe('ContainerModule API', () => { | ||
it('should provide expected service', async () => { | ||
expect(ninja).toBeInstanceOf(Ninja); | ||
expect(ninja.weapon).toBeInstanceOf(Shuriken); | ||
}); | ||
}); |
59 changes: 59 additions & 0 deletions
59
packages/docs/tools/inversify-code-examples/src/examples/containerModuleApiExample.ts
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,59 @@ | ||
import { | ||
Container, | ||
ContainerModule, | ||
inject, | ||
injectable, | ||
interfaces, | ||
named, | ||
} from 'inversify'; | ||
|
||
interface Weapon { | ||
damage: number; | ||
} | ||
|
||
@injectable() | ||
export class Katana implements Weapon { | ||
public readonly damage: number = 10; | ||
} | ||
|
||
@injectable() | ||
export class Shuriken implements Weapon { | ||
public readonly damage: number = 5; | ||
} | ||
|
||
@injectable() | ||
export class Ninja { | ||
@inject('Weapon') | ||
@named('Ranged') | ||
public readonly weapon!: Weapon; | ||
} | ||
|
||
// Begin-example | ||
const warriorsModule: ContainerModule = new ContainerModule( | ||
(bind: interfaces.Bind) => { | ||
bind<Ninja>('Ninja').to(Ninja); | ||
}, | ||
); | ||
|
||
const weaponsModule: ContainerModule = new ContainerModule( | ||
( | ||
bind: interfaces.Bind, | ||
_unbind: interfaces.Unbind, | ||
_isBound: interfaces.IsBound, | ||
_rebind: interfaces.Rebind, | ||
_unbindAsync: interfaces.UnbindAsync, | ||
_onActivation: interfaces.Container['onActivation'], | ||
_onDeactivation: interfaces.Container['onDeactivation'], | ||
) => { | ||
bind<Katana>('Weapon').to(Katana).whenTargetNamed('Melee'); | ||
bind<Shuriken>('Weapon').to(Shuriken).whenTargetNamed('Ranged'); | ||
}, | ||
); | ||
|
||
const container: Container = new Container(); | ||
container.load(warriorsModule, weaponsModule); | ||
|
||
const ninja: Ninja = container.get('Ninja'); | ||
// End-example | ||
|
||
export { ninja }; |