views:

256

answers:

3

Lets say we have the following file and [folder] structure in a project with a main namespace of MyNamespace:

  • [Entities]
    • Article.cs
    • Category.cs
  • [Interfaces]
    • IReviewable.cs
    • ISearchable.cs
  • Enumerations.cs

According to ReSharper's suggestions, the namespace of the classes Article and Category should be MyNamespace.Entities, the namespace of IReviewable and ISearchable should be MyNamespace.Interfaces and the namespace for the Enumerations class should be simply MyNamespace.

This is because ReSharper's suggestions are based on the folder structure depending and its suggestions are based on where th file is located in the structure.


What do you think of the above namespaces? Do you think that it is correct to implement namespaces for classes (interfaces etc...) solely on their folder location?

Or do you think that namespace declaration shouldn't depend solely on the folder structure?

Personally, I would put all the above files under the single MyNamespace since they are all kind-of related to one another.

+6  A: 

I think ReSharper's suggestions are fine. I think it's a mistake to group classes, etc., by what they are instead of what they do.

An analogy is grouping documents in subfolders Word, Excel, etc. instead of by Project or some other functional grouping.

Jamie Ide
+3  A: 

Note that ReSharper adds a "Namespace Provider" property to each folder in your project. You can set that property to false for a folder that you want to use for organization, but to not contribute to namespaces.

John Saunders
This is a great opportunity to organize a Web site namespaces.You site structure is fixed any you can finaly elimimate a App_code.SomeFolderName Resharper namespace suggestion.
klashar
A: 

It is a good practice to organize files in meaningful folders and build up namespace from that structure. Java has been following that rule for many years and it works out to be good.

Also note that when you have a lot of sub namespaces as above, it is time to consider levelizing as suggested by Patrick (well, you know him?)

http://codebetter.com/blogs/patricksmacchia/archive/2009/02/15/re-factoring-re-structuring-and-the-cost-of-levelizing.aspx

Lex Li