You could probably solve this with DI, but you shouldn't.
If I understood correctly, you have something like this:
+ Assembly A + Assembly B
| |
+ - Interface IFoo + - Class ConcreteFoo: IFoo
| ^
+ - Class MyClass -> -------> ------- |
In other words, you are trying to get the
MyClass ConcreteFoo link, but you cannot, because the assembly
B in which
ConcreteFoo is located already depends on
IFoo in
AThis is a design mistake. If you declare an IFoo interface in Assembly A , but no specific implementations, then any other interfaces / classes in assembly A should only reference IFoo , never a specific class that implements it.
There are three ways to eliminate cyclic dependency:
Make MyClass dependent on IFoo instead of ConcreteFoo . This is probably the best option if you can do this. If the problem is that you need a physical instance of IFoo for use in MyClass and donβt know where to get it, then you need it to accept IFoo in the constructor - let someone use a MyClass shape outside of which IFoo .
Move the interfaces to your assembly. This is still pretty good practice. Your design will look like this:
+ Assembly App + Assembly Interfaces + Assembly Concrete
| | |
| + - Interface IFoo |
| | \ | |
+ - Class MyClass | \ ------ + - Class ConcreteFoo
| | | ^
+ ---- Member Foo -> ---------------------> ------------------- |
Move MyClass to your own assembly. Effectively, the dependency tree will look the same as in Example 2, but if assembly A much smaller than B , this will require less effort.
Hope this helps.
source share