带有延迟用户模块的身份验证模块 [英] Auth module with deferred User module
问题描述
我在通用库中有一个AuthModule
,它需要与UserModule
进行交互,类似于文档.
I have an AuthModule
in a generalized library which requires interacting with a UserModule
similar to the docs.
我想做的是定义一个UserService
必须遵守的接口,而不是实际的实现.这样会将实现细节留给库的用户.
What I want to do is define an Interface that the UserService
must adhere to instead of an actual implementation. This will leave the implementation details to the users of the library.
我尝试了几种不同的方法,例如最初使用字符串令牌APP_USER_SERVICE
null
,然后由实现者覆盖,但这似乎在注入null
值而不注入实际值时遇到了麻烦
I've tried a few different approaches such as having a string token APP_USER_SERVICE
initially null
then be overridden by the implementor, but this seemed to run into trouble injecting a null
value and not injecting the actual value.
我尝试过的另一种方法是执行AuthModule.withUserModule(UserModule)
,其中AuthModule
导入动态UserModule
并查找由UserModule
实现定义的APP_USER_SERVICE
令牌.这种方法的问题在于,它似乎陷入了循环依赖地狱.
Another approach I tried which I kind of like is doing AuthModule.withUserModule(UserModule)
where AuthModule
imports the dynamic UserModule
and looks for the APP_USER_SERVICE
token defined by the UserModule
implementation. The problem with this approach is it seems to run into circular dependency hell.
我是NestJS项目的新手,所以也许我缺少明显的东西.非常感谢您提供有关如何组织此工作流程的任何提示.
I'm new to the NestJS project, so maybe I'm missing something obvious. Any pointers on how to organize this workflow is greatly appreciated, thanks.
总之,这是我要使用的结构:
In summary this is the structure I am going for:
-> =取决于
图书馆:
AuthModule
-> IUserModule
USER:
AuthModule.withUserModule(UserModule)
填写IUserModule
要求.
UserModule
-> AuthModule
下面是代码(自动柜员机损坏):
Here is the code (broken atm):
推荐答案
使用forwardRef()
只是您的最后选择.或者,考虑将模块之一分成两个或多个部分,以打破循环依赖关系,例如:
Working with forwardRef()
should only be your last resort. Alternatively, consider splitting one of the modules into two or more parts to break the circular dependencies, e.g.:
AuthModule
进入
-
AuthLoginModule
被UserModule导入 -
ValidateAuthModule
导入UserModule
AuthLoginModule
gets imported by the UserModuleValidateAuthModule
imports the UserModule
或UserModule
分为两部分.
这篇关于带有延迟用户模块的身份验证模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!