https://github.com/inversify/inversify-basic-example
A basic example that showcases how to setup InversifyJS
https://github.com/inversify/inversify-basic-example
Last synced: 11 months ago
JSON representation
A basic example that showcases how to setup InversifyJS
- Host: GitHub
- URL: https://github.com/inversify/inversify-basic-example
- Owner: inversify
- License: mit
- Created: 2016-09-01T22:38:47.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2020-05-25T16:18:53.000Z (about 6 years ago)
- Last Synced: 2025-06-06T21:14:28.962Z (about 1 year ago)
- Language: TypeScript
- Size: 254 KB
- Stars: 110
- Watchers: 13
- Forks: 207
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# inversify-basic-example
[](https://gitter.im/inversify/InversifyJS)
[](https://travis-ci.org/inversify/inversify-basic-example)
[](https://david-dm.org/inversify/inversify-basic-example#info=dependencies)
[](https://david-dm.org/inversify/inversify-basic-example/#info=devDependencies)
[](https://david-dm.org/inversify/inversify-basic-example/#info=peerDependenciess)
A basic example that showcases how to setup InversifyJS
This is a very basic InversifyJS example.
This program declares:
- Three interfaces `Warrior`, `Weapon` and `Battle`.
- Two implementations of `Weapon`: `Katana` and `Shuriken`
- Two implementations of `Warrior`: `Ninja` and `Samurai`
- One implementation of `Battle`: `EpicBattle`.
The warriors own a weapon and are tagged with some metadata.

We use some constraints `whenTargetNamed` and `whenParentNamed` to indicate which
`Weapon` should be injected into and implementation of `Warrior` and which implementation
of `Warrior` should injected into `EpicBattle`:
```ts
container.bind(SERVICE_IDENTIFIER.WARRIOR).to(Ninja).whenTargetNamed(TAG.CHINESE);
container.bind(SERVICE_IDENTIFIER.WARRIOR).to(Samurai).whenTargetNamed(TAG.JAPANESE);
container.bind(SERVICE_IDENTIFIER.WEAPON).to(Shuriken).whenParentNamed(TAG.CHINESE);
container.bind(SERVICE_IDENTIFIER.WEAPON).to(Katana).whenParentNamed(TAG.JAPANESE);
container.bind(SERVICE_IDENTIFIER.BATTLE).to(EpicBattle);
```
# How can I run it?
You can clone it using:
```
$ git clone https://github.com/inversify/inversify-basic-example.git
```
To run this example you need to install some dependencies:
```
$ cd inversify-basic-example
$ npm install
```
And compile the TypeScript code into JavaScript code:
```
$ gulp
```
The generated code is available at the `dist` directory.
At this point you are ready to run the example:
```
$ node dist/main.js
```
You should see the following in console:
```
FIGHT!
Ninja (Shuriken)
vs
Samurai (Katana)
```