Laravel Repository Pattern Contract

I am trying to implement the repository pattern using Laravel. I can understand the advantages of having interfaces to abstract the data storage.

But what about the objects returned by the methods defined in the interface. Should they also be defined in some kind of abstract class?

For instance:

class UserRepository implements UserInterface {
    protected $user;

    public function __construct(Model $user)
    {
        $this->user = $user;
    }

    public function find($userId)
    {
        return $this->user->find($userId);
    }
}

by doing this in my controller:

$user = UserRepository::find(1);
$user->name

am I breaking the design pattern?

Answers


Short answer: Yes.

Long answer: according to the Dependency Inversion Principle you always have to depend on abstractions not concretions. If one day you need to change the implementation of that Model, you'll have to change that repository class, also going against the Open-Closed Principle.


Need Your Help

jsonRootName missing while performing unit test case for spring hateoas

java spring-boot mockito junit4 spring-hateoas

I have developed an rest service using spring-boot-starter-Hateoas, and I am able to get the json output properly as shown below:

assign object to array in javascript multidimensional array

javascript arrays object multidimensional-array associative-array

Below i have given my code which is a javascript function which calls a php file and gets json data .... all things are perfect yet but i am getting problem when i want to adda data to a multidimen...