Mushr00m's avatar

About the Repository Pattern

Hi,

I'm trying to use the Repository Pattern but have a little issue. I have models for Artist, Song, Album and Lyrics. For the relations part, it's classic, a Lyrics belongs to an Artists, an Album and a Song. Also an Artist or a Song or an Album has many Lyrics. Super simple.

My problem is that I want to get all the Lyrics for an Artist and also for a Song or an Album. I don't want to duplicate my code so I try to find what repo should have the function that does this. Is it more logic to have findAllLyrics() in an ArtistRepo or to have findAllForArtist() in the Lyrics repo ?

A note about that, I don't want to duplicate the code because it's not a simple Lyrics::all(), I have inputs that can filter the results, also change the order and the pagination amount. So the query is not small.

Thanks !

0 likes
3 replies
DarkRoast's avatar

To me it would make more sense to have your lyrics repository return the lyrics model or whatever data type you represent that as..

twoSeats's avatar
Level 1

Hi @Mushr00m,

If I understand what you're trying to achieve, I'd say that you should be creating something like.

interface LyricsRepository {

    getAll();
    findByArtist($id);
    findByAlbum($id);
    findBySong($id);
}

Personally I would be using the relation flexibility provided by the Eloquent Model class and using the $with property or with() method from the Eloquent Builder class. You could have lyrics returned with the Artist model by default if required. I guess it depends on the context and what your application needs.

You can just write your code to get to working functionality and refactor later though.

2 likes

Please or to participate in this conversation.