在不同的命名空间相同的类名 [英] Identical class names in different namespaces
问题描述
我有两个不同的命名空间,有很多类具有相同的名称。我相信有些code会更容易理解:
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
{
// ...
}
}
的基本原理是一样的,以PostScript和PCL具有相似的构造。因此,无论是命名空间最终不得不相同的名称为几类。
The basic implementation is the same, as PostScript and PCL have similar constructs. So, both namespaces end up having identical names to several classes.
我很想做以下...
namespace Print.Pdl.PostScript.Operators
{
public abstract class BasePsOperator : IPsOperator
{
// ...
}
}
namespace Print.Pdl.Pcl6.Operators
{
public abstract class BasePclOperator : IPclOperator
{
// ...
}
}
...但是,恕我直言,这种失败的目的,因为在识别冗余。我为什么要preFIX /更改的类名称,如果命名空间已经创建了一个合乎逻辑的障碍?
... 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?
所以,你让人觉得?我应该保持相同的名字,因为他们在不同的命名空间,或者我应该preFIX /修改类的名称,以便更容易识别源,并避免冲突,如果有人想使用这两种命名空间在一起?
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?
谢谢!
推荐答案
诺尔德,
我不认为你应该牺牲的架构有利于可读性。我相信,它更直观,如果你保持相同的类名称,因为这使得它更简单,如果你是从PCL交换到PostScript和反之亦然。
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.
如果您需要在同一code文件使用这两个类,创建一个别名命名空间。这将是非常清楚的写着:
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
这篇关于在不同的命名空间相同的类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!