Search code examples
c#code-organizationnamespaces

Organizing namespaces and classes in directories


When I put a class file inside a folder, Resharper suggest me to update the namespace. So if I have the file Classes/Game.cs (Class), it will suggest the namespace MyApplication.Classes.

The problem is that I have a subfolder called Game too, that have components for the Game class. And it is confliting with namespaces. For instance:

/Classes/Game.cs                   MyApplication.Classes.{Game}
/Classes/Game/Version.cs           MyApplication.Classes.Game.{Version}

What is the best method to turn around that?

Currently I'm organizing like that:

/Classes/Game/Game.cs              MyApplication.Classes.Game.{Class}
/Classes/Game/Player.cs            MyApplication.Classes.Game.{Player}
/Classes/Game/Version/Version.cs   MyApplication.Classes.Game.Version.{Class}
/Classes/Game/Version/History.cs   MyApplication.Classes.Game.Version.{History}

So note that I rename the "main" class to Class to avoid conflict, but I keep a more accurate name in the filename. But it is ugly, not?


Solution

  • First of all, a folder named "Classes" doesn't seem particularly helpful - everything is a class after all and this doesn't seem to give you any useful information.

    If you wish to keep this naming scheme, then there are two options. The first is to disable Resharper's recommendations - or at least tell R# to treat them as Hints, rather than Warnings.

    The second is to give your folder a name that isn't a valid namespace declaration. This will make R# stop suggesting it as a valid namespace. The format I've most often seen and used is to use square brackets around the name. So you have a folder hierarchy like this:

    /Classes/Game.cs
    /Classes/[Game]/Version.cs
    

    etc. In this case, R# will suggest "MyApplication.Classes" as the namespace for both the Game and Version classes.