The Repository and Command Pattern…

So I’m a big fan of the Repository Pattern with Laravel. Using interfaces it separates the logic of searching away from the models and also acts as a place to store all your searching commands.

However sometimes I feel like the definition goes to far and needs splitting out. It might only be me making a rod for my own coding back, however, the repository makes sense to me only as a data access place, but not a wrapper for CRUD like operations.

So I split my repos out, there’s a base Class to extend which has some generic methods findOrFail, allPaginate. Then inside each extension are methods which search more specifically for some data.

However, for storing a new model I use a SelfHandling Command / Job.

class ProjectStoreCommand extends Command implements SelfHandling {

and the same for Destroy and Update.

It does make for more tests, but having the repo as a place for too much doesn’t sit right with the cleanliness of a Repository for me.

Author: Dan Course

Dan Course, E-Learning Development at Warwick University (WMG). Agile Scrum trained and full-stack development. Interested in E-learning, Democracy, Politics and Tech. All thoughts are mine and not of my employer.