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)
不适合RhinoMocks隔离模型的类(例如DirectoryInfo或WindowsIdentity)
本机Win API方法(我通常将我需要的所有方法收集到一个类中,并将本机调用包装为类方法)
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
.
然后我发现自己附加了一个用'W'包装的类(表示它是一个包装器),所以我最终得到了DirectoryInfoW(而不是DirectoryInfoWrapper,它似乎相当冗长)。同样,我最终使用名为NativeMethods.DuplicateTokenW的包装本机方法。
What would be a good rule of thumb to follow when naming wrapper classes?
在命名包装类时,遵循什么是一个很好的经验法则?
4 个解决方案
#1
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.
我倾向于更喜欢更详细的版本,即DirectoryInfoWrapper,而不是只有一个字母不向任何不熟悉代码的人解释任何内容。但那只是我。
#2
I'll agree with aberrant80 , if everyone agrees with the convention you are using, then it'll work.
我同意异常80,如果每个人都同意你正在使用的惯例,那么它就会起作用。
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.
我个人更喜欢使用更短和描述类目的的名称。至少在界面层面。如果您使用的是模拟框架,那么IDirectory或IDirectoryInfo将是一组不错的名称,而DirectoryInfoW或DirectoryInfoWrapper将是一个接口实现者。
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.
一个更好的例子可能是包装HttpRequest;定义一个IRequest来声明'这是对我的应用程序很重要',然后Request,HttpRequestWrapper,Request等将是实现者。
So, to summarize, try and use descriptive, non-overly-verbose interface names.
因此,总而言之,尝试使用描述性的,非过于冗长的接口名称。
#3
Just as a side note, I found a more aesthetically pleasing (well, to me) way of wrapping native method calls:
就像旁注一样,我发现了一种更美观的(好吧,对我来说)包装本机方法调用的方式:
public class NativeMethods
{
// made virtual so that it can be mocked - I don't really want
// an interface for this class!
public virtual bool RevertToSelf()
{
return WinApi.RevertToSelf();
}
...
private static class WinApi
{
[DllImport("advapi32.dll")]
public static extern bool RevertToSelf();
...
}
}
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的建议,并明确地调用我的包装器包装器。
#4
If you are using C++, you can use namespaces and then just re-use the same class name. For example:
如果您使用的是C ++,则可以使用命名空间,然后重复使用相同的类名。例如:
namespace WrapperNamespace
{
class MyClass {...};
}
namespace InternalNamespace
{
class MyClass {...};
}
#1
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.
我倾向于更喜欢更详细的版本,即DirectoryInfoWrapper,而不是只有一个字母不向任何不熟悉代码的人解释任何内容。但那只是我。
#2
I'll agree with aberrant80 , if everyone agrees with the convention you are using, then it'll work.
我同意异常80,如果每个人都同意你正在使用的惯例,那么它就会起作用。
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.
我个人更喜欢使用更短和描述类目的的名称。至少在界面层面。如果您使用的是模拟框架,那么IDirectory或IDirectoryInfo将是一组不错的名称,而DirectoryInfoW或DirectoryInfoWrapper将是一个接口实现者。
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.
一个更好的例子可能是包装HttpRequest;定义一个IRequest来声明'这是对我的应用程序很重要',然后Request,HttpRequestWrapper,Request等将是实现者。
So, to summarize, try and use descriptive, non-overly-verbose interface names.
因此,总而言之,尝试使用描述性的,非过于冗长的接口名称。
#3
Just as a side note, I found a more aesthetically pleasing (well, to me) way of wrapping native method calls:
就像旁注一样,我发现了一种更美观的(好吧,对我来说)包装本机方法调用的方式:
public class NativeMethods
{
// made virtual so that it can be mocked - I don't really want
// an interface for this class!
public virtual bool RevertToSelf()
{
return WinApi.RevertToSelf();
}
...
private static class WinApi
{
[DllImport("advapi32.dll")]
public static extern bool RevertToSelf();
...
}
}
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的建议,并明确地调用我的包装器包装器。
#4
If you are using C++, you can use namespaces and then just re-use the same class name. For example:
如果您使用的是C ++,则可以使用命名空间,然后重复使用相同的类名。例如:
namespace WrapperNamespace
{
class MyClass {...};
}
namespace InternalNamespace
{
class MyClass {...};
}