#Issue understanding dependency injection in this case

1 messages · Page 1 of 1 (latest)

median blaze
#

Let's say I have a first module which imports a typeorm repository which is used in a guard. That guard is exported by that module to be reused by other modules.

Then in a second module, I import first module and use the guard in my controller.
Why am I getting an error on startup stating that the dependency on the typeORM repository could not be resolved?
What I would think is that the instantiation of that repository is covered by ModuleB, why would moduleA care how to resolve it ?

gilded finch
#

Guards, and other enhancers, are not standard providers, and end up getting re-created per module context. There are a few ways around this if you need to re-use the same guard, mainly making the logic of the guard its own provider and injecting that logical provider into the guard, but generally guards are stateless and can be created per context without issue.

Rather than exporting the guard, have the module export theTypeOrmModule and your issue will be resolved

median blaze
gilded finch
#

We don't have that specifically documented, it is just knowledge picked up from trials and issues

median blaze
#

Thanks again !