revane-ioc
revane-ioc
is a inversion of control framework inspired by spring.
Table of Content
Installation
npm i revane-ioc
Example
//userRepository.jsconst Repository = ; { return name: 'max' }; moduleexports = ; //userController.jsconst Controller = ; { thisuserRepository = userRepository; } { router; }; moduleexports = ; //app.jsconst RevaneIOC = ; const options = basePackage: __dirname loaderOptions: componentScan: true basePackage: __dirname ;const revane = options;revane ;
Usage
Component registration
Components may be registered by json file, xml file or by scanning for components. The class property in the configuration files accepts three different kind of paths:
- Absolute paths starting with
/
- Paths relative to
basePackage
starting with./
- Names of modules
Configuration files may be passed with the configurationFiles
option. The configurationFiles
requires absolute paths.
Json File
//app.jsconst options = basePackage: __dirname loaderOptions: file: __dirname + '/config.json' ;const revane = options;revane
Xml File
//app.jsconst options = basePackage: __dirname loaderOptions: file: __dirname + '/config.xml' ;const revane = options;revane
Component Scanning
The component scan scans for decorated classes.
It determines the id, scope and dependencies of the decorated class (if not passed as options to the decorator). The id of a bean is based on the class name. The dependencies will be determined by the constructor of the class and passed to the constructor at the creation of a bean.
Note: The component scan is enabled by default.
Note: The basePackage
option determines which folder will be scanned.
Note: The component scan may be deactivated with the componentScan
option.
//userRepository.jsconst Repository = ; { return name: 'max' }; moduleexports = UserRepository; //controller.jsconst Controller = ; { thisuserRepository = userRepository; } { router; };
The component scan may be activated in a xml file, too:
Filters
It is possible to apply filters to the component scan. There are two types of filters: includeFilters
and excludeFilters
.
//app.jsconst options = basePackage: __dirname loaderOptions: componentScan: true basePackage: __dirname excludeFilters: type: 'regex' regex: '.*Mock.js' ;const revane = options;revane
Dependency Injection
If configuration by json file is used it is possible to inject dependencies to a class by adding a reference or a value object to the properties
:
The xml files work the same way.
If component scanning is used the dependencies defined by the constructor parameters of a class or defined by the options of the decorator are being injected.
Post Construct
If a class has a postConstruct
function it will be executed after the creation of the bean.
{ // do something amazing }
Scopes
There are two possible scopes: singleton
and prototype
. If no scope is specified singleton
will be used.
Bean Factory
Beans may be defined by decorating a method, that returns a bean, on a class
with the @Bean
decorator.
API
Container
const RevaneIOC = ; const options = basePackage: __dirname;const revane = options;revane
get(id)
Returns the bean for the id
. Throws an error if no bean with the id
is found.
has(id)
Allows to check if a bean for id
exists.
getMultiple(ids)
Returns multiple beans specified by the ids
.
initialize(): Promise<void>
Initializes the container by reading all configured configuration files and performes the component scan. Returns a Promise that resolves when all postConstruct
calls are finished.
tearDown(): Promise<void>
Calls preDestroy()
on all beans if present.
options
componentScan
A boolean
that enables or disables the component scan. Defaults to true
.
basePackage (required)
The base package where the container looks for files.
noRedefinition
Prevents the duplicate defininion of beans. If a duplicate definition is found an error will be thrown. Defaults to true
.
configurationFiles
An array
of absolute paths to configuration files, that provide bean definitions.
includeFilters
excludeFilters
Decorators
Component(options: string | Options): Function
Used to declare classes as components to be considered by the component scan.
In Javascript it is necessary to call the decorator on the class:
const Service = ; {}Example
In Typescript it is possible to use them as decorators if the
experimentalDecorators
option is activated.
;
options
It is possible to pass the following options to the Component
decorator:
- id - the id of the bean
- dependencies - the dependencies of the class
Example
Service(options: string | Options): Function
Alias for Component
.
Repository(options: string | Options): Function
Alias for Component
.
Controller(options: string | Options): Function
Alias for Component
.
Scope(scope: string): Function
Adds a scope to a class. Possible values: singleton
, prototype
const Scope = ; {}Example
Inject(ids: string | string[])
Sets the properties defined by ids
at the decorated class.
const Inject = ; {}Example