简体   繁体   中英

Properly assigning Namespaces (with ReSharper's suggestions)

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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM