DEV Community

wszgrcy
wszgrcy

Posted on • Edited on

Using dependency injection in nodejs environment

Introduction

  • Angular dependency injection standalone version
  • The usage method is completely consistent with Angular's dependency injection
  • No transformer required
  • 0 dependencies
  • Remove Decorator > @Injectable()=>static injectOptions={} > @Inject() xx=>xx=inject() > @Optional()=>xx=inject(token,{optional:true})
  • JS/TS Support

Source

  • Angular 17.3.6

Usage

  • npm i static-injector
  • Create a first level dependency injector with Injector.create
import { Injector, inject } from 'static-injector';

class Main {
  child = inject(Child);
}
class Child {
  output() {
    return 'hello world';
  }
}
let injector = Injector.create({ providers: [Main, Child] });
const instance = injector.get(Main);
console.log(instance.child.output());
Enter fullscreen mode Exit fullscreen mode

Different from injection-js

  • injection-js belongs to dynamic dependency injection and is a version used before Angular5. Currently no longer updated
  • The two are basically interchangeable (the details need to be adjusted)

  • Support the use of inject during construction

No Decorator

  • The original use of @Injectable() to pass parameters has been changed to static injectOptions={}. If there are no parameters, there is no need to set them
  • Originally, @Optional, @SkipSelf, @Self, please use the second pass parameter of inject instead

Test

  • Partially conducted unit testing to ensure that most functions are functioning properly
  • Because most of the code itself is extracted from Angular, stability is definitely guaranteed

Sync

  • Currently, the synchronization logic has been refactored and modified using @code-recycle/cli to ensure consistency with the official version of angular

Examples

Contact me

  • If you have any technical issues, please contact me for assistance wszgrcy@gmail.com

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay