mardi 27 juillet 2021

laravel service-repository pattern static vs non-static methods

i've got a laravel project where service and repository classes are created as following:

class UserService
{
    public function save($data)
    {
        return User::create($data);
    }

    public function update($id, $data)
    {
        return User::where('id', $id)->update($data);
    }

    public function delete($id)
    {
        return  User::where('id', $id)->delete();
    }
}
class UserRepository
{
    public function findAll()
    {
        return User::all();
    }

    public function find($id)
    {
        return User::find($id);
    }
}

and then those are initiated in the controller like following:

    protected $userRepository;
    protected $userService;

    public function __construct(UserRepository $userRepository, UserService $userService)
    {
        $this->userRepository = $userRepository;
        $this->userService = $userService;
    }

now my question is, since there's nothing else but static method call inside those class methods, is there any reason/benefit for make those methods non static and initiating those on the controller firsthand instead of make those static as well for the ease of use?

Aucun commentaire:

Enregistrer un commentaire