https://stackoverflow.com/questions/28399536/how-to-best-structure-an-express-v4-11-project-with-socket-io
https://stackoverflow.com/questions/10869276/how-to-deal-with-cyclic-dependencies-in-node-js
While node.js does allow circular require dependencies, as you've found it can be pretty messy and you're probably better off restructuring your code to not need it. Maybe create a third class that uses the other two to accomplish what you need.
Circular dependencies are code smell. If A and B are always used together they are effectively a single module, so merge them. Or find a way of breaking the dependency; maybe its a composite pattern.
Not always. in database models, for example, if I have model A and B, in model A I may want to reference model B (e.g. to join operations), and vice-versa. Therefore, export several A and B properties (the ones that does not depend on other modules) before use the "require" function may be a better answer. – Joaobrunoah Jan 26 '15 at 18:38
3
I also don't see circular dependencies as code smell. I'm developing a system where there are a few cases where it is needed. For example, modeling teams and users, where users can belong to many teams. So, it's not that something is wrong with my modeling. Obviously, I could refactor my code to avoid the circular dependency between the two entities, but that would not be the most pure form of the domain model, so I will not do that. – Alexandre Martini Sep 6 '16 at 11:02
Then should I inject the dependency when needed, is that what you mean? Using a third to control the interaction between the two dependencies with the cyclic problem?
http://blog.carbonfive.com/2014/02/28/taking-advantage-of-multi-processor-environments-in-node-js/
https://www.joyent.com/blog/walmart-node-js-memory-leak
https://blog.risingstack.com/operating-node-in-production/
How to deal with cyclic dependencies in Node.js
https://stackoverflow.com/questions/10869276/how-to-deal-with-cyclic-dependencies-in-node-jsWhile node.js does allow circular require dependencies, as you've found it can be pretty messy and you're probably better off restructuring your code to not need it. Maybe create a third class that uses the other two to accomplish what you need.
Circular dependencies are code smell. If A and B are always used together they are effectively a single module, so merge them. Or find a way of breaking the dependency; maybe its a composite pattern.
Not always. in database models, for example, if I have model A and B, in model A I may want to reference model B (e.g. to join operations), and vice-versa. Therefore, export several A and B properties (the ones that does not depend on other modules) before use the "require" function may be a better answer. – Joaobrunoah Jan 26 '15 at 18:38
3
I also don't see circular dependencies as code smell. I'm developing a system where there are a few cases where it is needed. For example, modeling teams and users, where users can belong to many teams. So, it's not that something is wrong with my modeling. Obviously, I could refactor my code to avoid the circular dependency between the two entities, but that would not be the most pure form of the domain model, so I will not do that. – Alexandre Martini Sep 6 '16 at 11:02
Then should I inject the dependency when needed, is that what you mean? Using a third to control the interaction between the two dependencies with the cyclic problem?
http://blog.carbonfive.com/2014/02/28/taking-advantage-of-multi-processor-environments-in-node-js/
https://www.joyent.com/blog/walmart-node-js-memory-leak
https://blog.risingstack.com/operating-node-in-production/
Comments
Post a Comment