[FXCOP.設計規則]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個類型。

規則描述:

確保你命名空間有良好的邏輯結構;如非必要,不要把類型放在一個新的命名空間裏。

命名空間應該包含一組經常被一起使用的類型。如果幾個類型的使用場景是互斥的,他們應該被放在不同的命名空間中。例如:System.Web.UI命名空間內的類型被Web程序使用,System.Windows.Forms命名空間內的類型則被Windows-based程序使用。儘管這兩個命名空間都包含控制UI的類型,但這些類型的應用場景並不一樣,因此,它們被分置於不同的命名空間中。

精心組織的命名空間結構同樣很有幫助,它增加了用戶發現類庫功能的能力,通過查詢命名空間的層次結構,類庫用戶很容易知道將實現某個功能的類型置於何處。

備註:設計時(Design-time)類型和許可相關的類型(permissions)將不符合這條策略,它們將在主命名空間內擁有獨立的命名空間。並且分別以.Design.Permissions結尾。

修復問題:

如果需要修復這個問題,可以嘗試將幾個小的命名空間合併到一個命名空間。

可否忽略:

如果一個命名空間內的類型不與其它命名空間內的類型同時使用,可以忽略該規則引發的警告。如果類庫內總共不到5個類型,也可以忽略這條規則。

 

發佈了32 篇原創文章 · 獲贊 3 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章