[FXCOP.设计规则]5. 避免命名空间中包含太少的类型

来源:互联网 发布:白色丝袜淘宝 编辑:程序博客网 时间:2024/04/28 08:48

原文引用:

Avoid namespaces with few types

TypeName:

AvoidNamespacesWithFewTypes

CheckId:

CA1020

Category:

Microsoft.Design

Message Level:

Warning

Certainty:

50%

Breaking Change:

Breaking


Cause: A namespace other than the global namespace contains fewer than five types.

Rule Description

Make sure that there is a logical organization to each of your namespaces, and that there is a valid reason for putting types in a sparsely populated namespace. Namespaces should contain types that are used together in most scenarios. When their applications are mutually exclusive, types should be located in separate namespaces. For example, the System.Web.UI namespace contains types that are used in Web applications, and the System.Windows.Forms namespace contains types that are used in Windows-based applications. Even though both namespaces have types that control aspects of the user interface, these types are not designed to be used in the same application, and are therefore located in separate namespaces. Careful namespace organization can also be helpful because it increases the discoverability of a feature. By examining the namespace hierarchy, library consumers should be able to locate the types that implement a feature.

Note: Design-time types and permissions should not be merged into other namespaces to comply with this guideline. These types belong in their own namespaces below your main namespace, and the namespaces should end in .Design and .Permissions, respectively.

How to Fix Violations

To fix a violation of this rule, try to combine namespaces that contain a small number of types into a single namespace.

When to Exclude Messages

It is safe to exclude a message from this rule when the namespace does not contain types that are used with the types in your other namespaces. It is also safe to exclude a violation if there are fewer than five types in the library.

 

引发原因:

一个非全局的命名空间内少于5个类型。

规则描述:

确保你命名空间有良好的逻辑结构;如非必要,不要把类型放在一个新的命名空间里。

命名空间应该包含一组经常被一起使用的类型。如果几个类型的使用场景是互斥的,他们应该被放在不同的命名空间中。例如:System.Web.UI命名空间内的类型被Web程序使用,System.Windows.Forms命名空间内的类型则被Windows-based程序使用。尽管这两个命名空间都包含控制UI的类型,但这些类型的应用场景并不一样,因此,它们被分置于不同的命名空间中。

精心组织的命名空间结构同样很有帮助,它增加了用户发现类库功能的能力,通过查询命名空间的层次结构,类库用户很容易知道将实现某个功能的类型置于何处。

备注:设计时(Design-time)类型和许可相关的类型(permissions)将不符合这条策略,它们将在主命名空间内拥有独立的命名空间。并且分别以.Design.Permissions结尾。

修复问题:

如果需要修复这个问题,可以尝试将几个小的命名空间合并到一个命名空间。

可否忽略:

如果一个命名空间内的类型不与其它命名空间内的类型同时使用,可以忽略该规则引发的警告。如果类库内总共不到5个类型,也可以忽略这条规则。

 

原创粉丝点击