@ingress/di
v3.0.0-alpha.8
Published
Server-side dependency injection for ingress
Downloads
124
Maintainers
Readme
@ingress/di
Server-side dependency injection middleware for ingress
Based on angular di
Example (ES2015)
import di from "@ingress/di";
import {
perRequestProviders,
providers,
SomeDependency,
} from "./my-dependencies";
const container = di({ providers, perRequestProviders });
Configuration
There are a few ways to let the root container know about your dependencies and their lifetimes.
- Provide them at creation time (shown in example)
- Use
.Singleton
and.PerRequestLifetime
decorators exposed on a container instance - Builder (TODO) - Use a container builder to discover, configure, and load dependencies.
Decorator Configuration example
//configuration.js
import di from '@ingress/di'
const container = di()
const { Singleton, PerRequestLifetime } = container
export {
container,
Singleton,
PerRequestLifetime
}
//my-dependency.js
import { Singleton } from './configuration'
@Singleton
export class MyDependency {
...
}
//index.js
import ingress from '@ingress/core'
import { container } from './configuration'
import { MyDependency } from './my-dependency'
const app = ingress()
app.use(container).use((context, next) => {
const dep = context.scope.get(MyDependency)
...
})
It is important to note that in index.js
loading ./my-dependency
after ./configuration
is required.