Should I open interfaces instead of specific objects in my API?

I am working on an API, and one of my teammates insists that it is better to return interfaces than to return specific objects to the end user.

The problem is that as the coding of the API continues, the maintainability of the interfaces becomes difficult, if we need to add something like a parameter to the interface method, then we need to add it to the interface first, and then add this method to all the others classes that inherit from this interface.

This made us doubt the benefits of these interfaces and their maintainability, is there really a good reason to expose only the APIs?

+4
source share
1 answer

"Is there really a good reason to expose only APIs?"

Absolutely! Testability! For you and for your consumers. They can mock your API to return layouts that implement your interfaces and test their code without having to run your API.

It also gives you great flexibility in your implementation. By keeping your concrete classes out of the public eye, you can make rev-to-rev changes about what you are actually returning to satisfy the interface request.

+9
source

All Articles