Wrapper Classes and Candy Wrappers!

October 03, 2017

With Halloween just around the corner I figured it would be good to talk about some wrappers.  As a consultant, I frequently choose to use an already established and community trusted library, plugin, or some other type of dependency that handles a specific task rather than writing my own from scratch.  Handling specific protocols like authentication or validation can be self written but there’s a possibility that some needed unknown behavior is left out.  People in the community use and trust certain libraries for a reason.  

Something I’ve learned through my experience as a developer that helps me and anyone on my team out is to wrap any dependencies used in a wrapper class.  

So you ask, “What’s a wrapper class Sam?”

“It’s an abstraction for your dependencies to ensure the application code is less dependent on a specific dependency,” replied Sam, the ingenious master developer.  

Let’s look at the problem first and we will expand on the solution later.  We need to use a plugin to map one class’s properties to another class’s properties.  We’ll use a mapping library that we found online!  It’s called SamsMappingPlugin.  Let’s look at how the code looks for us to start using it.

X53jIyYoFckLola3OcMLhM8nOQ_S8TTDc4dqdTTId9LBVf3hpbEscnXDHhiK9ku-TklVZiTdma2B0Hrg7M5Jq6Bgk6br7tGBSffFmqpxP-1IzV8Qn2hSeOUBH8iY9oEQJz9AV1Hq

Take a look at the code with the red underlines.  We now have the “using” statement and a call directly to the library code using the program specific syntax and naming.  All of this is ok and will work fine but if presented with the situation of needing to choose a different mapping library.  It could take some time to make all of the adjustments.  

A super hip coworker comes in to work and says, “Hey!  I found this new mapping library yesterday that’s supposed to be 30 times faster than SamsMappingPlugin and it also works with the latest version of .NET Core!  SamsMappingPlugin only works on .NET 4.5.  We should switch to this newer and better mapping plugin!”

After some research and pushback it appears your hip coworker was right and this OtherMappingPlugin looks like the best option.  The downside is all of your code is dependent on SamsMappingPlugin directly which means you will need to go change all of the code using SamsMappingPlugin to use the OtherMappingPlugin code.  Here’s what it looks like after switching to OtherMappingPlugin.

mSsmsYb6irV1QgUw8KnSeEouX4XlZDPBRBm1J_OrzLl-GzFkV33tC1l7Jjs4yszBLPxSAHWTuCRhLOO06W02yjDKy_gDp8zd0IdN6Mj7My7Ln_JkoYHdqs4kN2psGErNHoUDKIlF

Look at the code underlined with red in the image above.  Notice that we have updated our code to use the OtherMappingPlugin mapper, but also notice that the naming conventions of this library are different.  This will still work fine unless another hip coworker comes in and has discovered a new better mapping library to use.  Then, we would be in the same boat as before.   We would have to go change the code in all six billion places that relies directly on these dependencies.

ENTER.  WRAPPER CLASS.

What’s a wrapper class again?  A wrapper class is simply a custom class that you make that surrounds, contains, or wraps a dependency but still allows the dependency’s functionality to be used.  It’s solves the problem of being too reliant on your dependencies directly.  It allows you to abstract the dependencies into a centralized location and it makes it easy to change dependencies if the situation arises.  Here’s what I do.

9am9ADshhao33PP7VoYkJqyWzF8vPwnrRoMuD-_hpHaSX697xKYQZ7dVWeMAUjOJio1cFycExUINbR2KFdvd-eCZIBFPP8JbZnmZ2N3ijz_LptFjtk-KHWAlkXz8CL8fRvZtMXIC

Notice above that I’m using SamsMappingPlugin on the red underlined code.  Also, notice that the green underlined code is our custom function that does nothing except for call the SamsMappingPlugin code.  This is the wrapper in effect.  Now we can change all of the dependency reliant code to use the wrapper class.  

qRDaqMJhdl1SHRVVYo1hGNkVkqy49DzGQl70s2zHflIgVqAWVGwyvw-IDnj3390o4KJ9sz7AYN1ZFQr46Oi3e1E2ZSCA6U0ssBd7sMmIfxys36OfzChzi_-e5A-kKPKn1nure3fn

Now we have the logic using the wrapper instead of a SamsMappingPlugin directly.  We are also ready for what the future holds.  Tomorrow, our most elitist hip developer could come in and realize that we need to change our mapping plugin and all it would take is changing one line.  

G0HaskOT25N9a820kEOoZsI4vhasHIfBby75cnGYQmWQ8lYCh7DtFn6mHl0iHkN_0S8MtFLV63zcu3boIMRzJbG_jdsZiV-ZYMFPih1NEuMdSY4f1O3XDk6AwOqbjQ6o42JzYEJM

Now look at the image above and notice what has changed.  The only line that has changed is the line underlined in red.  We now have our entire application using the OtherMappingPlugin.  The green underlined code is to show that it stayed the same.  Our logic is not relying on any specific mapping dependency, so none of the six billion times we map objects have to change!  

This is a very smart technique to handle the management and usage of any dependencies.  There is minimal setup and it can save developers a lot of time in the future.  Wrapper classes were taught to us in early college but I don’t know that many of us remember that.  So for Halloween this year, wrap your dependencies, only hand out wrapped candy, and watch some David S. Pumpkins with the time saved swapping out future dependencies.  

Sam North

Did this blow your mind?  Did it make you a better developer?  Do you have a better way to do it that I don’t know of yet?  Here at Sparkhound there is always a conversation going on about good ways to improve our code.  If you like discussions like that or like to do a good job and also have some fun then contact someone at Sparkhound or me directly at sam.north@sparkhound.com.

Information and material in our blog posts are provided "as is" with no warranties either expressed or implied. Each post is an individual expression of our Sparkies. Should you identify any such content that is harmful, malicious, sensitive or unnecessary, please contact marketing@sparkhound.com.

Meet Sparkhound

Review our capabilities and services, meet the leadership team, see our valued partnerships, and read about the hardware we've earned.

Engage with us

Get in touch with any of our offices, or check out our open career positions and consider joining Sparkhound's dynamic team.