在 C# 中的不同命名空间中具有相同名称的类是一个好主意吗?
例如,我们有两个用于解析简历的类,一个用于解析Excel,另一个用于解析HTML。我的同事喜欢做的是将这两个类命名为相同的名称,并将它们放入不同的命名空间中,如下所示:
namespace XX.ResumeParsers.Excel
class ResumeParser{}
namespace XX.ResumeParsers.Html
class ResumeParser{}
我觉得这不是一个好主意,我更愿意重命名这些类,并将它们放入一个命名空间中(但如果需要,可以放在不同的文件中):
//in Excel folder under ResumeParsers folder
namespace XX.ResumeParsers
class ExcelResumeParser{}
//in Html folder under ResumeParsers folder
namespace XX.ResumeParsers
class HtmlResumeParser{}
因此,层次结构仍然存在于文件夹中,但命名空间是相同的(与文件夹层次结构不完全匹配),可以吗?
如果我是对的,你知道如何说服我的同事吗?或者他们的解决方案有什么明显的缺点吗?
谢谢。
for example, we have two classes for parsing the resumes, one to parse Excel and the other to parse HTML.what my colleagues love to do is to name these two classes the same name and put them into different namespace,like shown as below:
namespace XX.ResumeParsers.Excel
class ResumeParser{}
namespace XX.ResumeParsers.Html
class ResumeParser{}
I feel it is not a good idea, I'll prefer to rename the classes, and put them into one namespace(but in different files if needed):
//in Excel folder under ResumeParsers folder
namespace XX.ResumeParsers
class ExcelResumeParser{}
//in Html folder under ResumeParsers folder
namespace XX.ResumeParsers
class HtmlResumeParser{}
thus, the Hierarchy still exists in the folder, but the namespace is the same(do not match the folder hierarchy exactly), is that OK?
and if I am right, any idea how to persuade my colleagues? or is there any significant drawback in their solution?
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这通常不是一个好主意,特别是如果您需要使用同一代码中的两个类。它们是否应该位于不同的命名空间中是一个单独的决定,但我肯定会称它们为
HtmlResumeParser
和ExcelResumeParser
,而不是尝试使命名空间通信上下文。这将使您在阅读代码时更容易准确地确定您在谈论的内容。It's not usually a good idea, no - particularly if you need to use both classes from the same code. Whether they should be in different namespaces or not is a separate decision, but I'd definitely call them
HtmlResumeParser
andExcelResumeParser
instead of trying to make the namespace communicate context. It will make it much easier to determine exactly what you're talking about when reading code.我可以从经验中看出——在涉及类似示例的大型代码库上工作过——第二个选项在可读性方面要好得多。就我而言,我很想联系选择相反做法的程序员:)。
一般来说,对于编写代码的人来说,总是很清楚正在使用哪个类以及为什么 - 但想想阅读它的人 - 他们能够一眼看出你的代码中使用了哪个解析器吗?
还有一种情况,当你需要在同一个方法中使用两个类时 - Jon 已经提到过 - 然后你会遇到冲突,并且需要使用完整的命名空间 - 这是一个痛苦。即使您知道自己不这样做,未来其他人也可能需要这样做。并且可能也想联系您:)。
I can tell from experience - worked on a large codebase involving a similar example - that the second option is much better in terms of readability. In my case, I would love to get a hold of the programmer that chose to do it the other way around :).
In general, for people writing the code, it's always clear which class is being used and why - but think about people reading it - will they be able to tell at a glance which parser is used in your code?
There's also the case when you need both classes in the same method - Jon already mentioned it - you'll get conflicts then and will need to use the full namespace - which is a pain. And even if you know you don't - somebody else in the future might need to. And will probably want to get a hold of you, too :).
这里没有绝对的正确或错误,但将相似的类放在同一个命名空间中似乎是一个好主意。
您可以将
StreamReader
和StringReader
视为框架中的类似示例。它们都实现相同的接口 (TextReader
),并且都位于System.IO
命名空间中,尽管StringReader
类不执行任何操作从内存中的字符串读取时的实际 I/O。无论是否将类放在同一个命名空间中,都应该尽量使类名唯一。如果您需要在同一个文件中使用这两个类,那么必须始终指定完整的名称空间会很麻烦。
There isn't any absolute right or wrong here, but putting similar classes in the same namespace seems like a good idea.
You can look at the
StreamReader
andStringReader
as a similar example in the framework. They both imlplement the same interface (TextReader
), and are both in theSystem.IO
namespace, eventhough theStringReader
class isn't doing any actual I/O as it reads from a string in memory.Regardless of whether you put the classes in the same namespace or not, you should try to make the class names unique. If you ever need both classes in the same file, it's a hassle to have to specify the full namespace all the time.
我想说,如果任何给定的代码将使用其中之一而不是两者,并且如果两者在单独的程序集中实现,那么这可能是可以的。如果它们位于同一个程序集中,或者一个类想要同时使用两者,那么我更喜欢不同的类名。
I'd say it's probably OK if any given code will use either one or the other but not both, and if the two are implemented in separate assemblies. If they're in the same assembly, or if one class will want to use both, then I'd prefer distinct class names.
如果解析器有一个基本接口和一些特定的帮助器类,但仅在基本接口上使用,则第一个解决方案(不同的命名空间)是最好的。因为:
程序集
来自其他
If parsers have one base interface and some specific helper classes, but used only over base interface, first solution (different namespaces) is best. Because:
assemblies
from other