I am developing a new microservice application that will be part of big architecture with lots of other microservices. This application needs to get content from other applications and I want to encapsulate HTTP calls into the service layer. But I noticed there are two different approaches.
Let's assume that my application will get contact information from another microservice which is deployed as User API.
This approach has the business name as the file name. Inside the file, there is only one public method which is get
and it receives a single parameter. The method calls user-api/contact/{id}
.
Filename: contact.service.js
Method: get(id) -> contact
This approach encapsulates all communication between User API and the consumer application into a single file. There is a method for every endpoint provided for User API.
Filename: user.service.js
Method: getContact(id) -> contact
What are the pros and cons using these approaches?
This question is more of an opinion question really, different companies / different software developers might give you different answers.
My take on this is, I think splitting them into their individual files - down to even the methods if the logic is complex - helps a lot with maintainability / testing.
Something like test contact.service.test.js
where the change is isolated and they can run the related tests immediately pretty easily.Ask these questions to yourself;
Do your services have very complex logic in them which will extend the file into thousands of lines - making it harder to document / harder to spot the mistakes / harder to refactor? Or are they smaller endpoints which the logic is offloaded to some other part of the system?
Are the tests for these methods / services going to exist in (another) single file - mimicing the pattern in the service implementation? Will you be able to call an individual test while testing a feature?
Are you prepared to accommodate the decision that you made across all of your services? Your particular service(s) therefore that exact file might be relatively small, what about a service which one of your colleagues might write and turn into a giant 1K line file?