Skip to content

all-in-js/dependencies-injector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

npm Build Status Coverage Status

a Javascript & Nodejs dependencies injector

Usage

  • Container class
import { ContainerClass } from '@all-in-js/injector';

const initDeps = {
  key: function key() {}
};
const container = new ContainerClass(initDeps);

container.add(class TestService{});

container.resolve(['TestService', 'key'], function(testService, key) {});
// or
const [testService] = container.resolve('TestService');

// this `container.resolve` support various types, eg:
// `container.resolve(string[array<string>[function]])` 
// if argument just is a function, it's arguments would be parsed to be an array to be resolved.
  • Decorator
import { Injectable } from '@all-in-js/injector';

@Injectable
class TestService {}
  • inject to constructor
import { Inject } from '@all-in-js/injector';

@Inject('TestService')
class TestControler {
  constructor(testService) {
    this.testService = testService;
  }
}
  • inject to class's function prop
import { Inject } from '@all-in-js/injector';

class TestControler {
  @Inject('TestService')
  test(testService) {
    // ...
  }
}
  • inject to class's value prop
import { Inject } from '@all-in-js/injector';

class TestControler {
  @Inject('TestService') testService;
  toString() {
    // this.testService
  }
}
  • when inject to class's function prop, merge injected value and your arguments.
import { Inject } from '@all-in-js/injector';

class TestControler {
  @Inject('TestService')
  test(testService, yourArguments) {
    // ...
  }
}

new TestControler().test('yourArguments');

About

偏传统与现代结合的依赖注入 DI

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published