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

@wessberg/di

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wessberg/di

A Dependency-Injection container that holds services and can produce instances of them as required. It mimics reflection by parsing the app at compile-time and supporting the generic-reflection syntax.

  • 1.0.20
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
581
decreased by-9.36%
Maintainers
1
Weekly downloads
 
Created
Source

DI (Dependency Injector)

NPM version Dev Dependencies

deps

License-mit

A Dependency-Injection container that holds services and can produce instances of them as required. It mimics reflection by parsing the app at compile-time and supporting the generic-reflection syntax.

Installation

Simply do: npm install @wessberg/di.

Description

DI is truly a fresh take on dependency injection in a TypeScript/JavaScript environment:

  • It does its work on compile-time. The only runtime dependency is the DIContainer itself, which is tiny.
  • It doesn't ask you to reflect metadata or to annotate your classes with decorators. "It just works".
  • It maps interfaces to implementations. Most popular dependency injection systems for TypeScript doesn't do this. This allows you to truly decouple an abstraction from its implementation.
  • It supports the .NET generic reflection flavour: registerSingleton<Interface, Implementation>(). No need for anything else.

Usage

import {DIContainer} from "@wessberg/di";
DIContainer.registerSingleton<IFoo, Foo>();
DIContainer.registerTransient<IBar, Bar>();
DIContainer.get<IBar>(); // Retrieves a concrete instance of the IBar service.

// And so on...

Overwriting a new-expression for a custom constructor

Sometimes, you may want to invoke the constructor of a service with custom arguments, rather than relying on every other non-initialized parameter of the service constructor to be dependency injected. You can do that by passing a function that returns a new instance of the provided service as the first argument registerSingleton or registerTransient.

DIContainer.registerSingleton<IFoo, Foo>(() => new Foo("customArg", 123, "foo"));

Making it work

To make the injections work - and to support the generic reflection notation - you need to compile the source code with the DI-Compiler.

If you are using Rollup, then use rollup-plugin-di to compile your code automatically as part of your bundle.

How does it work

It uses CodeAnalyzer to check all of your code and all of its dependencies recursively to track classes and the constructor arguments they take (and their order). When a DIContainer constructs a new instance of a service, it knows which concrete implementations match interfaces that exists in the signature of class constructors.

Changelog

1.0.20 (2017-07-19)

1.0.19 (2017-07-19)

1.0.18 (2017-07-19)

1.0.17 (2017-05-31)

  • 1.0.17 (b537054)
  • Added a 'noInject' decorator to the exports of the module which, together with the DI-compiler, will (29c539e)

1.0.16 (2017-05-30)

  • 1.0.16 (53d8f6b)
  • Added a new method: 'has', which returns true if a service matching the given generic type parameter (f759574)

1.0.15 (2017-05-24)

  • 1.0.15 (9d53d45)
  • Added the 'IGetOptions' and 'IRegisterOptions' to the exports of the module (df9b8c0)

1.0.14 (2017-05-24)

  • 1.0.14 (e2b442d)
  • Separated the DIContainer from the compiler. (cff58fc)

1.0.13 (2017-05-24)

1.0.12 (2017-05-19)

1.0.11 (2017-05-18)

  • 1.0.11 (7858546)
  • Fixed a bug where irrelevant CallExpressions would be validated unnecessarily. (872b616)

1.0.10 (2017-05-18)

1.0.9 (2017-05-18)

1.0.8 (2017-05-18)

  • 1.0.8 (ddd7752)
  • Mapped interfaces are now stored on the global object to support IIFE and arbitrary execution order. (d3f54a7)

1.0.7 (2017-05-18)

1.0.6 (2017-05-18)

1.0.5 (2017-05-16)

1.0.4 (2017-05-16)

1.0.3 (2017-05-14)

1.0.2 (2017-04-24)

  • 1.0.2 (89ff73d)
  • Fixed a bug where 'hasAltered' would always be false. Cleanup. Added typings to .gitignore and .npmi (ec99adc)

