@azera/container
Javascript dependecy injection container
Last updated a month ago by mdzzohrabi .
MIT · Original npm · Tarball · package.json
$ cnpm install @azera/container 
SYNC missed versions from official npm registry.

Azera Container

Intro

Azera container is a dependency injection service container for JavaScript written in Typescript.

First simple example :

import {Container} from "@azera/container";

let container = new Container;

container.set('logger', class Logger {
  log(message) {}
})

let app = container.invoke(['logger', class App {
  constructor(logger) {
    logger.log('Initialize app');
  }
}]);

Simple injection with decorators in Typescript :

import {Service, Container} from "@azera/container";

class Logger {
  @Inject('loggerNS') namespace: string;
  log(message) { console.log(`[${this.namespace}] ${message}`); }
}

@Inject([ Logger ]) class App {
  constructor(private logger: Logger) {}
  init() { this.logger.log('Initailize application'); }
}

let container = new Container();
// Set a parameter
container.setParameter('loggerNS', 'app');
// Create an instance from App
let app = container.invoke(App);
app.init();
// Console output : "[app] Initialize application"

Property injection

class App {
  @Inject('logger') logger: Logger;
}

Simply !

Container-aware class

import {ContainerAware, Container} from "@azera/container";

@Service('logger') class Logger {
  log(message) { console.log(message); }
}

class App extends ContainerAware() {
  init() {
    this.container.get('logger').log('Initialize app');
  }
}

let container = new Container();
container.add(Logger);
let app = container.invoke(App);
app.init(); // output: Initialize app

Factory

You can also use factories to generate services, only add Factory to the end of function name :

import {Container} from "@azera/container";

class Logger {
  log() {}
}

container.set('logger', function loggerFactory() {
  return new Logger;
});

let logger: Logger = container.get('logger');
// Or
let logger = container.get<Logger>('logger');

Tags

Also you can define tag for services :

import {Container, Tag} from "@azera/container";

abstract class Command { }

@Tag('command') class RunCommand extends Command {}
@Tag('command') class HelpCommand extends Command {}

let container = new Container;
container.add(RunCommand, HelpCommand);

let commands: Command[] = container.getByTag<Command>('command');
// Or inject them
class ConsoleApp {
  @Inject('$$command') commands: Command[];
}

Auto tagging

You can do tagging automatically :

import {Container} from "@azera/container";

abstract class Command { }

class RunCommand extends Command {}
class HelpCommand extends Command {}

container
  .autoTag(Command, [ 'command' ])
  .add(RunCommand, HelpCommand);
  
class ConsoleApp {
  @Inject('$$command') commands: Command[];
}

let app = container.invoke(ConsoleApp);

Create custom auto tagging function

container.autoTag( definition => {
  return definition.name.endsWith('Command') ? ['command'] : []
})

Predefined Services and Parameters

// services.ts
import {Inject} from "@azera/container";

export default {


  app: class App {

    @Inject('logger')
    logger: Logger;
    
    run() {
      // Run logic
    }

  },

  // You can also declare service with Definition schema
  logger: {
    service: class Logger {
      constructor(private ns: string) {}
    },
    parameters: [ '$loggerNS' ]
  }
}
// parameters.ts
export default {
  loggerNS: 'app'
}
// index.ts
import {Container} from "@azera/container";
import Services from "./services";
import Parameters from "./parameters";

let container = new Container(Services, Parameters);
let app = container.get('app');
app.run();

Type-based injection

We can also emit service configuration and naming and use type-based injection.

// Logger.ts
export default class Logger {
  log(message: string) {
    console.log(message);
  }
}
// App.ts
import Logger form './Logger.ts'

export default class App {
  constructor(@Inject() public logger: Logger) {}
}
// index.ts
import App from './App.ts';

new Container()
  .invoke(App)
  .logger
  .log('Hello World');

Async

@Service({
  factory: async function connectionFactory() {
    let connection = new Connection();
    await connection.connect();
    return connection;
  }
})
class Connection {
  name = "default";
  async connect() { /** Connection logic **/ }
  async execute(query: string) { /** Command Exection **/ }
}

class Model {
  constructor(@Inject() connection: Connection) {
  }
}

let container = new Container();
container.invokeAsync(Model).then(model => {
  console.log(model.connection.execute("SELECT * FROM User"));
});

Current Tags

  • 2.0.18                                ...           latest (a month ago)

36 Versions

  • 2.0.18                                ...           a month ago
  • 2.0.17                                ...           a month ago
  • 2.0.16                                ...           2 months ago
  • 2.0.15                                ...           3 months ago
  • 2.0.14                                ...           9 months ago
  • 2.0.13                                ...           9 months ago
  • 2.0.12                                ...           9 months ago
  • 2.0.11                                ...           9 months ago
  • 2.0.10                                ...           9 months ago
  • 2.0.9                                ...           9 months ago
  • 2.0.8                                ...           9 months ago
  • 2.0.7                                ...           a year ago
  • 2.0.6                                ...           a year ago
  • 2.0.5                                ...           a year ago
  • 2.0.4                                ...           a year ago
  • 2.0.3                                ...           2 years ago
  • 2.0.2                                ...           2 years ago
  • 2.0.1                                ...           2 years ago
  • 2.0.0                                ...           2 years ago
  • 1.1.0                                ...           2 years ago
  • 1.0.15                                ...           2 years ago
  • 1.0.14                                ...           2 years ago
  • 1.0.13                                ...           2 years ago
  • 1.0.12                                ...           2 years ago
  • 1.0.11                                ...           2 years ago
  • 1.0.10                                ...           2 years ago
  • 1.0.9                                ...           2 years ago
  • 1.0.8                                ...           2 years ago
  • 1.0.7                                ...           2 years ago
  • 1.0.6                                ...           2 years ago
  • 1.0.5                                ...           2 years ago
  • 1.0.4                                ...           2 years ago
  • 1.0.3                                ...           2 years ago
  • 1.0.2                                ...           2 years ago
  • 1.0.1                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (4)
Dev Dependencies (13)
Dependents (1)

Copyright 2014 - 2017 © taobao.org |