Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# single project organization

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

  • a paranoic configured antivirus software;
  • Advices on partitioning code through .NET assemblies
  • Control component dependencies to gain clean architecture
  • Benefit from the C# and VB.NET compilers perf

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?

like image 360
alexandrul Avatar asked Dec 09 '08 11:12

alexandrul


2 Answers

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
    {
    }
}
like image 169
Jon Skeet Avatar answered Sep 25 '22 00:09

Jon Skeet


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.

like image 29
Tom Smykowski Avatar answered Sep 22 '22 00:09

Tom Smykowski