Should I stop fighting the default Visual Studio namespace namespace convention?

I am working on an MVVM project, so I have folders in my project such as Models, ViewModels, Windows, etc. Whenever I create a new class, Visual Studio automatically adds the folder name to the namespace notation, and not just saves the namespace at the project level. Thus, adding a new class to the ViewModels folder will result in the MyProject.ViewModels namespace instead of just MyProject .

When I first encountered this, it made me angry. My class names are pretty clear, sometimes even contain the folder name in them (e.g. ContactViewModel ). I quickly discovered that I manually deleted the folder name in namespaces. I even tried at some point to create a custom class template (see this question ), but I could not get it to work, so I continued to do this manually.

I began to wonder, although this agreement exists for a good reason that I simply don’t see. I saw that this is useful if for some reason you had many sets of the same class names organized in folders, but this does not seem like a particularly common scenario.

Questions:

  • Why does this general convention for namespace names reflect the structure of folders?
  • Do you adhere to this convention? Why?
+63
c # namespaces visual-studio naming-conventions
Aug 17 '09 at 17:54
source share
10 answers

Same as you - I struggled with this for the longest time. Then I began to consider why I created folders. I found that I began to create folders to represent namespaces and packages instead of arbitrary buckets.

For example, in an MVVM project, it might be useful to place views and view models in a separate namespace. MVC will have a separate namespace for models, controllers, and views. It is also useful to group classes by their attribute.

Suddenly, the project feels more organized. Other developers find it easier to find where the functions are implemented.

If you standardize your namespace methods, all of your projects will have the same predictable structure as a big win in maintenance.

+45
Aug 17 '09 at 17:59
source share

If you want some solid advice, I would recommend buying the Framework Design Guidelines: conventions, idioms and templates for reusable .NET libraries , which gives you everything you need to know from the actual framework development team.

... the purpose of naming namespaces creates sufficient clarity for the programmer using the framework to immediately find out what the contents of the namespace will be ...

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

And what's important

Do not use the same name for namespace and type in this namespace

Fragmenting every 1/2 type into a namespace would not meet the first requirement, since you would have a swamp of namespaces that would have to be qualified or used if you followed the Visual Studio method. for example

Basic - Domain - Users - Permissions - Accounts

Do you create

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

or simply

  • MyCompany.Core.Domain

For Visual Studio, this will be the first. Also, if you use lowercase file names, you look at renaming the class every time, as well as creating one large namespace.

Most of them are common sense and really get to the point where you expect to see organized namespaces if you are a consumer of your own API or framework.

+16
Aug 17 '09 at 18:22
source share

I was also annoyed by this, but working with projects with large code bases and reorganizing projects quickly taught me differently. Having accepted the concept, I believe that this is 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 the folders, it becomes difficult to quickly find the files. It's also much harder to remember where things are ...

Also, if ReSharper recommends it, then this is probably a good idea. For example. R # will complain if the namespace of your class does not match the name of its folder.

+10
Aug 17 '09 at 18:11
source share

File system folders and namespaces are a hierarchy. It seems to me completely natural coincidence with these two. I go one step further and use a 1: 1 ratio between files and classes. I even do this when I program in other languages, such as C ++.

Now, when you ask a question about the relationship between these two hierarchies, I seriously wonder what you would like to represent in the file system hierarchy.

+5
Aug 17 '09 at 18:11
source share

One way to not follow the convention is to create a file in the root folder of the project, and then move it to the final subfolder.

Anyway, this is an agreement that I really like. If I am splitting types into folders, then probably these types have some conceptual grouping associated with this folder. Therefore, it ends up making sense; their namespaces are also similar. Java takes this approach and applies it with its package system. The biggest difference is that VS only “offers” it to you, since neither the language nor the CLR apply it.

+4
Aug 17 '09 at 18:09
source share

As long as I agree with everyone that a physical structure corresponding to a logical structure is useful, I must say that I am also struggling with the Visual Studio auto-name. There are half a dozen reasons I need to rename classes:

  • I use the root folder "src" to visually separate my code from the embedded resources.
  • I want a different capitalization
  • I organize my code in subfolders for organization in the namespace
  • I like to separate interfaces from implementations and base classes
  • I like

For all reasons, I have come to terms with the need to adjust them for each class I create. My strategy to avoid this problem is to copy the namespace declaration file I want, and then delete the contents immediately.

+4
Aug 17 '09 at 19:11
source share

Before namespaces were introduced in C ++, all C types were in the global namespace. Namespaces were created to separate types into logical containers, so it was clear which type they belonged to. This also applies to C #.

Assemblies are a deployment solution. If you look at the .Net structure, this assembly will contain several different namespaces.

The folder is intended for organizing files on the disk.

These three have nothing to do with each other, but it is often convenient that the assembly name, namespace, and folder names are the same. Note that Java collapses folders and namespaces into the same thing (limiting the freedom of developers to organize files and namespaces).

Often we choose to organize files in a project into several folders, because it is easier for me or my team to move around the files. Usually this file organization has nothing to do with the namespace design that we use. I'm sorry that the VS command will not use the namespace by default as the folder name, or at least give the option so that it is not the default.

Do not suffer, either change the template for new classes, or correct the namespace after creating a new file.

+3
Apr 13 '13 at
source share

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

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

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

There is a way to stop this behavior . This is due to the creation of a custom class template.

+2
Aug 17 '09 at 18:02
source share

I think there really are reasons for creating different structures for project name and folder spaces. If you are developing a library, the namespace structure should primarily serve the users of your API: this should be logical and easy to understand. On the other hand, the folder structure should be primarily to make your life easier, as an API designer. Some goals are really very similar, for example, the structure should be logical. But there may be others, for example. that you can quickly select related files for a snap or easily navigate. For example, I myself usually create new folders when a certain threshold of the file is reached, otherwise it takes too much time to search for the file that I am looking for. But respect for designer preferences can also mean strict adherence to the namespace - if that is their preference.

In general, in many cases it makes sense that both are the same, but I think there are valid cases for rejection.

In the past, it was useful for me to create a file (for example, WPF UserControl) in one place, to get the namespace on the right, and then move it to the "right" folder.

+2
Aug 15 '15 at 20:03
source share

Although I agree that matching the namespace hierarchy with the folder hierarchy is convenient, and a good idea, I think the fact that Visual Studio doesn't seem to support switching this feature is disgusting. There are many applications in Visual Studio, and there are many coding styles and ways to structure the source files that work great.

Say there are thousands of files that belong to the namespace, but the programmer just wants to group them into folders to make it easier to navigate the hierarchy. Is this really such a bad idea? Will it really make things so insecure that it should be prohibited by the IDE ???

Let's say I use Visual Studio to work with Unity. Now all my scripts are in the namespace "Assets.Scripts". There is not just a useless Assets namespace that now does not contain scripts, but “Assets.Scripts” is pointless - it does not describe which project or part of the project the source file belongs to. Useless.

0
Jan 07 '15 at 19:15
source share



All Articles