Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

power-di

Package Overview
Dependencies
Maintainers
1
Versions
121
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

power-di

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

  • 2.4.34
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
299
increased by15%
Maintainers
1
Weekly downloads
 
Created
Source

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.

Keywords

FAQs

Package last updated on 31 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc