简体   繁体   English

命名空间命名约定

[英]namespace naming conventions

For those of you out there writing reusable components, what do you consider to be best practice if you're extending the functionality of the .NET framework? 对于那些编写可重用组件的人,如果扩展.NET框架的功能,您认为最佳实践是什么?

For example, I'm creating a Pop3 library at the moment as one doesn't exist in .NET. 例如,我正在创建一个Pop3库,因为.NET中不存在。 Do I create a custom namespace or do I use System.Net.Mail ? 我是否创建自定义命名空间或使用System.Net.Mail

From the Namespace Naming Guidelines : 命名空间命名指南

The general rule for naming namespaces is to use the company name followed by the technology name and optionally the feature and design as follows. 命名命名空间的一般规则是使用公司名称,后跟技术名称,以及可选的功能和设计,如下所示。 Copy Code 复制代码

CompanyName.TechnologyName[.Feature][.Design]

Generally it's a really bad practice to start including things into the default namespace of a framework or library. 通常,将内容包含在框架或库的默认命名空间中是一种非常糟糕的做法。 This can cause confusion in terms of whether a new namespace is part of the existing library that is part of a framework that is distributed to everyone, or is part of a custom framework that was added by someone else. 这可能会导致混淆,即新命名空间是否是现有库的一部分,该库是分发给每个人的框架的一部分,或者是由其他人添加的自定义框架的一部分。

Also, the naming convention tries to avoid namespace collisions by having unique identifiers such as CompanyName . 此外,命名约定通过使用诸如CompanyName唯一标识符来尝试避免命名空间冲突。 It also reduces any confusion and issues in terms of the source of the new library. 它还减少了新库源方面的任何混淆和问题。

This is not only a Microsoft thing, but in the Java as well. 这不仅是微软的东西,也是Java中的东西。 Namespaces in Java, called "packages" has the following convention : Java中的命名空间,称为“包”,具有以下约定

The prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981. 唯一包名称的前缀始终以全小写ASCII字母书写,应该是顶级域名之一,目前是com,edu,gov,mil,net,org,或者是英文双字母代码之一识别ISO标准3166,1981中规定的国家。

Subsequent components of the package name vary according to an organization's own internal naming conventions. 软件包名称的后续组件根据组织自己的内部命名约定而有所不同。 Such conventions might specify that certain directory name components be division, department, project, machine, or login names. 此类约定可能指定某些目录名称组件是分区,部门,项目,计算机或登录名。

So, if I had a super awesome piece of software, it may be in the net.coobird.superawesomesoftware package. 所以,如果我有一个超级棒的软件,它可能在net.coobird.superawesomesoftware包中。

And using package names that contain the default java. 并使用包含默认java.包名称java. , javax. javax. , com.sun. com.sun. packages are a big no-no. 包裹是一个很大的禁忌。

Also have a look at following MSDN article for guidelines about naming Namespaces 另请参阅以下MSDN文章,了解有关命名命名空间的指导

Names of Namespaces 命名空间的名称

The name chosen for a namespace should indicate the functionality made available by types in the namespace. 为命名空间选择的名称应指示命名空间中的类型可用的功能。 For example, the System.Net.Sockets namespace contains types that enable developers to use sockets to communicate over networks. 例如,System.Net.Sockets命名空间包含允许开发人员使用套接字通过网络进行通信的类型。

The general format for a namespace name is as follows: 命名空间名称的一般格式如下:

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

For example, Microsoft.WindowsMobile.DirectX . 例如, Microsoft.WindowsMobile.DirectX

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

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