Reputation: 2571
I'm trying to get a handle on DI lately. If I understand everything correctly so far, the main purpose is to write loosely coupled code, to facilitate re-usability. (Also see https://stackoverflow.com/a/9503612/579740)
So far so good, but one thing that's still not entirely clear to me, is where to place the interfaces.
An example tells more then a thousand words:
Library A:
public class A
{
public A(IInterfaceB b)
{}
}
Library B:
public interface IInterfaceB
{}
public class B : IInterfaceB
{
public B (IInterfaceC c)
{}
}
Library C:
public interface IInterfaceC
{}
public class C : IInterfaceC
{
public C()
{}
}
If I place IInterfaceC in Library C, I still have a reference to Library C in Library B. So when I decide to reuse library B, I still need Library C??? Which, in my mind at least, doesn't seem to be loosely coupled...
Can someone explain to me where my thinking is going wrong?
Upvotes: 0
Views: 170
Reputation: 3380
It looks like following
class A -> IInterfaceB <- class B
so modules containing class A and class B are not coupled.
Upvotes: 1