A good way to use 3rd-party libraries into your app

January 4, 2023 Architecture, Design Pattern

Since I started developing mobile apps, I’ve been using a lot of 3rd-party libraries into my main apps. Although it was super cool to use popular external libraries out there, some pitfalls came along.

The main three pitfalls were;

  1. When some libraries must be upgraded because of the requirements from their eco-systems such as Google and Firebase, and they have some breaking changes
  2. When some native library upgrades are necessary, however bindings (kind of library wrappers for Xamarin. Yes, I worked on Xamarin ㅠ.ㅠ) are not available yet so need to work on own custom bindings and plan to replace them later
  3. When I write unit test

Those cases made me crazy and I had to pull out a bunch of hairs while resolving these. The most simple and easy solution that my colleagues and I came up with was to add an additional layer in between. This can be called like a wrapper or a delegator as well.

For instance, If I want to use the google map library, the architecture would be like:

Main app <- map wrapper <- google map

So, google map library is loosely coupled to the main app so it can be easily mocked and its changes don’t always affect to the main app. And there is even a chance to replace map provider if needed later.

“Wrapping third-party APIs is a best practice.” — Robert C. Martin

Yes, I also think it is a best practice and at least it is a good way to plug in 3rd-party libraries. I needed this kind of quote to back me up as he is a renowned software engineer a.k.a Uncle Bob in the software world.

I also noticed that this is the decorator pattern, which is one of the well-known design patterns from the Gang of Four. This pattern is quite often used when we do dependency injection stuff.

However, it is always not good to have black and white thinking. We have to choose a proper solution depending on our situations. Luckily, I was able to bring one good article from the Very good layered architecture in Flutter written by Very Good Ventures (VGV). If you go to the subtitle Use the right amount of abstraction in the article, you will see that there are two choices we can take. In most business cases, we have to wrap it though.

I also found out two really great articles about this approach to back me up. They explain everything that I want to write here so I could save my time and toss my baton to them. Nice..

If you want to see more organized and clean articles and examples, please read below links.

Happy Coding!