问题
I have an AuthModule in a generalized library which requires interacting with a UserModule similar to the docs.
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.
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.
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.
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:
-> = depends on
LIBRARY:
AuthModule -> IUserModule
USER:
AuthModule.withUserModule(UserModule) to fill in the IUserModule requirement.
UserModule -> AuthModule
Here is the code (broken atm):
Library
Sample Implementation
回答1:
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 into
AuthLoginModulegets imported by the UserModuleValidateAuthModuleimports the UserModule
or respectively the UserModule into two parts.
来源:https://stackoverflow.com/questions/54472337/auth-module-with-deferred-user-module