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?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Venkat
  • 551
  • 1
  • 4
  • 3
  • 2
    Just try some of the top entries in a quick Google search. http://en.wikipedia.org/wiki/Circular_dependency. Good explanation in another question on SO at http://stackoverflow.com/questions/308915/why-are-circular-references-in-visual-studio-a-bad-practice – Peter Kelly Aug 17 '11 at 13:51

11 Answers11

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.

Regent
  • 5,142
  • 3
  • 21
  • 35
K-Dawg
  • 3,013
  • 2
  • 34
  • 52
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!

jefsmi
  • 721
  • 2
  • 9
  • 19
  • 10
    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. – jefsmi Aug 17 '11 at 13:54
  • I just add like that only create 2 new projects but still i am getting the issue – Venkat Aug 17 '11 at 13:58
9

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.

  • 33
    -1 as I still have no idea, after reading your answer, how to make VS do what I need. – Nathan C. Tresch Apr 28 '13 at 20:00
  • 1
    -1 as this didn't answer the question of the user or myself, who is having same situation. – SnapJag Jan 14 '14 at 05:51
  • 4
    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. – ElGavilan Feb 03 '14 at 14:53
  • 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. – Sisyphus Feb 14 '14 at 12:15
  • 2
    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. – Michael12345 May 14 '14 at 23:16
  • 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. – LogicalMan Aug 28 '17 at 18:34
  • .. 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 ...... – LogicalMan Aug 28 '17 at 18:44
  • This is a fine general point. But it completely fails to answer the question. – Auspex Jan 02 '19 at 16:14
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.

plenderj
  • 193
  • 1
  • 1
  • 7
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.

richard
  • 12,263
  • 23
  • 95
  • 151
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.

Graham Laight
  • 4,700
  • 3
  • 29
  • 28
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.

Abhay Shiro
  • 3,431
  • 2
  • 16
  • 26
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.

Sebazzz
  • 1,205
  • 2
  • 15
  • 33
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

mahmoud elgml
  • 161
  • 1
  • 6
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.

  • 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: https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question – Brian Tompsett - 汤莱恩 Jan 02 '22 at 13:27
-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.

Tony Raymond
  • 187
  • 10
  • 5
    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. – TrueWill Oct 03 '12 at 15:11
  • 4
    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. – avl_sweden Jan 08 '14 at 13:06