Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organizing classes into namespaces

Are there some principles of organizing classes into namespaces?

For example is it OK if classes from namespace N depends on classes from N.X? And if classes from N.X depends on classes from N?

like image 643
xoposhiy Avatar asked Mar 01 '23 07:03

xoposhiy


2 Answers

In general, that should be fine for your example, if your packages were "N.UI" and "N.Util". I've seen namespaces used in two general fashions:

1) All tiers of a system have a namespace (i.e. database, web, biz, etc.)
2) Each component has a namespace (i.e. Customer, Invoice) and tiered namespaced underneath

Either way, the sub namespaces would be inter-related packages within a larger namespace, so it would be perfectly fine for you UI code to depend on your domain objects.

However, while it would be fine for N.X classes to depend on classes from N, I don't think it would make much sense for classes from N to depend on classes from N.X - it sounds like you could use some reorganization in that case.

like image 90
cynicalman Avatar answered Mar 05 '23 17:03

cynicalman


Classes in N.X can rely on classes in N. But classes in N shouldn't rely on classes in N.X; that's bad design.

Some namespace guidelines:

  • Namespace Naming Guidelines
  • Names of Namespaces
like image 34
Carra Avatar answered Mar 05 '23 15:03

Carra