Code Monkey home page Code Monkey logo

power-di's Introduction

Power DI

CI Coverage Version License

A lightweight Dependency Injection library. Using es6 and other features, remove unnecessary concepts, easy and convenient to use.

Install

npm i power-di --save

Example

a simple example

import { IocContext } from 'power-di';

class AService { }

// default instance, you can also new IocContext to get a instance.
const context = IocContext.DefaultInstance;
context.register(AService);
const aService = context.get(AService); // a instance of AService

inject with key

class AService { }

const context = IocContext.DefaultInstance;
context.register(AService, 'XService'); // key need a string or class, e.g super class or whatever class.
context.get('XService');

use with decorators

const context = new IocContext();

@injectable()
class NRService { }

@injectable()
class LITestService {
  @inject()
  public testService: NRService;
}

const test = context.get(LITestService);

collect impl or extends of some interface/base class

class A { }

@classInfo()
class B extends A { }

@classInfo()
class C extends A { }

@injectable()
class LITestService {
  @imports({ type: A })
  public testService: A[];
}

const test = context.get(LITestService); // test.testService as A[];

use in react

const context = new IocContext;
class NRService { }
context.register(NRService);

class TestComponent extends Component<{}, {}> {
  @inject()
  service: NRService;

  componentDidMount() {
    t.true(this.service instanceof NRService);
  }

  render(): any {
    return null;
  }
}

create(
  <IocProvider context={context}>
    <TestComponent />
  </IocProvider>
);

Class Loader

Power DI introduced the concept of class loader. We can customize the find rule of ioc.

See the test case for details.

power-di's People

Contributors

paranoidjk avatar zhang740 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.