Best practice: wrapper service for Capacitor APIs / plugins

In the context of a Ionic/Angular app, I am inclined to always create a wrapper service around Capacitor APIs and plugins.

The rationale is to import the Capacitor plugin only once, as well as all the boilerplate (like checking the plugin is available, handling platform differences…).

Then in the application I only use the dedicated service, and never use the Capacitor plugin directly in other services.
This also simplifies Unit Testing.

I would like to know what others do and what is considered Best Practice.

Do you always create dedicated services, sometimes, never?
And please explain why.

1 Like

:cry: :cry: :cry:
Is there a better place to ask that question?

Hey @sebastienguillon1 ,

i normally do it like you. One Plugin = One Service :blush:

  1. Easier when Plugin api changes
  2. better for testing & mocking (as you mentioned)
  3. Code that belongs to one native feature in one service (good capsulation)
2 Likes

Same here. I use a dedicated class/service to wrap Capacitor plugins.

2 Likes