kingshark
1 year ago

Repository best practice: pass object id or object(model instance) as repository functions' parameter?

Posted 1 year ago by kingshark

Hello everyone,

I've just sign in to Laracasts and this is my first question:

Now we use andersao/l5-repository package and adopt repository pattern. There is a dilemma, should we pass object id or object (instance of model) as the parameters of repository functions?

e.g: for UserRepositoryInterface, should we implement


public function flag($userId);

or


public function flag(User $user);

I prefer id, because as I understand, UserRepositoryInterface can be implemented by Model|Eloquent type of repository, but it might also be implemented by special repository without using of Model. Also, andersao/l5-repository package's BaseRepository functions all use object id as parameters.

But my tech lead thought it should be better to use model, and his reason is: when we get instance of model, we can use multiple repository functions, then every time pass id and find the same instance in DB is a waste.

I want to hear your suggestions, thanks a lot!

Please sign in or create an account to participate in this conversation.