简体   繁体   English

不同名称空间中的相同类名

[英]Identical class names in different namespaces

I have two different namespaces, with lots of classes with the same name. 我有两个不同的命名空间,有很多具有相同名称的类。 I believe some code will make it easier to understand: 我相信一些代码会让它更容易理解:

namespace Print.Pdl.PostScript.Operators
{
    public abstract class BaseOperator : IOperator
    {
         // ...
    }
}

namespace Print.Pdl.Pcl6.Operators
{
    public abstract class BaseOperator : IOperator
    {
         // ...
    }
}

The basic implementation is the same, as PostScript and PCL have similar constructs. 基本实现是相同的,因为PostScript和PCL具有类似的结构。 So, both namespaces end up having identical names to several classes. 因此,两个名称空间最终都与几个类具有相同的名称。

I am tempted to do the following... 我很想做以下事情......

namespace Print.Pdl.PostScript.Operators
{
    public abstract class BasePsOperator : IPsOperator
    {
         // ...
    }
}

namespace Print.Pdl.Pcl6.Operators
{
    public abstract class BasePclOperator : IPclOperator
    {
         // ...
    }
}

... but, IMHO, it kind of defeats the purpose, as there is a redundancy in the identification. ...但是,恕我直言,它有点挫败了目的,因为识别中存在冗余。 Why should I prefix/change the classes names, if the namespace already creates a logical barrier? 如果命名空间已经创建了逻辑障碍,为什么还应该为类名添加前缀/更改?

So, what you people think? 那么,你们的想法是什么? Should I keep the identical names, as they are in different namespaces, or should I prefix/change the classes names to make it easier to identify the source, and avoid conflicts if someone wants to use both namespaces together? 我应该保留相同的名称,因为它们位于不同的名称空间中,或者我应该使用前缀/更改类名称以便更容易识别源,并避免在有人想要同时使用两个名称空间时发生冲突?

Thanks! 谢谢!

Nolde, 诺尔德,

I don't think you should sacrifice the architecture in favor of readability. 我不认为你应该牺牲架构来支持可读性。 I believe its more intuitive if you keep the same class names as this makes it simpler if you are switching from PCL to PostScript and vice-versa. 我相信如果你保持相同的类名,它会更直观,如果你从PCL切换到PostScript,反之亦然。

If you have to use both classes in the same code file, create an alias for the namespace. 如果必须在同一代码文件中使用这两个类,请为命名空间创建别名。 It will be very clear to read: 阅读非常清楚:

using Pcl = Print.Pdl.Pcl6.Operators;
using PostScript = Print.Pdl.PostScript.Operators;
...
// use PCL
Pcl.BaseOperator.DoSomething();
// Use PostScript
PostScript.BaseOperator.DoSomething();

Thanks, Luciano Bargmann 谢谢Luciano Bargmann

It's a tricky question IMO. 这是一个棘手的问题IMO。 To answer it well, you need to know how often people are likely to use both namespaces at once, and how annoyed they get at having to prefix everything with a namespace. 为了更好地回答这个问题,你需要知道人们一次可能同时使用这两个名称空间的频率,以及他们不得不使用命名空间为所有内容添加前缀。

I personally tend to lean towards the "different names". 我个人倾向于倾向于“不同的名字”。 I think of the namespaces as a mechanism to limit the set of names visible in the code, and a safeguard against the unlikely event that names clash even within this reduced set. 我认为命名空间是一种限制代码中可见名称集的机制,并且防止名称在这个简化集中发生冲突的不太可能发生的事件。 So keeping the clash "unlikely" is important. 所以保持冲突“不太可能”很重要。 Therefore, I personally wouldn't design with a clash on purpose. 因此,我个人不会故意设计冲突。

Especially since in your case the difference is so small: BaseOperator is only a tad shorter than BasePsOperator . 特别是因为在你的情况下,差异是如此之小: BaseOperator只比BasePsOperator短一点。

如果两个提议的基类之间没有功能或接口差异,那么可能会创建另一个带有BaseOperator的通用命名空间 - 并且只需定义一次。

My opinion is you should use a naming scheme reflecting specialization. 我的意见是你应该使用反映专业化的命名方案。

I mean you don't need to think about if it's a prefix, suffix or any other. 我的意思是你不需要考虑它是前缀,后缀还是其他任何东西。 Just write names that may clearly identify classes. 只需编写可明确标识类的名称。

Honestly I believe namespacing wouldn't repleace a right class naming scheme, because namespaces are an organizational thing while classes are part of what your program does. 老实说,我相信命名空间不会满足正确的类命名方案,因为命名空间是一个组织的东西,而类是你的程序的一部分。

So, at the end of the day, I'd choice your second option: specialized naming for namespaces and classes. 因此,在一天结束时,我会选择第二个选项:命名空间和类的专门命名。

Different namespaces are just that... Different , name , spaces so you shouldn't worry at all (generally) about duplicate names across namespace boundaries . 不同的命名空间就是...... 不同的名称空格, 所以你不应该(通常)担心跨命名空间边界的重复名称

However, you should be conscious of usage. 但是,你应该意识到使用。 eg you are creating a layered app with many different layers (in different namespaces), you would not want to duplicate class names that might be shared across layers. 例如,您正在创建具有许多不同层(在不同命名空间中)的分层应用程序,您不希望复制可能跨层共享的类名。

If you feel it is sensible to use duplicate naming between namespaces, you can always use the handy using statements for renaming in consuming classes eg 如果您觉得在命名空间之间使用重复命名是明智的,那么您总是可以使用方便的using语句在消费类中重命名,例如

you have two User classes : 你有两个用户类:

Data.Entities.User;
App.Core.User;

And want to use them in the same place... 并希望在同一个地方使用它们......

You can use a simple using statement such as 您可以使用简单的using语句,例如

using HttpUser = App.Core.User; 

To alias one of them and so avoiding full qualification, and avoiding any confusion. 别名其中一个,因此避免完全限定,并避免任何混淆。

Developers maintaining the code after you will appreciate it if you try and avoid duplicating class names. 如果您尝试避免重复类名,那么维护代码的开发人员将会很感激。 It just makes it hard to tell at a glance what class is being used. 它只是让人难以一目了然地说出正在使用哪个类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM