nolros's avatar
Level 23

Abstract Repository Class - good or bad?

I find myself repeating the same methods across repositories. Is an abstract repository breaking the SOLID principles? I'm sure it a security taboo, thoughts?

Example :

abstract class AbstractRepository {

    /**
     * Get row from repository
     * 
     * @param $id
     * @param $alias
     * @return mixed
     */
    public function getRow($id, $alias)
    {
        return $this->{$alias}->find($id);
    }

   // rest of methods   
}
0 likes
11 replies
accent-interactive's avatar

As long as the abstract class is used for only a certain type of implementation I woild say it's quite alright.

E.g. you vould have abstract classes: RepositoryEloquentAbstract.php RepositoryFluentAbstract.php RepositoryRssAbstract.php

1 like
thepsion5's avatar
Level 25

An abstract repository doesn't break SOLID principles on it's own, no. I have a helper package that contains abstract database and eloquent repositories. The only consideration I ever have toward SOLID is the abstract repositories don't contain public methods because I don't want them to influence the public API. But I don't think it would be a big deal either way, it's more my personal preference.

That being said, a lot of what you can do with an abstract class you can also do more cleanly with traits. For example:

Inheritance:

abstract class FilterableAndSortableRepo { /* snip */ }

abstract class FilterableAndSortableEloquentRepo extends FilterableAndSortableRepo { /* snip */ }

class EloquentPostRepo extends FilterableAndSortableEloquentRepo { /* snip */ }

Traits:

trait FilterableRepo { /* snip */ }
trait SortableRepo { /* snip */ }

class EloquentPostRepo
{
    use FilterableRepo;
    use SortableRepo;
    /* snip */
}
1 like
milon's avatar

Rather I would like to use traits. My personal preference.

1 like
nolros's avatar
Level 23

@thepsion5 @millon @joostvanveen Thanks guys that makes perfect sense. I really like the concept of "RepositoryEloquentAbstract.php RepositoryFluentAbstract.php RepositoryRssAbstract.php" very hot! and did not think about it, but trait seems like the logical approach. Now to go redo lost of code ... lol.

Thanks again, your time and experience is very much appreciated.

1 like
bestmomo's avatar

I like a lot this forum where I learn a lot and where I come to ask me a lot of questions that I had never asked me earlier stages !

It seems to me that the choice between classes and traits is not a matter of taste but of context. Traits provide more flexibility but can be difficult to manage if they become numerous.

1 like
thepsion5's avatar

@joshuahornby No, but I'll see if I can spend some time cleaning it up and putting it up on packagist in the near future. It's mostly a matter of me being lazy and not wanting to write tests for Eloquent's fluent interface. ;)

2 likes
nolros's avatar
Level 23

@ thepsion5 I'm with @joshuahornby that definitely be something I would use. Maybe post it as dev and let other contribute by testing and writing tests. I find myself cutting and pasting the same code from repo to repo with class changes, seems like there is an opportunity for optimization.

1 like

Please or to participate in this conversation.