Search code examples
c#.netvb.netnamespacesoop

What should go into a top level namespace?


What should go into the top level namespace? For example, if I have MyAPI.WebLogic, MyAPI.Compression, etc. If I put classes into the top level namespace, am I violating the principle of encapsulation?


Solution

  • Depends what the classes are.

    One guideline I try to follow is that dependencies between namespaces shouldn't follow a cycle. In other words, low-level namespaces can't access types from higher-level namespaces.

    This means that the top-level MyAPI namespace must contain either:

    • High-level code: code that's allowed to look inside MyAPI.WebLogic and MyAPI.Compression
    • Or, low-level code: code that's used by MyAPI.WebLogic and/or MyAPI.Compression

    Patrick Smacchia has written a lot on the advantages of structuring your code in this way, including on this site: Detecting dependencies between namespaces in .NET