1.2.0 • Published 4 years ago
chubbyjs-container v1.2.0
chubbyjs-container
Description
A PSR-11 container implementation.
Requirements
- node: 10
- psr-container: ^1.0.0
Installation
Through NPM as chubbyjs-container.
npm i chubbyjs-container@1.0
Usage
There are two PSR-11 implementations:
Container
prototype (each get will return a new instance) and shared servicesMinimalContainer
shared services
MinimalContainer / Container
Factories
import FactoryInterface from 'chubbyjs-container/dist/FactoryInterface';
import Logger from 'some-logger/dist/Logger';
import MinimalContainer from 'chubbyjs-container/dist/MinimalContainer';
import MyService from './Service/MyService';
import PsrContainerInterface from 'psr-container/dist/ContainerInterface';
const container = new MinimalContainer();
container.factories(
new Map<string, FactoryInterface>().set(MyService.name, (container: PsrContainerInterface) => {
return new MyService(container.get(Logger.name));
}),
);
Factory
import FactoryInterface from 'chubbyjs-container/dist/FactoryInterface';
import Logger from 'some-logger/dist/Logger';
import MinimalContainer from 'chubbyjs-container/dist/MinimalContainer';
import MyService from './Service/MyService';
import PsrContainerInterface from 'psr-container/dist/ContainerInterface';
const container = new MinimalContainer();
// new
container.factory(MyService.name, (container: PsrContainerInterface) => {
return new MyService(container.get(Logger.name));
});
// existing (replace)
container.factory(MyService.name, (container: PsrContainerInterface) => {
return new MyService(container.get(Logger.name));
});
// existing (extend)
container.factory(MyService.name, (container: PsrContainerInterface, previous?: FactoryInterface) => {
if (!previous) {
throw 'Missing service';
}
const myService: MyService = previous(container);
myService.setLogger(Logger.name);
return myService;
});
Factory with Parameter
import MinimalContainer from 'chubbyjs-container/dist/MinimalContainer';
import Parameter from 'chubbyjs-container/dist/Parameter';
const container = new MinimalContainer();
container.factory('key', Parameter('value'));
Get
import MinimalContainer from 'chubbyjs-container/dist/MinimalContainer';
import MyService from './Service/MyService';
const container = new MinimalContainer();
container.get(MyService.name);
Has
import MinimalContainer from 'chubbyjs-container/dist/MinimalContainer';
import MyService from './Service/MyService';
const container = new MinimalContainer();
container.has(MyService.name);
Container
All methods of the MinimalContainer
and the following:
Prototype Factories
each get will return a new instance
import Container from 'chubbyjs-container/dist/Container';
import FactoryInterface from 'chubbyjs-container/dist/FactoryInterface';
import Logger from 'some-logger/dist/Logger';
import MyService from './Service/MyService';
import PsrContainerInterface from 'psr-container/dist/ContainerInterface';
const container = new Container();
container.prototypeFactories(
new Map<string, FactoryInterface>().set(MyService.name, (container: PsrContainerInterface) => {
return new MyService(container.get(Logger.name));
}),
);
Prototype Factory
each get will return a new instance
import Container from 'chubbyjs-container/dist/Container';
import FactoryInterface from 'chubbyjs-container/dist/FactoryInterface';
import Logger from 'some-logger/dist/Logger';
import MyService from './Service/MyService';
import PsrContainerInterface from 'psr-container/dist/ContainerInterface';
const container = new Container();
// new
container.prototypeFactory(MyService.name, (container: PsrContainerInterface) => {
return new MyService(container.get(Logger.name));
});
// existing (replace)
container.prototypeFactory(MyService.name, (container: PsrContainerInterface) => {
return new MyService(container.get(Logger.name));
});
// existing (extend)
container.prototypeFactory(MyService.name, (container: PsrContainerInterface, previous?: FactoryInterface) => {
if (!previous) {
throw 'Missing service';
}
const myService: MyService = previous(container);
myService.setLogger(Logger.name);
return myService;
});
Copyright
Dominik Zogg 2020