Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Circular Dependency Solution

Our current project has ran into a circular dependency issue. Our business logic assembly is using classes and static methods from our SharedLibrary assembly. The SharedLibrary contains a whole bunch of helper functions, such as a SQL Reader class, Enumerators, Global Variables, Error Handling, Logging and Validation.

The SharedLibrary needs access to the Business objects, but the Business objects need access to SharedLibrary. The old developers solved this obvious code smell by replicating the functionality of the business objects in the shared library (very anti-DRY). I've spent a day now trying to read about my options to solve this but i'm hitting a dead end.

I'm open to the idea of architecture redesign, but only as a last resort. So how can i have a Shared Helper Library which can access the business objects, with the business objects still accessing the Shared Helper Library?

like image 909
gfoley Avatar asked Apr 07 '10 22:04

gfoley


People also ask

How can cyclic dependencies be resolved?

A cyclic dependency is an indication of a design or modeling problem in your software. Although you can construct your object graph by using property injection, you will ignore the root cause and add another problem: property injection causes Temporal Coupling. Instead, the solution is to look at the design closely.


2 Answers

You could create a separate project only for value objects (no logic) and interfaces.

Have your shared library classes implement the interfaces, and the Business library depend on the interfaces (do I hear more testable and decoupled code here? Not to mention you remove the dependency from the Shared Library).

Ideally, you could have the business objects on which your shared library depend on this extra project too. If the business objects are too complex, you could also transform them into interfaces.

You will have both projects not depending on each other, but only on another project with only "dummy" objects (no logic):

Business ---> Interfaces and value objects <--- Shared Library

Now those are decoupled =)

like image 173
Samuel Carrijo Avatar answered Sep 22 '22 07:09

Samuel Carrijo


Anytime you have a "shared" library you absolutely must not ever reference your business entity project. Doing so will cause this issue to happen as you obviously see.

The solution to this is remove all of the business entity dependent code from the shared library and either rearchitect away the need for that helper code or place that helper code inside the business entity project itself.

like image 45
Chris Marisic Avatar answered Sep 23 '22 07:09

Chris Marisic