Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespaces and folder structures in c# solutions: how should folders on disk be organised?

First off, let’s agree that namespace should match folder structure and that each language artefact should be in its own file.

(see Should the folders in a solution match the namespace? ).

The next question is how the folders should actually be organised on disk.
Suppose I have ClassC in the A.B.C namespace and ClassD in the A.B.C.D namespace.
Let’s also assume that each namespace is built into its own assembly (project) and that namespaces have dependencies from right to left as per accepted best practice (A.B.C.D can depend on A.B.C which can depend on A.B which can depend on A). I appreciate that each namespace doesn’t have to be in a separate assembly but in the general case we will have some namespaces in separate assemblies and my example illustrates that.

I can see (at least) two ways to create the folder tree – which I’ll call “nested folders” and “flat folders”:

1 - Nested folders:

A
--A.csproj
--B
----A.B.csproj
----C
------A.B.C.csproj
------classC.cs
------D
--------A.B.C.D.csproj
--------classD.cs

OR

2 – Flat folders:

A
--A.csproj
A.B
--A.B.csproj
A.B.C
--A.B.C.csproj
--classC.cs
A.B.C.D
--A.B.C.D.csproj
--classD.cs

You will see I’ve made a few assumptions already:

  • Each project file has a fully qualified name (FQN) based on the namespace.
  • Each class file uses a non-FQN

Nested folders seems more natural (we all like hierarchies), but may be a bit harder to navigate in large solutions:

When you look at your solution in VS, it shows a flat list of projects rather than a nested view. This looks more like “flat folders” so there may be merit in organising the folders on disk to match the view in VS.

If you look in each folder on disk you will see the folder artefacts for that project plus the sub folder for the namespace: taking C as an example:

C
--bin
--D
--obj
--Properties
--A.B.C.csproj
--classC.cs

Depending on D’s real name it may not be obvious that D is a namespace folder rather than an organisational folder in the C namespace.

I know we’ve had folders and namespaces from day one in .NET (8 or 9 years ago) and Java before that, but, personally speaking, we don’t appear to have come to a consensus on best practice project organisation for large solutions. I’d be really interested to find out what you all think.

Thanks
Michael

like image 933
Micdev42 Avatar asked Dec 30 '08 17:12

Micdev42


People also ask

Is a folder a namespace?

Folders are much simpler than namespaces. They are purely for organizational purposes and do not impact object IDs or your content. You can create folders to organize objects by subject or functional area. This makes it easier for you to locate metadata, particularly in large projects.

What are the two types of namespaces?

When creating a namespace, you must choose one of two namespace types: a stand-alone namespace or a domain-based namespace. In addition, if you choose a domain-based namespace, you must choose a namespace mode: Windows 2000 Server mode or Windows Server 2008 mode.

What is the example of namespace?

In an operating system, an example of namespace is a directory. Each name in a directory uniquely identifies one file or subdirectory. As a rule, names in a namespace cannot have more than one meaning; that is, different meanings cannot share the same name in the same namespace.


1 Answers

I use the flat approach. I find a nested hierarchy too hard to maintain. I group my projects into several solutions, with maximum reusability and cross-referencing in mind, and always found this satisfactory. Example (projects are indented):

CompanyName
  CompanyName.Core
    Class1
    Struct2
    Enum3
  CompanyName.Data
  CompanyName.Web

CompanyName.Projects
  CompanyName.Projects.Core

CompanyName.Projects.ProjectX
  CompanyName.Projects.ProjectX.Core
  CompanyName.Projects.ProjectX.Website
  CompanyName.Projects.ProjectX.ToolY

etc. etc.

Edit: removed nonsense remark

like image 109
user39603 Avatar answered Oct 17 '22 04:10

user39603