5. 避免命名空间中包含太少的类型原文引用: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个类型。
描述:确保你的每一个命名空间对应到一个逻辑结构;除非必要,否则不要把你的类型放在一个新的命名空间里。
一个命名空间应该包含一组经常被使用在同一个场景中的类型。当一组类型的使用场景是互斥的,他们应该被放在不同的命名空间中。例如:Web应用程序使用System.Web.UI命名空间中的类型,而Windows-based应用程序使用System.Windows.Forms命名空间中的类型。即使两个命名空间都包含UI控制方面的类型,这些类型不被设计为在同一个应用程序中使用,因此,他们仍然被放在不同的命名空间中。细致的命名空间结构同样很有帮助,它增加了用户发现库功能的能力,通过调查命名空间,库用户将能够定位一个实现的功能。
备注:设计时类型和许可相关的类型将不符合这条策略,它们将在主命名空间下面拥有它门自己的命名空间。并且分别以”.Design”和”.Permissions”结尾。
修复:如果需要修复这个问题,可以尝试将几个命名空间合并一个单独的命名空间中。
例外:如果一个命名空间不包含任何与其它命名空间中的类型同时使用的类型,可以忽略这条消息。如果你的库只包含少于5个类型,这条规则也可以忽略。