What are the 'best' practices regarding circular dependencies amongst DLL's in .net?
I am trying to work on a DLL for error handling/logging that needs to read/write XML.
I already have several classes to help with XML manipulation in a seperate 'utility' type DLL.
It would be nice to incorporate my fancy-pants error handling classes into the utility DLL, but I also need the XML manipulation code for the error logging DLL.
I was thinking I should keep the DLL's seperate for re-use in other projects, but now I'm not sure what the best approach would be.
Any suggestions on how to handle this scenario?
To resolve circular dependencies: Then there are three strategies you can use: Look for small pieces of code that can be moved from one project to the other. Look for code that both libraries depend on and move that code into a new shared library. Combine projectA and projectB into one library.
A circular dependency occurs when two classes depend on each other. For example, class A needs class B, and class B also needs class A. Circular dependencies can arise in Nest between modules and between providers. While circular dependencies should be avoided where possible, you can't always do so.
To reduce or eliminate circular dependencies, architects must implement loose component coupling and isolate failures. One approach is to use abstraction to break the dependency chain. To do this, you introduce an abstracted service interface that delivers underlying functionality without direct component coupling.
By running a cli command npx madge --circular --extensions ts ./ we can quickly get a list of circular dependencies of all . ts files in current directory and its subdirectories. That's it! Now you see where you have circular dependencies and can go and fix it.
One way of handing it is to merge one part into the other part's assembly. My experience is that people tend to fragment their project into lots of small assemblies which does not really improve anything but causes dependency problems.
I generally advocate very few big assemblies. Use assemblies as a unit of deployment, not to structure your code. Structure your code using namespaces.
A different way to fix this is to introduce interfaces, either in a common assembly or in one of the two existing assemblies. The latter case makes sense if the interface methods themselves do not have a circular dependency but the method bodies do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With