A minimal Dependency Injection Container (DIC) which implements PSR-11.
- node: 12
- @chubbyjs/psr-container: ^1.0
Through NPM as @chubbyjs/chubbyjs-container.
npm i @chubbyjs/chubbyjs-container@1.0.9
There are two PSR-11 implementations:
Container
prototype (each get will return a new instance) and shared servicesMinimalContainer
shared services
import FactoryInterface from '@chubbyjs/chubbyjs-container/dist/FactoryInterface';
import Logger from 'some-logger/dist/Logger';
import MinimalContainer from '@chubbyjs/chubbyjs-container/dist/MinimalContainer';
import MyService from './Service/MyService';
import PsrContainerInterface from '@chubbyjs/psr-container/dist/ContainerInterface';
const container = new MinimalContainer();
container.factories(
new Map<string, FactoryInterface>([
[MyService.name, (container: PsrContainerInterface): MyService => {
return new MyService(container.get<Logger>(Logger.name));
}]
])
);
import FactoryInterface from '@chubbyjs/chubbyjs-container/dist/FactoryInterface';
import Logger from 'some-logger/dist/Logger';
import MinimalContainer from '@chubbyjs/chubbyjs-container/dist/MinimalContainer';
import MyService from './Service/MyService';
import PsrContainerInterface from '@chubbyjs/psr-container/dist/ContainerInterface';
const container = new MinimalContainer();
// new
container.factory(MyService.name, (container: PsrContainerInterface): MyService => {
return new MyService(container.get<Logger>(Logger.name));
});
// existing (replace)
container.factory(MyService.name, (container: PsrContainerInterface): MyService => {
return new MyService(container.get<Logger>(Logger.name));
});
// existing (extend)
container.factory(
MyService.name,
(container: PsrContainerInterface, previous?: FactoryInterface): MyService => {
if (!previous) {
throw 'Missing service';
}
const myService: MyService = previous(container);
myService.setLogger(container.get<Logger>(Logger.name));
return myService;
}
);
import MinimalContainer from '@chubbyjs/chubbyjs-container/dist/MinimalContainer';
import Parameter from '@chubbyjs/chubbyjs-container/dist/Parameter';
const container = new MinimalContainer();
container.factory('key', Parameter('value'));
import MinimalContainer from '@chubbyjs/chubbyjs-container/dist/MinimalContainer';
import MyService from './Service/MyService';
const container = new MinimalContainer();
container.get<MyService>(MyService.name);
import MinimalContainer from '@chubbyjs/chubbyjs-container/dist/MinimalContainer';
import MyService from './Service/MyService';
const container = new MinimalContainer();
container.has(MyService.name);
All methods of the MinimalContainer
and the following:
each get will return a new instance
import Container from '@chubbyjs/chubbyjs-container/dist/Container';
import FactoryInterface from '@chubbyjs/chubbyjs-container/dist/FactoryInterface';
import Logger from 'some-logger/dist/Logger';
import MyService from './Service/MyService';
import PsrContainerInterface from '@chubbyjs/psr-container/dist/ContainerInterface';
const container = new Container();
container.prototypeFactories(
new Map<string, FactoryInterface>([
[MyService.name, (container: PsrContainerInterface): MyService => {
return new MyService(container.get<Logger>(Logger.name));
}]
])
);
each get will return a new instance
import Container from '@chubbyjs/chubbyjs-container/dist/Container';
import FactoryInterface from '@chubbyjs/chubbyjs-container/dist/FactoryInterface';
import Logger from 'some-logger/dist/Logger';
import MyService from './Service/MyService';
import PsrContainerInterface from '@chubbyjs/psr-container/dist/ContainerInterface';
const container = new Container();
// new
container.prototypeFactory(
MyService.name,
(container: PsrContainerInterface): MyService => {
return new MyService(container.get<Logger>(Logger.name));
}
);
// existing (replace)
container.prototypeFactory(
MyService.name,
(container: PsrContainerInterface): MyService => {
return new MyService(container.get<Logger>(Logger.name));
}
);
// existing (extend)
container.prototypeFactory(
MyService.name,
(container: PsrContainerInterface, previous?: FactoryInterface): MyService => {
if (!previous) {
throw 'Missing service';
}
const myService: MyService = previous(container);
myService.setLogger(container.get<Logger>(Logger.name));
return myService;
}
);
Dominik Zogg 2021