Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I expose interfaces instead of concrete Objects on my API?

Tags:

rest

c#

I'm working on an API and one of my teammates insists that it is better to return Interfaces than returning concrete objects to the end user.

The problem is that as the API coding is ongoing the maintainability of the interfaces is getting troublesome, if we need to add something like a parameter to a method of the interface then we need to add it in the interface first, and then add that method on all the other classes that inherit from that interface.

That made us doubt of the benefit of having those interfaces vs their maintainability, is there really a good reason to expose only interfaces on an API?

like image 730
rovinos Avatar asked Mar 17 '15 15:03

rovinos


1 Answers

"is there really a good reason to expose only interfaces on an API?"

Absolutely! Testability! For you, and for your consumers. They can mock your API to return mocks implementing your interfaces and test their code without having to run your API.

It also gives you a lot more flexibility in your implementation. By keeping your concrete classes out of the public eye, you're free to make changes rev-to-rev about what you actually return to satisfy the interface request.

like image 152
n8wrl Avatar answered Sep 17 '22 09:09

n8wrl