I was looking at some dependency graphs for my primary personal project recently, and I noticed that I had a mutual dependency between objects in nested namespaces. E.g., I had an object in MyNamespace.Foo
that implemented a generic interface in MyNamespace.Foo.Interfaces
with that object as the generic parameter in the interface.
namespace MyNamespace.Foo
{
internal class Foo : MyNamespace.Foo.Interfaces.IFoo<Foo>
{ }
}
In this case. the dependency analysis tool (VS2010 beta) reasonably considers the generic "instantiation" (for the sake of discussion, I know this isn't c++) of the interface to be a member of Interfaces namespace that then depends on its parent namespace.
After some consideration on my part, I've more-or-less reached the conclusion that my existing design, in my particular case, is a code-smell. I should merge the Interfaces
namespace into the parent Foo namespace. (It's silly to require clients to drill down an extra layer to the interfaces namespace if I want them to use Foo
via IFoo
.) Is this true in the general case, however?
How should inter-namespace dependencies be managed? Should "broader" namespaces (like MyNamespace.Foo
generally depend on "narrower" namespaces (like MyNamespace.Foo.Interfaces
), or should narrower namespaces depend on broader ones? Or is there some better, more subtle answer?