[英]Is it OK to have a namespace name which exists at two points in the tree?
Is this OK: 这个可以吗:
namespace Simple.OData
{
// Common OData functionality
}
namespace Simple.Data.OData
{
// The Simple.Data adapter for OData
}
It feels like it might be wrong, but I'm not sure. 感觉可能是错的,但我不确定。
It's certainly valid - consider System.Xml.Linq
and System.Linq
. 它当然有效 - 考虑
System.Xml.Linq
和System.Linq
。 I can't immediately foresee any problems... but that's not to say it's necessarily a good idea. 我无法立即预见到任何问题......但这并不是说它一定是个好主意。
Personally I prefer Simple.Data.OData
over Simple.OData.Data
, as I suspect this is primarily aimed at people who are using Simple.Data
, but happen to be using OData - not people who are focused on OData
. 我个人更喜欢
Simple.Data.OData
超过Simple.OData.Data
,因为我怀疑这主要是针对谁使用人Simple.Data
,但碰巧使用OData的-不是谁的重点是人OData
。 Again, this is like LINQ: System.Xml.Linq
is an XML API which plays will with LINQ; 再次,这就像LINQ:
System.Xml.Linq
是一个XML API,它与LINQ一起播放; it's not a LINQ "provider" as such. 它不是LINQ“提供者”。
Basically it's the same sort of problem as "I have a converter to convert from type A to type B; do I put it near type A or type B?" 基本上它与“我有一个转换器从A型转换为B型”的问题相同;我把它放在A型或B型附近吗? - but with namespaces.
- 但是有名称空间。 My experience is that usually more head-scratching goes into thinking of the best thing to do than the problems that would be caused by taking either approach...
我的经验是,考虑采取任何一种方法所引起的问题,通常更需要考虑最好的事情。
More correct would be, namespace Simple.OData.Data
. namespace Simple.OData.Data
更正确。
This is because the Data
namespace should be grouped with the other classes relating to OData
. 这是因为
Data
名称空间应该与其他与OData
相关的类组合在一起。
If you're thinking along with lines of System.Data
, System.Data.SqlClient
then it's pretty much because they are a part of the System.Data.dll
assembly, and are an integrated part of it. 如果您正在考虑
System.Data
, System.Data.SqlClient
行,那么它几乎是因为它们是System.Data.dll
程序集的一部分,并且是它的集成部分。 My own implementation of the IDbCommand
etc classes live in MyNamespace.SubNamespace.AdoWrapper
namespace, if that gives you some context. 我自己的
IDbCommand
等类的实现存在于MyNamespace.SubNamespace.AdoWrapper
命名空间中,如果它给你一些上下文。
In your case, Simple.Data
presumably doesn't exist or have much in it, unlike System.Data
.. 在你的情况下,
Simple.Data
可能不存在或有很多,与System.Data
不同。
Sure it is, if it's semantically correct. 当然,如果它在语义上是正确的。 Look at how many framework namespaces end in
.Design
, for example! 例如,查看有多少框架名称空间以
.Design
结尾!
Yes, it is, but there are some drawbacks. 是的,它是,但有一些缺点。
The namespace is part of the type name. 命名空间是类型名称的一部分。 That is, a type named
C
inside namespace AB
, is actualy named ABC
. 也就是说,命名空间
AB
名为C
的类型实际上命名为ABC
。
When you use the using
statement, then you are just using a kind of shortcut . 当您使用
using
语句时,您只是使用一种快捷方式 。
Drawbacks: 缺点:
I have noticed that sometimes Visual Studio may become little confused, specialy when using namespaces such as System
, and others that exist in the .Net framework... in such a way that you must enter the full name of the type. 我注意到有时Visual Studio可能会变得有点混乱,特别是在使用
System
等名称空间时,以及.Net框架中存在的其他名称空间......这样你必须输入该类型的全名。
It might be confusing to some people, but it should not cause any problems. 某些人可能会感到困惑,但不应该引起任何问题。 You'll probably need to preprend the namespace though if you type this:
如果您输入以下内容,您可能需要预先建立名称空间:
using Simple.Odata;
using Simple.Data.Odata;
Otherwise the compile won't recognise it. 否则编译将无法识别它。 There's probably a better structure possible for the thing you're trying to create, but to anwser your question : yes it is ok to do it if you want.
对于你正在尝试创建的东西,可能有一个更好的结构,但是为了回答你的问题:是的,如果你愿意,可以这样做。
The last part should be dependant on previous part(s) of the namespace. 最后一部分应该依赖于名称空间的前一部分。 Your adapter has a dependancy with Simple.OData but also with Simple.Data.
您的适配器具有Simple.OData的依赖性,但也具有Simple.Data。 But since Simple.OData is less generic I'd prefer something like:
但由于Simple.OData不太通用,我更喜欢这样的东西:
using Simple.Data
namespace Simple.OData.Adapters
{
// The Simple.Data adapter for OData
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.