How to create a better namespace structure?
I'm new to C#, but I'm having trouble creating a namespace structure
My question is, is it correct for a parent namespace to have a dependency on a child namespace?
https://github.com/dotnet/runtime/tree/main/src/libraries/System.Private.CoreLib/src/System
If I look at the files in the System namespace, they are accessing the sub-namespaces to organise the code, is this an exception because it is the same concept as the root namespace, like a main function, or is it okay to have dependencies on sub-namespaces?
GitHub
runtime/src/libraries/System.Private.CoreLib/src/System at main · d...
.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps. - dotnet/runtime
14 Replies
Namespaces are just for organization
And basically follow the file structure
So however you structure your code is fine
And stuff in whatever namespace can access stuff from whatever other namespace
There are no rules about that
If add a feature or a high-level API, I think that namespace should be dependent on the parent namespace, and the reverse is something... unnatural
Move the files around and the parent becomes the child
Or they are on the same level
So, am I correct in understanding that the sub-namespaces in donet code, except for the System namespace, do not reference the parent namespace due to internal rules within the development team?
Idk, never heard of any such rules
So I'd stick to what I said,
And stuff in whatever namespace can access stuff from whatever other namespacePerhaps with the addition of "unless the team set up some different weird rules"
I asked this question out of curiosity, as I don't know anyone else who works with special rules for namespaces, and I've been working without any rules until now.
Thank you for your answers
Have a great day!
my namespaces have never not simply matched my folder structure
I agree
To clarify, my question was about whether it's appropriate for a class in a parent namespace to access a class in a child namespace.
then the answer is yes 😛
if anything i would say do the opposite (child namespaces don't know about parent namespaces), but it doesn't matter either way
The principles are usually hard to work out. You will need specific tools like NDepend to monitor namespaces and their dependencies on each other, https://www.ndepend.com/docs/dependency-structure-matrix-dsm
NDepend
Dependency Structure Matrix
How to setup and use NDepend's Dependency Matrix to get a burd's eye view of how your code base is related alongside the dependency graph.
Honestly speaking, even .NET BCL has dependency cycles. So nothing can be perfect in the real world.
After reading responses and seeing some more code, my thoughts have been cleared up. I think should keep the relationship between namespaces flexible, but focus on which namespace to put the actual functionality in
IMO you'd generally want to build up, as in your deepest classes have few dependencies and you compose functionality together as you go up
that helps keep more of your code independent and easier to refactor
Thank you for answering question
Have a great day!