inversify-binding-decorators
An utility that allows developers to declare InversifyJS bindings using ES2016 decorators:
Installation
You can install inversify-binding-decorators
using npm:
$ npm install inversify inversify-binding-decorators reflect-metadata --save
The inversify-binding-decorators
type definitions are included in the npm module and require TypeScript 2.0.
Please refer to the InversifyJS documentation to learn more about the installation process.
The basics
The InversifyJS API allows us to delcare bindings using a fluent API:
;; ;container.bind"Katana".toKatana;container.bind"Shuriken".toShuriken;
This small utility allows you to declare bindings using decorators:
;;; ;// Reflects all decorators provided by this package and packages them into // a module to be loaded by the containercontainer.loadbuildProviderModule;
Using @provide multiple times
If you try to apply @provide
multiple times:
The library will throw an exception:
Cannot apply @injectable decorator multiple times. Please use @provide(ID, true) if you are trying to declare multiple bindings!
We throw an exception to ensure that you are are not trying to apply @provide
multiple times by mistake.
You can overcome this by passing the force
argument to @provide
:
Using classes, string literals & symbols as identifiers
When you invoke @provide
using classes:
A new binding is created under the hood:
container.bindKatana.toKatana;container.bindNinja.toNinja;
These bindings use classes as identidiers but you can also use string literals as identifiers:
;
You can also use symbols as identifiers:
;
Fluent binding decorator
The basic @provide
decorator doesn't allow you to declare contextual constraints,
scope and other advanced binding features. However, inversify-binding-decorators
includes a second decorator that allows you to achieve access the full potential
of the fluent binding syntax:
;; ; ;container.loadbuildProviderModule;
One of the best things about the fluent decorator is that you can create aliases to fit your needs:
;
Another example:
;
Using @provideFluent multiple times
If you try to apply @provideFluent
multiple times:
;; ;
The library will throw an exception:
Cannot apply @provideFluent decorator multiple times but is has been used multiple times in Ninja Please use done(true) if you are trying to declare multiple bindings!
We throw an exception to ensure that you are are not trying to apply @fluentProvide
multiple times by mistake.
You can overcome this by passing the force
argument to done()
:
; ;container.loadbuildProviderModule;
The auto provide utility
This library includes a small utility apply to add the default @provide
decorator to all
the public properties of a module:
Consider the following example:
; ;autoProvidecontainer, entites;;expectwarrior.fight.eql"Using Katana...";
The contents of the entities.ts file are the following:
;;
The contents of the katana.ts file are the following:
;
The contents of the warrior.ts file are the following:
;; ;
Support
If you are experience any kind of issues we will be happy to help. You can report an issue using the
issues page or the
chat. You can also ask questions at
Stack overflow using the inversifyjs
tag.
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 and browse the documented source code to learn more about InversifyJS internals.
Acknowledgements
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
License under the MIT License (MIT)
Copyright © 2016 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.