سؤال

I have a project that gets all of its data from API calls.

The server that hosts this API connects with it's own database (not even sure what DB it is, etc).

More or less the idea of a repository is to separate data and map it, right? I shouldn't be concerned where my data comes from. But if, at the end of the day, they are API calls, should the repository itself be doing all the api calls within its methods?

For example if we had a FindBY method in the repository, should that method be the one making the API call?

هل كانت مفيدة؟

المحلول

The gist of using a repository is to allow replacing the repo object with a mock repo for automatic testing purposes. If your API implementation allows to be replaced that easily (in-memory, without the need for a 2nd server, without any horrible overhead), then you don't need a repository.

For example, you want to write an automatic test for code which uses your FindBY method - can you easily create an environment in which the FindBY does not actually call a DB server? When the code under test calls "FindBY", it should get a predefined result set from your mock repository, so the automatic test runs fast, and if it fails, you know for sure it does not fail because the network is down or the data in the DB server was changed.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى softwareengineering.stackexchange
scroll top