DEV Community

Ivan V.
Ivan V.

Posted on • Edited on

7 2

Dependency injection without the decorators

I'm a big fan of dependency injection since I'm working with the front-end a lot, I needed a dependency injection library that is not based on decorators and has a small file size. I couldn't find anything, so I've created one myself.

It's called PumpIt it's less than 2KB and this is how it works.

class A {
  static inject=[B]
  constructor(public b:B){}
}
class B {}

const container = new Pumpa()

container.bindClass(A,A)
container.bindClass(B,B)

const instanceA = container.resolve<A>(A)
Enter fullscreen mode Exit fullscreen mode

You can also register function factories

class B{}

function myFactory(b: B) {
  return function resolvedFunction() {
    // do something with b
  }
}

myFactory.inject = [B]

container.bindFactory(myFactory, myFactory)

const fn = container.resolve(myFactory)
fn === resolvedFunction
Enter fullscreen mode Exit fullscreen mode

There is also another way to register values with the container. It is particularly useful when you don't have access to the values you want to register (third-party libraries).

class A {
  //note: there is no static inject property on the class
  constructor(public b:B){}
}
class B {}

const container = new Pumpa()

//alternative way of registering
container.bindClass(A,{value:A,inject:[B]})
container.bindClass(B,B)

const instanceA = container.resolve<A>(A)
instanceA.b === B
Enter fullscreen mode Exit fullscreen mode

It also supports injecting an array of dependencies.

class A {
  static inject = [getArray([A, B, C])]
  constructor(deps: [A, B, C]) {}
}
Enter fullscreen mode Exit fullscreen mode

And that's the gist of it.

It is written in Typescript, it is modeled similarly to the Microsoft tsyringe (which I was using previously).

I've spent a lot of time on the documentation, so head over to the Github repository to see what else the library can do.

I'm open to new ideas and pull requests.

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

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

Okay