I have a question regarding the repository design pattern. Let's say I have a datatype Foo
. In several places in my application, I must fetch a list of Foo
from a remote database. For example, let's say I need to fetch a list of trending Foo
s and somewhere else a list of the most recent Foo
s. In my mind I have two options:
- I could define two repository classes, one called
TrendingFoosRepository
which fetches from/foos/trending
and another calledRecentFoosRepository
which fetches from/foos/recent
. - However, since
TrendingFoosRepository
andRecentFoosRepository
do exactly the same thing just at different endpoints, I could define one repository class calledFoosRepository
which takes in anendpoint
argument, and instantiateFoosRepository('/foos/trending')
andFoosRepository('/foos/recents')
.
To me, the second option is more concise and reusable and is therefore preferable. However, something feels odd to me about passing in endpoints into a repository. Is this antipattern? If so, why and what is the alternative?
Aucun commentaire:
Enregistrer un commentaire