Project#1 has some interfaces and classes that project#2 references.
Now I want to use the implementation of Project#2 in Project#1 but vs.net complains about a circular dependency.
If I was to use dependancy injection in Project#1 and bind to the implementation in Project#2 (since it adheres to the interface contract), will this work or I will still get the circular dependency error message at runtime?
You probably could solve this with DI, but you shouldn’t.
If I understand correctly, you have something like this:
In other words, you’re trying to get
MyClassto referenceConcreteFoo, but you can’t because assemblyB, whichConcreteFooresides in, already depends onIFooinA.This is a design error. If you declare the interface
IFooin AssemblyA, but no concrete implementations, then any other interfaces/classes in assemblyAshould only referenceIFoo, never a concrete class that implements it.There are three ways to eliminate the circular dependency:
Make
MyClassdependent onIFooinstead ofConcreteFoo. This is probably the best option if you can do it. If the issue is that you need a physical instance ofIFoofor use inMyClassand don’t know where to get one from, then have it take anIFooin the constructor – let whoever usesMyClassfigure out whatIFooto use.Move the interfaces to their own assembly. This is still a reasonably good practice. Your design will look like this:
Move
MyClassto its own assembly. Effectively your dependency tree will look the same as in #2 above, but if assemblyAis much smaller thanBthen this would require less effort.Hope that helps.