简体   繁体   English

C# 中的命名空间

[英]Namespaces in C#

I am using an ASP.NET MVC project and everytime I add a class to a folder it makes really long namespaces.我正在使用 ASP.NET MVC 项目,每次我将 class 添加到文件夹时,它都会产生非常长的命名空间。

Example :示例

Project = Tully.Saps.Data  
Folder = DataAccess/Interfaces  
Namespace = Tully.Saps.Data.DataAccess.Interfaces

Folder = DataAccess/MbNetRepositories  
Namespace = Tully.Saps.Data.DataAccess.MbNetRepositories

Question :问题
Is it best to leave the namespace alone and add the using clause to the classes that access it or change the namespace to Tully.Saps.Data for everything in this project?是否最好单独保留命名空间并将 using 子句添加到访问它的类或将命名空间更改为 Tully.Saps.Data 以用于此项目中的所有内容?

Leave them alone and add the usings.不理会它们并添加使用。 You're asking for trouble manually changing things like that (harder to debug, inconsistent with other projects, et cetera).您在手动更改类似的东西时遇到麻烦(更难调试,与其他项目不一致,等等)。

It is really up to you how you want to deal with it.你想如何处理它真的取决于你。 If you are only going to be accessing a member of a namespace once or twice, then adding the "using" statement really doesn't do much for you.如果您只打算访问一个名称空间的成员一次或两次,那么添加“using”语句对您来说并没有多大用处。

If you are going to use it multiple times then reducing the namespace chain is probably going to make things easier to read.如果您要多次使用它,那么减少命名空间链可能会使事情更容易阅读。

You could always change the namespace so it doesn't add the new folder name if you are just looking to logically group files together, without creating a new namespace.如果您只是想在逻辑上将文件分组在一起,而不创建新的命名空间,您可以随时更改命名空间,这样它就不会添加新的文件夹名称。

According to FXCop, and I agree:根据 FXCop,我同意:

Avoid namespaces with few types避免使用少数类型的命名空间

A namespace should generally have more than five types.一个命名空间通常应该有五种以上的类型。

also (and this applies to the "single namespace" suggestion -- which is almost the same to say as no namespace)也(这适用于“单一命名空间”的建议——这几乎等同于没有命名空间)

Declare types in namespaces在命名空间中声明类型

A type should be defined inside a namespace to avoid duplication.应在命名空间内定义类型以避免重复。

  • Namespaces命名空间

.Namespaces help us to define the "scope" of a set of entities in our object model or our application. .Namespaces 帮助我们在 object model 或我们的应用程序中定义一组实体的“范围”。 This makes them a software design decision not a folder structure decision.这使它们成为软件设计决策而不是文件夹结构决策。 For example, in an MVC application it would make good sense to have Model/View/Controller folders and related namespaces.例如,在 MVC 应用程序中,拥有 Model/View/Controller 文件夹和相关的命名空间是很有意义的。 So, while it is possible, in some cases, that the folder structure will match the namespace pattern we decide to use in our development, it is not required and may not be what we desire.因此,虽然在某些情况下,文件夹结构可能与我们决定在开发中使用的命名空间模式相匹配,但这不是必需的,也可能不是我们想要的。 Each namespace should be a case-by-case decision每个命名空间都应根据具体情况决定

  • using statements使用语句

To define using statements for a namespace is a seperate decision based on how often the object in that namespace will be referred to in code and should not in any way affect our namespace creation practice.为命名空间定义 using 语句是一个单独的决定,具体取决于代码中引用该命名空间中 object 的频率,并且不应以任何方式影响我们的命名空间创建实践。

Leave it.别管它。 It's one great example of how your IDE is dictating your coding style.这是您的 IDE 如何决定您的编码风格的一个很好的例子。

Just because the tool (Visual Studio) you are using has decided that each folder needs a new Namespace doesn't mean you do.仅仅因为您使用的工具 (Visual Studio) 决定每个文件夹都需要一个新的命名空间并不意味着您需要这样做。
I personally tend to leave my "Data" projects as a single Namespace.我个人倾向于将我的“数据”项目保留为单个命名空间。 If I have a subfolder called "Model" I don't want those files in the Something.Data.Model Namespace, I want them in Something.Data.如果我有一个名为“Model”的子文件夹,我不希望这些文件位于 Something.Data.Model 命名空间中,而是希望它们位于 Something.Data 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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