New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@exosite/ziras-di

Package Overview
Dependencies
Maintainers
3
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@exosite/ziras-di

Minimal Dependency Injection

  • 1.0.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
219
increased by27.33%
Maintainers
3
Weekly downloads
 
Created
Source

ziras-di

Minimal Dependency Injection

Example

Di.global is a singleton of the Di class.

Register your dependency to a di instance.

Di.global.YourDependency = YourDepedency;
Di.global.yourDependency = new YourDependency();
Di.global.yourDependency = (...args) => new YourDependency(...args);

Apply a Di instance to your class. It applies to both itself and its prototype.

import Di from '@exosite/ziras-di';

class YourClass {}
Di.global(YourClass);

Use dependency depending on how it's registered.

import Di from '@exosite/ziras-di';

class YourClass {
    static factory() {
        this.yourDependency = new this.di.YourDependency();
        this.yourDependency = this.di.yourDependency;
        this.yourDependency = this.di.yourDependency();
    }

    constructor() {
        this.yourDependency = new this.di.YourDependency();
        this.yourDependency = this.di.yourDependency;
        this.yourDependency = this.di.yourDependency();
    }
}
Di.global(YourClass);

Create a Di instance and register services.

const di = new Di({
  YourDependency,
  yourDependency: new YourDependency(),
  yourDependency(...args) {
    return new YourDependency(...args);
  }
});

Of course you can still do the old-school way. But it's not recommended.

const di = new Di();
di.YourDependency = YourDepedency;
di.yourDependency = new YourDependency();
di.yourDependency = (...args) => new YourDependency(...args);

Test a Di'ed class with a different Di instance.

const di = new Di();
const SubclassYourClass = di.extends(YourClass);

// Test against SubclassYourClass

The reason a subclass is created and tested against is so that the original class isn't modified and this should prevent us from making mistakes, e.g. forgot to re-apply the original Di instance to the original class.

Apply di to an object is also possible.

const yourObject = {};
Di.global(yourObject);

Development

Run test in Node.js

npm test -- watch

Run test in browser

npm start

License

MIT

FAQs

Package last updated on 15 Dec 2021

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