youmiya
v0.1.12
Published
Simple modern dependency injection container.
Downloads
14
Readme
🍫 Youmiya
💡 Package
youmiya
is a set of atom packages of@youmiya/*
, including:
@youmiya/core
@youmiya/modular
Youmiya
is a simple dependency injection (DI) library for Typescript with modern features:
- Supports both decorator style and interface-oriented style
- Works without reflect-metadata support (interface-oriented style)
- On-demand lazy (delayed) instantiation
- Support async module injection
- Supports hierarchical containers
- Circular dependency detection & workaround
And more features are on the way:
- Interceptors like tsyringe
- Injection transform
- Modular system
- Idle-time load & instatiation
Getting Started
Basic Usage (decorator style)
reflect-metadata
polyfill is needed for decorator style.
import { injectable, inject, rootContainer } from 'youmiya';
@injectable()
class Foo {
echo() {
console.log('foo');
}
}
@injectable()
class Bar {
constructor(@inject() private readonly foo: Foo) {}
echo() {
this.foo.echo();
console.log('bar');
}
}
const bar = rootContainer.resolve(Bar);
bar.echo(); // foobar
Basic Usage (interface-oriented style)
Likes VSCode, works well without reflect-metadata
.
foo.ts
import { createProviderIdentifier, rootContainer } from 'youmiya';
// define and export interface
export interface IFoo {
echo: () => void;
}
// define an identifier for provider
export const IFoo = createProviderIdentifier<IFoo>('Foo');
// implement the class
class FooImpl implements IFoo {
echo() {
console.log('foo');
}
}
// bind provider identifier to implementation
rootContainer.register(Foo).toClass(FooImpl);
bar.ts
import { createProviderIdentifier, rootContainer } from 'youmiya';
import { IFoo } from './foo';
interface IBar {
echo: () => void;
}
const IBar = createProviderIdentifier<IBar>('Bar');
class BarImpl implements IBar {
constructor(@IFoo() private readonly foo: IFoo) {}
echo() {
this.foo.echo();
console.log('bar');
}
}
rootContainer.register(IBar).toClass(BarImpl);
const bar = rootContainer.resolve(IBar);
bar.echo(); // foobar
Prerequisities
Youmiya requires a modern JavaScript engine with support for:
- Map
- Typescript
experimentalDecorators
The following requires are depending on your usage:
- Typescript
emitDecoratorMetadata
andreflect-metadata
polyfill- *only required if using decorator style
- Promise
- *only required if using async module provider
- Proxy
- *only required if using lazy instatiation
If your environment doesn't support one of these you will need to import a shim or polyfill.