mercredi 8 juin 2022

Is it okay to pass an endpoint string to a repository?

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 TrendingFoosRepository which fetches from /foos/trending and another called RecentFoosRepository which fetches from /foos/recent.
  • However, since TrendingFoosRepository and RecentFoosRepository do exactly the same thing just at different endpoints, I could define one repository class called FoosRepository which takes in an endpoint argument, and instantiate FoosRepository('/foos/trending') and FoosRepository('/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