C
C#11mo ago
옵자

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
Angius
Angius11mo ago
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
옵자
옵자OP11mo ago
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
Angius
Angius11mo ago
Move the files around and the parent becomes the child Or they are on the same level
옵자
옵자OP11mo ago
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?
Angius
Angius11mo ago
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 namespace
Perhaps with the addition of "unless the team set up some different weird rules"
옵자
옵자OP11mo ago
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!
Jimmacle
Jimmacle11mo ago
my namespaces have never not simply matched my folder structure
옵자
옵자OP11mo ago
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.
Jimmacle
Jimmacle11mo ago
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
Lex Li
Lex Li11mo ago
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.
Lex Li
Lex Li11mo ago
Honestly speaking, even .NET BCL has dependency cycles. So nothing can be perfect in the real world.
옵자
옵자OP11mo ago
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
Jimmacle
Jimmacle11mo ago
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
옵자
옵자OP11mo ago
Thank you for answering question Have a great day!
Want results from more Discord servers?
Add your server