简体   繁体   English

是否可以在树中的两个点存在名称空间名称?

[英]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.LinqSystem.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.DataSystem.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.

相关问题 名称在名称空间中不存在 - The name doesn't exists in namespace 如何使一个路由指向两个不同的控制器端点,该端点在WEB Api 2中接受不同的参数 - How to have a Route which points to two different controller end points which accepts different arguments in WEB Api 2 类型或命名空间名称不存在? (静态类) - The type or namespace name does not exists? (static class) 命名空间和类具有相同的名称 - Namespace and class have the same name 我有Microsoft程序集名称。 如何找出此程序集所在的Dll名称 - I have Microsoft assembly name. How to find out the Dll name in which this assembly exists 获取“找不到类型或命名空间名称”但一切似乎都还好? - Getting "type or namespace name could not be found" but everything seems ok? 两个包在同一个命名空间内具有相同的类名。 如何具体使用每个类? - Two packages have the same class name inside the same namespace. How to specifically use each class? 编译器抱怨缺少类的名称空间 - Compiler complaining on a missing namespace which is the name of the class 当类和命名空间同名时区分类和命名空间 - Differentiating class and namespace when class and namespace have the same name 类型或名称空间名称不存在。 但仅在构建服务器上 - The type or namespace name does not exists. But only on the build server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM