1.0.13 • Published 4 years ago
dagger-di v1.0.13
Dagger
Dagger is a dependency injection container using Decorators.
- Tested with React Class and Functional components using Props.
- Tested with Singletons.
- Tested with Generators.
- Lazy loads dependencies until construction.
Installation
Dagger relies on the experimental stage-0 decorators.
$ npm install dagger-di
$ npm install @babel/plugin-proposal-decorators @babel/plugin-proposal-class-properties --save-dev
$ yarn add dagger-di
$ yarn add -D @babel/plugin-proposal-decorators @babel/plugin-proposal-class-properties
Turn on Decorators in Babel.
{
// React.
"babel": {
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
[
"@babel/plugin-proposal-decorators",
{
"legacy": true
}
],
"@babel/plugin-proposal-class-properties"
]
}
// React-Native.
"babel": {
"presets": [
"module:metro-react-native-babel-preset"
],
"plugins": [
[
"@babel/plugin-proposal-decorators",
{
"legacy": true
}
],
"@babel/plugin-proposal-class-properties"
]
}
}
Turn on Decorators in ESLint.
package.json
{
...,
"eslintConfig": {
"ecmaFeatures": {
"experimentalDecorators": true,
"legacyDecorators": true
}
}
}
Usage
import { singleton } from "dagger-di";
// Registers "Foo" with a singleton Foo.
@singleton
class Foo {
}
import { inject, generator } from "dagger";
// Registers "Bar" with a Generator creating
// a new instance for every injection of "Bar".
@generator
@inject("Foo")
class Bar {
constructor(foo) {
this.foo = foo;
}
}
export const warrior = () => {};
export const ninja = () => {};
export const archer = () => {};
import { register, registerLazily, registerModule } from "dagger-di";
import { injectProps } from "dagger-di/src/react"
// Load the module to load the singleton decorator.
import from "./Foo";
// Or manually register without decorators.
register("BarTwo", require("./Bar"));
// Or register a lazy function to evaluate when it is used.
registerLazily("LazyBar", () => require("./Bar"));
// Or register all exports within a module.
registerModule(require("./module"));
// Throws an error if any dependencies haven't been registered.
// On construction, Dagger will inject the dependencies for you.
@injectProps("Foo", "Bar", "LazyBar", "warrior", "ninja")
class FooBar extends Component {
render() {
const { Foo, Bar, LazyBar, warrior, ninja } = this.props;
}
}
License
Dagger is available under the MIT license. See LICENSE for details.
1.0.3-rc.1
4 years ago
1.0.13
4 years ago
1.0.10
4 years ago
1.0.12
4 years ago
2.0.0-alpha.4
4 years ago
2.0.0-alpha.2
4 years ago
2.0.0-alpha.1
4 years ago
2.0.0-alpha.0
4 years ago
2.0.0
4 years ago
1.0.3
4 years ago
1.0.2
4 years ago
1.0.1
4 years ago
1.0.0
4 years ago