.NET 在命名空间之间共享 类,如何分配它们?
.NET shared classes between namespaces, how to distribute them?
前段时间我从 System.Net.Sockets
命名空间内的这个 book that namespaces inside libraries should be distributed in a way that inner namespaces have more concrete classes than their parent namespaces. We can find an example of this inside the namespace System.Net
, where we can find the class WebClient. This class has a higher abstraction level than Socket 读到(实际上,WebClient
使用 Socket
)。
我的问题是,父命名空间和后代命名空间使用的所有 类 怎么样?我应该把它们放在哪个命名空间中?我是否应该创建一个新的不同的名称,如 Parent.Shared
并从 Parent
、Parent.Child
和 Parent.Child.GrandChild
导入它们?
通常您在父命名空间中有通用 类,在子命名空间中有相关的 类。例如,在 System.Text 中,您没有像 Encoding
或 StringBuilder
这样非常相关的 类。在 System.Text.RegularExpressions
中,所有 类 都与正则表达式有关。
其他近似可以是:
您在父命名空间中定义一组接口,并在每个子命名空间中定义具体实现。例如
DataLayer.Repository
可以定义接口来实现存储库模式。然后你可以用 DataLayer.Repository.EntityFramework
和 DataLayer.Repository.NHibernate
来实现与不同 ORM 的接口
前段时间我从 System.Net.Sockets
命名空间内的这个 book that namespaces inside libraries should be distributed in a way that inner namespaces have more concrete classes than their parent namespaces. We can find an example of this inside the namespace System.Net
, where we can find the class WebClient. This class has a higher abstraction level than Socket 读到(实际上,WebClient
使用 Socket
)。
我的问题是,父命名空间和后代命名空间使用的所有 类 怎么样?我应该把它们放在哪个命名空间中?我是否应该创建一个新的不同的名称,如 Parent.Shared
并从 Parent
、Parent.Child
和 Parent.Child.GrandChild
导入它们?
通常您在父命名空间中有通用 类,在子命名空间中有相关的 类。例如,在 System.Text 中,您没有像 Encoding
或 StringBuilder
这样非常相关的 类。在 System.Text.RegularExpressions
中,所有 类 都与正则表达式有关。
其他近似可以是:
您在父命名空间中定义一组接口,并在每个子命名空间中定义具体实现。例如
DataLayer.Repository
可以定义接口来实现存储库模式。然后你可以用 DataLayer.Repository.EntityFramework
和 DataLayer.Repository.NHibernate
来实现与不同 ORM 的接口