Home > Software engineering >  Angular circular dependency: best practices for handle it
Angular circular dependency: best practices for handle it

Time:10-05

I have UserService and GroupService in my app and it make sense that both services will know each other and use each other functions.

There are several tools in angular that give me to "fix" the circular dependency warning.

But everywhere it is written that if you have circular dependency, it's a sign that you have bad architecture.

So, what is the good architecture for cases like that? Why is it right to force (quite forcibly) one of the services not to know the other?

CodePudding user response:

So the problem is when one service need to be instantiated, inside constructor it will require second service. But that second service in order to be instantiated, inside constructor it will require first service.

What you can do is move all the logic from these 2 services into one "parent" service, and use this "parent" service through the application.

CodePudding user response:

you have 2 options:

  1. Extract commonly needed parts of the logic into some SharedModule

  2. If some part of any modules logic is required in other modules then it doesn't belong into that feature module but in top level module (the one that starts the app, does core app things and loads other feature modules).

CodePudding user response:

From my experience the best practice of circular dependency in angular is to ignore them. ha ha

  • Related