Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/wookieb/alpha-dic

Powerful dependency injection container for node.js
https://github.com/wookieb/alpha-dic

alpha-packages dependency-injection dependency-injection-container ioc ioc-container javascript nodejs

Last synced: about 10 hours ago
JSON representation

Powerful dependency injection container for node.js

Awesome Lists containing this project

README

        

# Alpha DIC

[![CircleCI](https://travis-ci.org/wookieb/alpha-dic.svg?branch=master)](https://travis-ci.org/wookieb/alpha-dic)
[![Coverage Status](https://coveralls.io/repos/github/wookieb/alpha-dic/badge.svg?branch=master)](https://coveralls.io/github/wookieb/alpha-dic?branch=master)

Flexible Dependency Injection Container with support for asynchronous service creation, annotations and middlewares.

Features:
* Autowiring
* Supports middlewares
* Allows to define service as constructor, (async) factory or a value
* Detects cycle dependencies
* Prevents race condition for concurrent service requests
* Supports annotations (and ability to search services by annotations)
* Simple service definition via container methods or decorators

# Installation
```bash
npm install alpha-dic
```

# Example
```typescript
import {createStandard} from 'alpha-dic';

const container = createStandard();
// if you're defining services via decorators
// preloadServiceModules(container, './path/to/service/modules/*')

container.definitionWithFactory('my-service', async () => {
const connection = new DatabaseConnection();
await connection.connect();
return connection;
});

container.get('my-service').then((connection) => {
// connection has been established in factory
});

```

Example with dependencies and decorators
```typescript
import {createStandard, reference, AutowiredService, Service, OnActivation} from 'alpha-dic';
import * as assert from 'assert';

const container = createStandard();

@AutowiredService('NotificationCenter')
class NotificationCenter {
constructor(renderer: EmailRenderer, sender: EmailSender) {
assert.ok(renderer instanceof EmailRenderer);
// connection to SMTP was established in factory
assert.ok(sender instanceof EmailSender);
}
async sendEmail(to, type, vars) {
// render email
// send it via sender
}
}

@Service()
class EmailRenderer {

}

@Service()
@OnActivation(async (mailer) => {
await mailer.connectToSMTP();
return mailer;
})
class EmailSender {

}

container.get('NotificationCenter')
.then((centre) => {
return centre.sendEmail('[email protected]', 'limits-exceeded', {})
})
```

* [Simple usage rules](./docs/rules.md)
* [Defining services](./docs/defining-services.md)
* [Decorators](./docs/decorators.md)
* [Annotations](./docs/annotations.md)
* [Middlewares](./docs/middlewares.md)
* [Hierarchical container](./docs/hierarchical-di.md)
* [Injecting configuration values](./docs/configuration.md)
* [Hooks - @OnActivation, onActivation](./docs/on-activation.md)
* [Deprecating services](./docs/deprecating.md)
* [Autowiring](./docs/autowiring.md)
* [Big projects tips](./docs/big-project-tips.md)
* [Example](./example)