views:

2903

answers:

5

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

(see http://stackoverflow.com/questions/4664/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

A: 

"First off, let’s agree that namespace should match folder structure and that each language artifact [sic] should be in its own file."

Funny, that's how packages work in Java. I always thought that breaking the link between namespaces and directory structure was considered one of the improvements that C# introduced. Is that not true? (Forgive my ignorance, I'm a Java person.)

duffymo
You're correct, there is no direct link between the folders, file names, namespaces and type names. It's a convention however, so that it's easier to find files, but you're free to mix and match as you want.
Lasse V. Karlsen
+2  A: 

I have always done the nested hierarchy. It makes it painfully obvious where to add new projects to an existing solution and keeps your namespaces nicely organized on disk. It also makes the deliniation of namespace versus project more obvious.

Robert C. Barth
+2  A: 

I'm not a big fan of nested projects, since it buries projects deep inside a structure and if you need to reuse that project in another application then what do you do, copy/paste the code? I like to follow somewhat of a flat structure but organized by namespace.

This is how I do it:

- DataHelpers\
---Factory\
---DataAccess\
---...
- Components\
--- EmailProcessor\
--- ErrorLogger\
- Desktop\
--- WindowsApp1\
- Services\
--- WindowsService1\
--- WindowsService2\
- WebApps\
--- WebApp1\
--- WebApp2\

Now, inside each major application I have, for example:

- WindowsService1\
--- WindowsService1\ (this contains all *.cs, bin, obj, etc and .csproj file)
--- Solution\ (this contains the .sln file where you link to other projects like ErrorLogger, etc)

I hope that makes sense!

Ricardo Villamil
In the nested model, the entire project exists under the "dotted" name folder (e.g. A.B.C. in the example), so you can just reference that directly.
Robert C. Barth
+2  A: 

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

A: 

I think you need to define a cut-off point where the flat approach no longer works. For example, if you have a typical 3-tier architecture in a smallish project, having 3 projects on the outer level is no problem. However, if you have a few dozen, then some sort of grouping helps segment functionality and makes the whole solution more understandable.

Dmitri Nesteruk
Yeah, but isn't it more practical to just start a new (sub)solution then, instead of having dozens of projects in one? Anyway, in the end it's all about personal preference..