InversifyJS has been developed with 4 main goals:
Facilitate and encourage the adherence to the best OOP and IoC practices.
Add as little runtime overhead as possible.
Provide a state of the art development experience.
Dependency injection like InversifyJS works nicely
You can get the latest release and the type definitions using npm:
$ npm install inversify reflect-metadata --save
The InversifyJS type definitions are included in the inversify npm package.
⚠️ Important! InversifyJS requires TypeScript >= 2.0 and the
libcompilation options in your
"compilerOptions":"target": "es5""lib": "es6""types": "reflect-metadata""module": "commonjs""moduleResolution": "node""experimentalDecorators": true"emitDecoratorMetadata": true
If your environment doesn't support one of these you will need to import a shim or polyfill.
reflect-metadatapolyfill should be imported only once in your entire application because the Reflect object is mean to be a global singleton. More details about this can be found here.
Let’s take a look at the basic usage and APIs of InversifyJS with TypeScript:
Our goal is to write code that adheres to the dependency inversion principle. This means that we should "depend upon Abstractions and do not depend upon concretions". Let's start by declaring some interfaces (abstractions).
// file interfaces.ts
InversifyJS need to use the type as identifiers at runtime. We use symbols as identifiers but you can also use classes and or string literals.
// file types.ts;;
Note: It is recommended to use Symbols but InversifyJS also support the usage of Classes and string literals (please refer to the features section to learn more).
Let's continue by declaring some classes (concretions). The classes are implementations of the interfaces that we just declared. All the classes must be annotated with the
When a class has a dependency on an interface we also need to use the
@inject decorator to define an identifier for the interface that will be available at runtime. In this case we will use the Symbols
Symbol.for("ThrowableWeapon") as runtime identifiers.
// file entities.ts;;;;
If you prefer it you can use property injection instead of constructor injection so you don't have to declare the class constructor:
We recommend to do this in a file named
inversify.config.ts. This is the only place in which there is some coupling.
In the rest of your application your classes should be free of references to other classes.
// file inversify.config.ts;;;;;myContainer.bindTYPES.Warrior.toNinja;myContainer.bindTYPES.Weapon.toKatana;myContainer.bindTYPES.ThrowableWeapon.toShuriken;;
;;;;expectninja.fight.eql"cut!"; // trueexpectninja.sneak.eql"hit!"; // true
As we can see the
Shuriken were successfully resolved and injected into
Let's take a look to the InversifyJS features!
Please refer to the wiki for additional details.
In order to provide a state of the art development experience we are also working on:
Please refer to the ecosystem wiki page to learn more.
If you want to share your thoughts with the development team or join us you will be able to do so using the official the mailing list. You can check out the wiki to learn more about InversifyJS internals.
Thanks a lot to all the contributors, all the developers out there using InversifyJS and all those that help us to spread the word by sharing content about InversifyJS online. Without your feedback and support this project would not be possible.
License under the MIT License (MIT)
Copyright © 2015-2017 Remo H. Jansen
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.