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.
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.
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.
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.
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.
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.
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 firstname.lastname@example.org.