Rule of thumb for naming wrapper classes
Asked Answered
M

4

23

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 or WindowsIdentity)
  • 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?

Mingo answered 24/7, 2009 at 3:48 Comment(2)
Adding to the back is "appending" ;)Richrichara
Good point! I have edited my question accordinglyMingo
R
21

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.

Richrichara answered 24/7, 2009 at 6:51 Comment(0)
P
3

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.

Pinson answered 24/7, 2009 at 13:2 Comment(0)
M
0

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.

Mingo answered 30/7, 2009 at 2:35 Comment(0)
S
0

If you are using C++, you can use namespaces and then just re-use the same class name. For example:

namespace WrapperNamespace
{
    class MyClass {...};
}

namespace InternalNamespace
{
    class MyClass {...};
}
Salesperson answered 25/2, 2015 at 23:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.