views:

1466

answers:

7

I'm working on an MVVM project, so I have folders in my project like Models, ViewModels, Windows, etc. Whenever I create a new class, Visual Studio automatically adds the folder name to the namespace designation instead of just keeping the project-level namespace. So, adding a new class to the ViewModels folder would result in the namespace, MyProject.ViewModels instead of just MyProject.

When I first encountered this, it annoyed me. My class names are pretty clear, sometimes even containing the name of the folder in them (e.g., ContactViewModel). I quickly found myself manually removing the folder name on the namespaces. I even tried at one point to create a custom class template (see this question), but I couldn't get that to work, so continued doing it manually.

I've begun to wonder, though, if this convention exists for a good reason that I'm just not seeing. I could see it being useful if you for some reason had lots of sets of identical class names organized into folders, but that doesn't seem like a particularly common scenario.

Questions:

  • Why is it common convention for namespace names to reflect folder structure?
  • Do you abide by this convention? Why?

Thanks.

+16  A: 

Same as you - I fought this for the longest time. Then I started considering why I created folders. I found myself starting to create folders to represent namespaces and packages instead of...well...whatever I would have normally done.

For instance, for a web services project, I started creating folders called 'DataTypes' and another for services or utility based classes. For MVP projects, you can nicely separate objects into Model, View, and Presenter folders/namespaces. Suddenly, the project felt a little more organized.

In my opinion, it is up to how you use folders. When I moved on to the namespace convention, I felt myself making better buckets for my data types.

j0rd4n
+1  A: 

I also feel the pain with this 'by default' behaviour in Visual Studio.

Visual Studio also tries to set a namespace/directory match when you put your LinqToSql .dbml files in their own directory. Whenever I edit the .dbml, I have to remember to:

  • open the .dbml.designer.cs file
  • remove the directory/folder name from the namespace declaration

There's a way to stop this behaviour, though. It involves creating a custom class template.

p.campbell
@pccampbell, the custom template didn't work for me. See the end of the 2nd paragraph of my question.
DanM
+3  A: 

One way of not following the convention is to create the file in the project root folder and then move it to the final sub-folder.

Anyhow, it is a convention I actually like. If I am splitting types into folders, then probably those types have some kind of conceptual grouping related to the folder. Therefore, it ends making some sense, their namespaces are also similar. Java takes this approach and enforces it with its package system. The biggest difference is that VS is only "suggesting" it to you, since neither the language or the CLR enforces it.

Rui Craveiro
+3  A: 

File system folders and namespaces both represent a hierarchy. I seems perfectly natural to me to match the two. I go even one step further and use a 1:1 relationship between files and classes. I even do so when I program in other languages such as C++.

Now that you question the relation between these two hierarchies, I seriously wonder what you would like to represent by the file system hierarchy.

Malte Clasen
One class per file is generally accepted best practice.
Paul Sasik
@Malte, I give an example of how I'm trying to divide up my code in the first para of my question (Model, ViewModels, Windows, etc.). But my classes typically have names like ContactWindow and ContactViewModel, so it's sort of obvious which class goes with which folder. That's why I was questioning the need for the hierarchical namespaces.
DanM
I usually avoid duplicating names, so in this case I'd probably have a file ViewModels\Contact.cs containing the class ViewModels.Contact. There's nothing wrong using short class names if the namespaces are meaningful. And, by the way, using "var" avoids most of the text clutter often associated with namespace hierarchies.
Malte Clasen
@Malte, doesn't this mean you end up fulling qualifying a lot of class names, though? Not to say this is bad, but it does seem to add some burden.
DanM
You can usually add using statements for the namespaces that are closely related, which eliminates most namespace qualifiers. Second, you can use .NET 3.5 "var" for variable assignments. Now you're left with a few function parameters and constructors. In my experience, this is a quite low number, if your code is well structured. If you often depend on a large amount of types in different namespaces, you might question your namespace layout or your class dependencies. It's a typical case for refactoring.
Malte Clasen
I generally agree about one class per file. One exception I like to use is if I have a specific collection class to contain only members of one class. I have these two in the same file, as they are very tight connected.
awe
+5  A: 

i was annoyed by this as well but working with and refactoring projects with large codebases quickly taught me otherwise. Having embraced the concept i think that it's a very good way to structure your code "physically" as well as logically. When you have a large project and the namespaces do not match up to the folders it becomes difficult to locate files quickly. It's also that much more difficult to remember where things are...

Also, if ReSharper recommends it, then it's probably a good idea. E.g. R# will complain if your class' namespace does not match its folder name.

Paul Sasik
Yes, but ReSharper also adds a "Namespace Provider" property to the property grid for a folder. If you set it to false, then the folder will no longer contribute to the namespace. This is good, for instance, for folders containing generated code.
John Saunders
Good call John. i never noticed that property.
Paul Sasik
+3  A: 

If you want some solid advice I'd recommend buying Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries which gives you all you need to know from the actual framework design team.

...the goal when naming namespaces is creating sufficient clarity for the programmer using the framework to immediately know what the content of the namespace is likely to be...

<Company>.(<Product>|<Technology>)[.<Feature>][.<Subnamespace>]

And importantly

Do not use the same name for a namespace and a type in that namespace

Fragmenting every 1/2 types into namespaces would not meet the first requirement as you would have a swamp of namespaces that would have to be qualified or used, if you followed the Visual Studio way. For example

Core - Domain - Users - Permissions - Accounts

Would you create

  • MyCompany.Core.Domain.Users
  • MyCompany.Core.Domain.Permissions
  • MyCompany.Core.Domain.Accounts

or just

  • MyCompany.Core.Domain

For Visual Studio's way it would be the former. Also if you use lowercase file/folder naming you're looking at renaming the class each time, as well as making one big namespace tangle.

Most of it is common sense and really down to how you would expect to see the namespaces organised if you were a consumer of your own API or framework.

Chris S
That book explicitly says that the file structure *should* match the namespace structure as much as possible.
Max Schmeling
As much as possible but I infer it to mean that 2 classes in a folder doesn't warrant a namespace, e.g. Helpers namespace, Extensions namespaces.
Chris S
+2  A: 

While I agree with everyone else, that a physical structure matching the logical structure is helpful, I have to say I also fight with Visual Studio's auto-naming. There are half a dozen reasons why I have to rename classes:

  • I use a root "src" folder to visually separate my code from embedded resources
  • I want different capitalization
  • I'll organize my code into subfolders for organization within a namespace
  • I like to separate interfaces from implementations and base classes
  • I feel like it

With thiose reasons, I've resigned myself to having to adjust those for every class I create. My strategy to avoid the issue is copying a file that has the namespace declaration I want, and then immediately delete the contents.

Hounshell