What should go into the 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 in a top-level namespace, am I violating the principle of encapsulation?

0


a source to share


5 answers


Depends on classes.

One recommendation I'm trying to make is that the dependencies between namespaces shouldn't follow a loop. In other words, lower-level namespaces cannot access types from higher-level namespaces.

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



  • High-level code: code to view MyAPI.WebLogic and MyAPI.Compression
  • Or low-level code: code used by MyAPI.WebLogic and / or MyAPI.Compression

Patrick Smacchia has written extensively about the benefits of structuring your code this way, including on this site: Dependencies Between Namespaces in .NET

+2


a source


Namespaces are not an OOP related concept like encapsulation. There for organization, so organize it in a way that makes sense for your application. Most of the work I do on websites has a business library and more often than not it is fueled under a single namespace.



+3


a source


Depends on what the namespace is used for. If this is an application, maybe something like bootstrap classes, etx loaders, Main, etc. I would say (like everyone else) "it depends".

0


a source


I don't think you are really breaking encapsulation as such by doing this.

Usually namespaces are just a way to organize your classes to be easier to find, so that makes sense for your application.

0


a source


You are not breaking encapsulation at all as far as I can see it. In fact, I'm not even sure if this can be called encapsulation, given that namespaces are not specific to OOP - most likely it is just an orgsynthesis of types.

The rule is to just put the type in the top level namespace if you believe it belongs. A prime example for this situation is that (using your examples) MyAPI.WebLogic and MyAPI.Compression (probably as well as others) should all use a specific type - so it's best to put that type in MyAPI. If you're still not quite sure what belongs, use the Microsoft libraries as examples. There are many classes in the namespace System

in BCL!

0


a source







All Articles