GurdeepS
GurdeepS

Reputation: 67213

Resolving circular depenency

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

Upvotes: 8

Views: 693

Answers (6)

Mark Byers
Mark Byers

Reputation: 838076

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

Upvotes: 8

Mark
Mark

Reputation: 11740

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.

Upvotes: 0

A_Var
A_Var

Reputation: 1036

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.

Upvotes: 0

Rick Sladkey
Rick Sladkey

Reputation: 34240

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

Upvotes: 1

Babak Naffas
Babak Naffas

Reputation: 12561

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.

Upvotes: 4

duffymo
duffymo

Reputation: 308743

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

Upvotes: 2

Related Questions