Is this a good namespace name rating according to the directory name

I have a directory structure for storing source files. Is it good practice to name the namespace according to the directory structure?

how

Models\model.cs Data\data.cs 

One is defined in the Model namespace. One of them is defined in the Data namespace p>

+4
source share
6 answers

Yes, this is a typical approach, and one that is supported by tools like ReSharper.

The difference between this and the Java approach is that you do not add directories to the very top level - just from the default namespace for the project. For example, suppose we create Foo.Bar.Baz.Model and Foo.Bar.Baz.Data, C # and Java solutions can be:

WITH#

 Foo.Bar.Baz Foo.Bar.Baz.csproj defining a project with default namespace of Foo.Bar.Baz Model\ SomeModel.cs Data\ SomeData.cs 

Java:

 src\ foo\ bar\ baz\ model\ SomeModel.java data\ SomeData.java 
+6
source

yes is common practice, but you also put the project name in front of the directory name so that you have: myclasslibraryname.Models.Model and myclasslibraryname.Data.Data p>

0
source

Yes. This is common practice in Java (at least the source code that I reviewed for large projects was almost always structured this way). It is not so common in C # from what I saw, but nothing prevents you from doing this, and this will help you find the code faster.

You might need a deeper namespace hierarchy than just one level. Usually it is preceded by the name of your organization or group, the name of the project, the name of the library / program, and then the names of architectural architectures (for example, Model, View, Data, etc.). No matter what the meaning makes sense for any source in which the source code of your project will live.

0
source

In general, I think this is a good practice. When you do this this way, looking at the code, you can generally link or easily find and find out where your code comes from.

It is also good practice to maintain code. A new user appears, he can just see the namespace and determine where the code files are located or need to look for them.

0
source

I do not know if this is really good or not. But I call it that. I defined categories for different modules.

Like this: Company.Common Company.Common.Web

Company.Windows Company.Windows.Services

General represent a directory. Inside, I created a solution with VS2010. Inside the solution, I create a project for each part and for this subdirectories for the project, and if the project is complex, there are more subdirectories for existing classes inside the dll.

There I have a good overview in all views (dir - view and project view - code view).

0
source

This is a convenient agreement for many projects, and some of them support or expect.

However, this is not a complete story. Although this is a good default, I don’t think it should be considered untouchable best practice, because there are some circumstances that can motivate things to do differently. Additional factors to consider include:

  • The unnecessary propagation of a namespace and the deeply nested hierarchy namespace can be a pain for users of your types. In a large library, you may want to start organizing source code files in a folder before you feel the need to impose multiple namespaces on clients.
  • In this regard, the hierarchy namespace in .NET should work so that dependencies between types pass from the child namespace to the parent, and not vice versa. This is not always a natural way to organize source code into folders / directories. For example, one is often seen by people creating namespaces such as MyNamespace.Foo.Common which are used both by type in MyNamespace.Foo.Bar1 and in MyNamespace.Foo.Bar2. This seems reasonable for them at the source level of organization of the code, but it violates the dependence of the namespace conventions.
  • Sometimes you can provide additional functionality by adding some types to the library namespace distributing an additional assembly instead of completely freeing up a new version of the full assembly library. It will probably be more convenient to store the source code files for the corresponding assemblies separated from each other in the repository, rather than storing them together to save all types for the namespace in the same folder.

In short, I would say, following normal practice, if you have no good reason for this. But don't let this hold you back if you have a good reason to use the fact that namespaces can provide groupings of types that are completely orthogonal to their grouping into deployable assemblies and the source code that creates them.

0
source

Source: https://habr.com/ru/post/1340281/


All Articles