#Enhancers as "pseudo-providers" explanation?

1 messages · Page 1 of 1 (latest)

lapis quartz
#

So I am trying to get into Nest for the first time, reading the docs and toying with the DI system to get my bearings. In my experimentation, I found myself in a rather confusing situation pretty much exactly like what is described here: https://github.com/nestjs/nest/issues/3856

Basically, I wrote a guard (call it G) which had a dependency on a provider (call it P). And I had a controller (C) in a different module (M2) which used that guard (but no explicit dependency on the provider used by the guard). And I was having a hard time figuring out how to inject that guard into a module with a controller that used it. Tried putting it in providers, but that only worked if I also included the guard's dependency in the providers even though my module (M2) had no explicit dependency on that. I eventually independently stumbled into a similar solution that @wet blade describes in the aforementioned issue, encapsulating the guard G in a module (M1) which exports it (and its dependency), and then importing that module in the other module (M2) where my controller needs it. But it was bothering me that I had to also export the implicit dependency (provider P) in order for G to work in M2, because this is not necessary for normal providers and I don't really understand why this situation requires different semantics for providers vs enhancers.

I think I might be missing something fundamental about how class-based enhancers are typically used and how they get instantiated for injection, so I'm looking for some technical clarification on why this approach (M1 w/ providers G & P must export P along with G) requires additionally exporting the implicitly depended-upon provider P when this is not necessary for injection of true 'providers'.

GitHub

Bug Report A Guard with dependencies can't be injected into another module. Current behavior I have 3 modules UserModule, AuthModule and ProtectedModule ProtectedModule imports AuthModule. Auth...

#

To add some more context, I generally understand (I think) after reading a bunch of discussions around this topic that enhancers and providers, while similar and apparently easily confused, are not the same beast and so I shouldn't expect the semantics around DI to be exactly the same for both of them. And if I understand correctly, an enhancer isn't ever really supposed be injected into a module as a provider. Rather they participate in dependency injection sort of...implicitly? And any dependencies they have must be explicitly imported into the module which is making use of that enhancer?

Assuming that's right (and it may not be, please correct me if not), it seems to create a situation in which any module that wants to use a guard/pipe/etc needs to know the implementation details of that enhancer and make sure that it imports any providers required to instantiate it. I suppose that makes sense because those deps are necessary, but it strikes me as a bit confusing because you end up with a module that looks like it's importing providers which it's not actually using, and you wouldn't know if or why it needs them unless you peeked into the implementation of some guard outside the module.