views:

1004

answers:

3

I am reorganizing my source files into a single solution with a single project, due to various reasons:

This leaves me with many namespaces, which are splitted across multiple files. So far, I am using this convention: given the namespace Company.Project.A, the files are named A.f1.cs, A.f2.cs and so on, and the Company.Project.B namespace is splitted across B.f1.cs, B.f2.cs, etc.

Given the single project restriction, are there any better ways to organize multiple files in multiple namespaces?

+8  A: 

Yes - use folders.

If you create a folder within a project, new classes within that folder will automatically use the folder name as the basis for the namespace.

For instance, if you have a project with a default namespace of "Company.Project" and a folder "Foo" containing "Bar.cs" you'll end up with:

using System; // Etc

namespace Company.Project.Foo
{
    class Bar
    {
    }
}
Jon Skeet
+1  A: 

So the solution is right here. It's Folders. But it's sometimes tricky. First of all it's kind of a good idea to have one file per class. If you will pack several classes into one file - you'll have problems with finding them with time.

Second thing about folders - if you will click on a folder and choose for example "Add -> New Item", this item will be put into selected folder. But watch out! If you will move files between folders, namespaces are not updated.

It's common source of messing project. Just after a while you can end up with a project where you have neat organized folder and files, but not reflecting namespaces. So for example, if you have class MyClass in folder MyFolder make sure, your namespace for this class is something like MyApp.MyFolder and not some old rubbish.

So If you will not pack classes into one file and watch if classes namespaces reflect folder hierarchy - you're on the good road to make you project very easy to read and navigate.

tomaszs
+1  A: 

100% agree with Jon Skeet.

To gain more overview at the folder level we're creating folders breaking the namespace structure by prefixing them with an underscore.

m0rb