1.0.1 (2017-04-24)

  • 1.0.1 (b04742c)
  • Changed MagicString type to 'any' since there are no typings available (b5894af)
  • First commit (c1cf5b9)

1.0.19 (2017-07-19)

1.0.18 (2017-07-19)

1.0.17 (2017-05-31)

  • 1.0.17 (b537054)
  • Added a 'noInject' decorator to the exports of the module which, together with the DI-compiler, will (29c539e)

1.0.16 (2017-05-30)

  • 1.0.16 (53d8f6b)
  • Added a new method: 'has', which returns true if a service matching the given generic type parameter (f759574)

1.0.15 (2017-05-24)

  • 1.0.15 (9d53d45)
  • Added the 'IGetOptions' and 'IRegisterOptions' to the exports of the module (df9b8c0)

1.0.14 (2017-05-24)

  • 1.0.14 (e2b442d)
  • Separated the DIContainer from the compiler. (cff58fc)

1.0.13 (2017-05-24)

1.0.12 (2017-05-19)

1.0.11 (2017-05-18)

  • 1.0.11 (7858546)
  • Fixed a bug where irrelevant CallExpressions would be validated unnecessarily. (872b616)

1.0.10 (2017-05-18)

1.0.9 (2017-05-18)

1.0.8 (2017-05-18)

  • 1.0.8 (ddd7752)
  • Mapped interfaces are now stored on the global object to support IIFE and arbitrary execution order. (d3f54a7)

1.0.7 (2017-05-18)

1.0.6 (2017-05-18)

1.0.5 (2017-05-16)

1.0.4 (2017-05-16)

1.0.3 (2017-05-14)

1.0.2 (2017-04-24)

  • 1.0.2 (89ff73d)
  • Fixed a bug where 'hasAltered' would always be false. Cleanup. Added typings to .gitignore and .npmi (ec99adc)

1.0.1 (2017-04-24)

  • 1.0.1 (b04742c)
  • Changed MagicString type to 'any' since there are no typings available (b5894af)
  • First commit (c1cf5b9)

1.0.18 (2017-07-19)

1.0.17 (2017-05-31)

  • 1.0.17 (b537054)
  • Added a 'noInject' decorator to the exports of the module which, together with the DI-compiler, will (29c539e)

1.0.16 (2017-05-30)

  • 1.0.16 (53d8f6b)
  • Added a new method: 'has', which returns true if a service matching the given generic type parameter (f759574)

1.0.15 (2017-05-24)

  • 1.0.15 (9d53d45)
  • Added the 'IGetOptions' and 'IRegisterOptions' to the exports of the module (df9b8c0)

1.0.14 (2017-05-24)

  • 1.0.14 (e2b442d)
  • Separated the DIContainer from the compiler. (cff58fc)

1.0.13 (2017-05-24)

1.0.12 (2017-05-19)

1.0.11 (2017-05-18)

  • 1.0.11 (7858546)
  • Fixed a bug where irrelevant CallExpressions would be validated unnecessarily. (872b616)

1.0.10 (2017-05-18)

1.0.9 (2017-05-18)

1.0.8 (2017-05-18)

  • 1.0.8 (ddd7752)
  • Mapped interfaces are now stored on the global object to support IIFE and arbitrary execution order. (d3f54a7)

1.0.7 (2017-05-18)

1.0.6 (2017-05-18)

1.0.5 (2017-05-16)

1.0.4 (2017-05-16)

1.0.3 (2017-05-14)

1.0.2 (2017-04-24)

  • 1.0.2 (89ff73d)
  • Fixed a bug where 'hasAltered' would always be false. Cleanup. Added typings to .gitignore and .npmi (ec99adc)

1.0.1 (2017-04-24)

  • 1.0.1 (b04742c)
  • Changed MagicString type to 'any' since there are no typings available (b5894af)
  • First commit (c1cf5b9)

Keywords

FAQs

Package last updated on 19 Jul 2017

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