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 Foos and somewhere else a list of the most recent Foos. In my mind I have two options:
- I could define two repository classes, one called
TrendingFoosRepositorywhich fetches from/foos/trendingand another calledRecentFoosRepositorywhich fetches from/foos/recent. - However, since
TrendingFoosRepositoryandRecentFoosRepositorydo exactly the same thing just at different endpoints, I could define one repository class calledFoosRepositorywhich takes in anendpointargument, 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