命名包装类的经验法则
我发现自己创建了大量的包装类,纯粹是因为我想模拟那些
- 不太适合 RhinoMocks 隔离模型的类的行为(例如
DirectoryInfo
或WindowsIdentity
) - 本机 Win API 方法(我通常将所需的所有方法收集到一个类中,并将本机调用包装为类方法),
然后我发现自己附加了用“W”包装的类(以表明它是一个包装器),因此我最终得到了 DirectoryInfoW
(与看起来相当冗长的 DirectoryInfoWrapper
相反)。 同样,我最终得到了名为 NativeMethods.DuplicateTokenW
的包装本机方法。
命名包装类时应该遵循什么好的经验法则?
I find myself creating a significant number of wrapper classes, purely because I want to mock out the behaviour of
- Classes that don't lend themselves well to the RhinoMocks isolation model (for instance like
DirectoryInfo
orWindowsIdentity
) - Native Win API methods (I normally collect all the methods I need into a single class and wrap the native calls as a class method)
I then find myself appending the class that is wrapped with a 'W' (to indicate that it's a wrapper) and so I end up with DirectoryInfoW
(as opposed to DirectoryInfoWrapper
which seems rather verbose). Similarly, I end up with wrapped native methods called NativeMethods.DuplicateTokenW
.
What would be a good rule of thumb to follow when naming wrapper classes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用 C++,则可以使用命名空间,然后重复使用相同的类名。 例如:
If you are using C++, you can use namespaces and then just re-use the same class name. For example:
顺便说一句,我发现了一种更美观(对我来说)的包装本机方法调用的方法:
即通过将本机方法调用封装在私有嵌套类中来避免名称冲突。
虽然对于包装类命名问题没有“好的”解决方案,但我可能会采纳 aberrant80 的建议并显式调用我的包装器包装器。
Just as a side note, I found a more aesthetically pleasing (well, to me) way of wrapping native method calls:
i.e. avoid name collision by encapsulating native method calls in a private nested class.
No 'good' solution to the wrapper class naming issue though, I'd probably go with aberrant80's suggestion and explicitly call my wrappers wrappers.
我同意 aberrant80 ,如果每个人都同意您正在使用的约定,那么它就会起作用。
我个人更喜欢使用更短且能够描述课程目的的名称。 至少在界面层面上是这样。 如果您使用模拟框架,那么 IDirectory 或 IDirectoryInfo 将是一组不错的名称,而 DirectoryInfoW 或 DirectoryInfoWrapper 将是接口实现者。
一个更好的例子可能是包装 HttpRequest; 定义一个 IRequest 来声明“这对我的应用程序很重要”,然后 Request、HttpRequestWrapper、Request 等将是实现者。
因此,总而言之,尝试使用描述性的、不过分冗长的接口名称。
I'll agree with aberrant80 , if everyone agrees with the convention you are using, then it'll work.
I personally prefer using names that are shorter and descriptive to the class's purpose. At least at the interface level. If you're using a mock framework, then IDirectory or IDirectoryInfo would be a decent set of names, while DirectoryInfoW or DirectoryInfoWrapper would be an interface implementer.
A better example might be wrapping an HttpRequest; define an IRequest to state 'this is what is important to my application', then Request, HttpRequestWrapper, Request, etc would be implementers.
So, to summarize, try and use descriptive, non-overly-verbose interface names.
命名约定是任何适合您正在合作的团队的命名约定。 只要每个人都同意特定的惯例,那就没问题。
不过,我倾向于更详细的版本,即
DirectoryInfoWrapper
,而不是使用一个无法向不熟悉代码的人解释任何内容的字母。 但这只是我。Naming conventions are whatever works for the team that you're working with. As long as everyone's ok with a particular convention, then it's ok.
I tend to prefer the more verbose version though, i.e.
DirectoryInfoWrapper
, rather than having a single letter that doesn't explain anything to anyone who's not familiar with the code. But that's just me.