Auth module with deferred User module

此生再无相见时 提交于 2020-01-16 17:34:28

问题


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

  • AuthLoginModule gets imported by the UserModule
  • ValidateAuthModule imports the UserModule

or respectively the UserModule into two parts.



来源:https://stackoverflow.com/questions/54472337/auth-module-with-deferred-user-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!