zazoomauro / node-dependency-injection

The NodeDependencyInjection component allows you to standarize and centralize the way objects are constructed in your application.
https://github.com/zazoomauro/node-dependency-injection/wiki
MIT License
276 stars 34 forks source link

Check arguments #189

Closed Ceres6 closed 1 year ago

Ceres6 commented 1 year ago

Hi!

I was wondering whether there is any way to check if the arguments that you pass to a registered service are the ones that are needed for the service to run.

If there isn't, but there could be a way (adding typings or something like that) I'd be happy to try to help and submit a PR as I think it would really enhance dev experience.

Thanks in advance.

zazoomauro commented 1 year ago

Hello @Ceres6

can you please add an example of what are you trying to achieve?

Thanks in advance

Ceres6 commented 1 year ago

Of course. Let say I have a the following classes:

class MyClass {
  constructor(a: A, b:B)
}

class A {}

class B {}

then I have in my dependency injection

container.register('A', A)
container.register('B', B)

I would like the IDE to show errors if my registration of my class is incomplete

container.register('MyClass', MyClass)  // Error: Expected 2 args got 0
container.register('MyClass', MyClass)
  .addArgument(New Reference('B')  // Error: Arg of type B is not assignable to arg of type A

As I would have if I instantiate it directly in typescript.

zazoomauro commented 1 year ago

@Ceres6 If you are using typescript What about using Autowire?

https://github.com/zazoomauro/node-dependency-injection/wiki/Autowire

Ceres6 commented 1 year ago

That may work with simple configurations but I usually have an interface with multiple implementations and each service might need a different one, so I'm not sure if I could make it work with autowire, maybe I'm missing some feature that would make that work

zazoomauro commented 1 year ago

@Ceres6 there is no problem implementing multiple interfaces with autowire.