Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot add reference to project because of a circular dependency error

I created 2 dummy projects in my application and named them BAL and DAL. When I build them, they build successfully. If I add a reference to BAL to the DAL project, it added nicely. But while adding the DAL reference to the BAL project, I get the following error:

A reference to DAL could not be added. Adding this project as a reference would cause a circular dependency.

Can anyone help me to solve this error?

like image 333
Venkat Avatar asked Aug 17 '11 13:08

Venkat


People also ask

How do you fix a circular dependency problem?

There are a couple of options to get rid of circular dependencies. For a longer chain, A -> B -> C -> D -> A , if one of the references is removed (for instance, the D -> A reference), the cyclic reference pattern is broken, as well. For simpler patterns, such as A -> B -> A , refactoring may be necessary.

How do I get rid of circular dependency?

The Mediator Pattern can also help to lift circular dependencies by encapsulating the bidirectional interaction of two or more objects. The downside of your current code is (besides the circular dependency), that whenever class A changes and also data persistence changes, you have to touch and modify class B.

What causes circular dependency?

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.

How do you fix cyclic dependency in Python?

The easiest way to fix this is to move the path import to the end of the node module. By the way, is there some reason other than you "liking" it that you want one class per module? The few times I've seen this preference, it was because it's Java-like. the link to Importing Python Modules is broken.


2 Answers

Here's what you need to do:

  1. Right click on the DAL Project in the solution explorer and select Project dependencies in the context menu.

  2. You will now see a window that shows the project dependencies of the DAL Project. Make sure that BAL isn't checked.

Now you should be able to add your reference...

I hope this helps I've tried to keep it as simple and straight forward as possible.

Explanation:

Your DAL should not be able to access the BAL. Your code reference dependencies should be like this:

MVC project -> BAL -> DAL

The MVC project should reference the BAL, the BAL should reference the DAL. Set up your project like this. Make it work and then you will better understand why this setup is better.

Given:

  1. Data = raw numbers and strings
  2. Information = processed data into something meaningful

Cosider the following: The UI should get its information from the BAL which could be able to compose it's data based on the DAL.

like image 180
6 revs, 3 users 78% Avatar answered Sep 28 '22 06:09

6 revs, 3 users 78%


You can only reference in one way otherwise you get the error like you said. Just do this: delete the reference from your DAL to your BL and make a new one from your BL to your DAL!

like image 29
jefsmi Avatar answered Sep 28 '22 04:09

jefsmi