8

I have two class projects in a Visual Studio solution. Due to the nature of th project both projects reference each other because they need each other's services (think of the "I scratch your back, you scratch mine" phrase).

Visual Studio (2010) won't let me add a reference to project b from project a, because project a already references project b.

What strategies are there to resolve this circular dependency?

Thanks

GurdeepS
  • 65,107
  • 109
  • 251
  • 387

6 Answers6

8

There are two typical approaches you can use:

1) Combine both projects into a single project.

alt text

2) Find the common parts of the two projects and factor them out into a separate third project.

alt text

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
4

Refactor the independent services (those not dependent on the other projects) to a third class library and have both projects reference this third one.

On the other hand, if the two projects are so tightly coupled, then you should also considering combining them into a single project.

Babak Naffas
  • 12,395
  • 3
  • 34
  • 49
2

None other than "gotta break it". Either separate the two or combine them into a single module.

duffymo
  • 305,152
  • 44
  • 369
  • 561
1

One project can expose interfaces that the other project implements and then it no longer needs a reference to the other project.

Rick Sladkey
  • 33,988
  • 6
  • 71
  • 95
0

We had a similar situation where we need to get call dbCon class to get dbConnection string from project a to project b. We resolved it by storing it in the dbconfig table in database and passing it as a parameter to the project b.

A_Var
  • 1,056
  • 1
  • 13
  • 23
0

Probably the right solution is to break part of "project a" and "project b" into a "project c". Then A and B both depend on C, and neither depends on the other.

There's no possible way to have a real circular dependency, outside of some incremental build process where you build A.dll containing everything B.dll needs but nothing that B.dll needs, then afterward replace it with a new one that has been built using the newly-built B.dll and contains things that depend on it. In the end, such a complex strategy would certainly not be worth it.

It might also be possible to build netmodules and later combine them together into an assembly using the Assembly Linker, but again, the result would not justify the effort involved.

Mark
  • 11,257
  • 11
  • 61
  • 97