Cannot add reference to project because of a circular dependency error
Asked Answered
P

11

54

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?

Pluperfect answered 17/8, 2011 at 13:46 Comment(1)
Just try some of the top entries in a quick Google search. en.wikipedia.org/wiki/Circular_dependency. Good explanation in another question on SO at #309415Vertebrate
O
42

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.

Opus answered 17/8, 2011 at 13:46 Comment(0)
T
14

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!

Trainor answered 17/8, 2011 at 13:51 Comment(2)
Use a common layer than... More specific: Add a new layer in your project called common and then put your business objects in this layer. Now you can reference to this layer in all your BL and DAL Layer.Trainor
I just add like that only create 2 new projects but still i am getting the issuePluperfect
V
10

It is implicit in the concept of "layers" that higher layers depend on lower ones, and not the other way round. If 2 "layers" are mutually dependent, then one is not higher than the other, they are not layers in any meaningful sense, and so can be considered to be in the same layer. The same basic principle holds for architectural components or modules, as enforced by Studio for project dependencies. If you use this principle - think of your projects as design modules rather than e.g. just throwing everything into a single project - you will have well-structured codebase which will give you far less problems as it grows in size.

Viafore answered 17/8, 2011 at 16:16 Comment(9)
-1 as I still have no idea, after reading your answer, how to make VS do what I need.Eager
-1 as this didn't answer the question of the user or myself, who is having same situation.Decorative
You make a very valid point, however it is not always possible to just rewrite a massive code base that you have inherited that happens to be poorly written.Mo
What about the Domain Model which exists in the business layer, and apparently needs to be used by DAL datamapper classes, how do you solve this problem? p.s. without resorting to anemic domain model that will help you get the domain model out of the business layer.Combings
To the downvoters - I think it's not a case of making Visual Studio do what you want, it's more that you're wanting the wrong thing. This is a fundamental concept.Wellesley
I would put forwards the argument that mutually-dependent projects when it comes to DBs are not as uncommon as one might believe. Given two distinct databases for the same general purpose accessed via separate and distinct apps, having data that resides solely in each but required by the other, reduces the need for duplicate data and, by design, the need to ensure that same data in both is kept in sync.Eruptive
.. further to that though, there is no current resolution to the issue as stated, so I will be looking to split my projects into separate solutions and adding references to the databases themselves for each environment, in the hope that will be a reasonable workaround. Refactoring the 7 DBs I have inherited to provide an absolutely correct separation of 'layers' as there will be none in each solution. Not perfect I grant you, but if it works ......Eruptive
This is a fine general point. But it completely fails to answer the question.Silvie
You gotta love stackoverflow. It is as if "truth" and "correctness" are matters decidable by majority vote. "But I want to make VS do what I need" wrote the ignoramus in a comment, and 33 more ignoramuses up-voted his comment.Diversiform
O
3

That would cause a circular dependency. What you perhaps want to do instead is have a main application project, which references the BAL, and then BAL referenes DAL. Data access should not need to reference business logic.

Organ answered 17/8, 2011 at 14:1 Comment(0)
S
2

This just happened to me. You have a circular dependency, i.e. two projects both referencing each other. You need to make one of them independent of the other. Takes some time and it happens so quick. One second I was happily coding along, and the next I had 45 errors like this. Just took some time but it makes your architecture/program structure better too, helping you sort out dependencies properly.

Syncarpous answered 12/12, 2014 at 18:46 Comment(0)
C
2

Occasionally, you have two different projects, each of which needs methods that the other has. In this case, you can either make a third project and move the shared code into there, or choose one of the two projects to put the shared code in.

Catholicon answered 28/5, 2020 at 16:23 Comment(1)
I think this answer is the only correct oneNolen
E
1

This problem occurred to me when I was building a WPF application with several layers like repository interface layer, repository service layer, sql service layer, rest service layer and my main WPF UI layer.

  • I resolved this error. I noticed that some of the layers were unnecessarily referencing other projects. I removed this unnecessary reference.
  • Then I noticed that some of my service layer and repository layer had my WPF UI project as reference(My StartUp project); this is what was creating circular reference. I removed this.

========================================================================

Conclusion: Check each projects reference dependency and make sure there are no unnecessary reference. Make sure sub layers are not referencing the startup project in the reference.

Hope I was helpful.

Exult answered 20/11, 2016 at 11:44 Comment(0)
D
0

In my case I copied a project file without generating a new ProjectGuid. Since Visual Studio uniquely identifies projects using the ProjectGuid, it assumed the project was trying to reference itself.

Depreciate answered 14/10, 2016 at 9:37 Comment(0)
U
0

in my case the project was somehow already declared in refrences in csproj.cs in the targeted project so simply i removed all the dependency in csproj.cs and tried to add from main project again and it worked

Uttasta answered 18/10, 2021 at 9:28 Comment(0)
K
0

I had two projects called Application and Persistance that the reference of the Application project to the Persistance project encountered a circular dependency error. By clicking on the Application project and looking at the values used inside it, you will notice that the Persistance project is include inside the <ItemGroup> tag, which is why you receive a Circular Dependency error. To fix this error, just Comment this Include.

Kikuyu answered 2/1, 2022 at 13:9 Comment(1)
Please do not add an image of code, when you could just paste the code itself using the code tagging in the edit. See also: meta.#286051Ben
V
-5

To get around this, add the reference by browsing to the projects DLL after it has been built. Do not select it from the "Projects" tab.

Vermicelli answered 27/7, 2012 at 18:43 Comment(2)
Adding a reference to a DLL in another project's bin directory is typically a Very Bad Thing, especially if the projects are in the same solution and you have a build server set up. It will cause headaches.Asir
Circular dependencies between projects is a really bad idea, and will give non-reproducible builds. Every time you build, you will reference the DLL from the previous build.Mho

© 2022 - 2024 — McMap. All rights reserved.