Alias
Alias

Reputation: 3081

Laravel - 'Repository Pattern' questions

Using Laravel 4, I currently have an area for all of my repositories, and then I bind these repositories to an interface to use. This is all currently working and I access these in my controllers using the standard $this->users->find(1) method.

In my User repository, I have my assignRole (attach) and removeRole (detach) methods which I took my my model/User.php class. Okay great, but now I try to run my seeds it uses the eloquent classes, e.g. User::create();.

Question 1: Are my seed files also supposed to also use the repository pattern? I have no experience with say Mongo, but would I also seed the database the same way if I were going to switch?

Question 2: My Auth driver currently uses eloquent... so what if I also changed this? Should I be making a repository for my auth and implement things like $this->auth->login($user).

Really I'm confused as to when I should be implementing a repository interface. artisan migrate runs the seed files, which currently create a MySQL DB, what would happen if I wanted to use Mongo or a NoSQL DB?

Upvotes: 1

Views: 725

Answers (2)

Hamza Ouaghad
Hamza Ouaghad

Reputation: 589

As Taylor Otwell (The author or laravel) has mentiond in his book From Apprentice to Artisan, the repositories are a way of separating concerns, and to avoid fat controllers.

Namely, they are used to contain business logic that should be applied to your business objects, objects that are constructed from data in your database...

So, if you ask me whether I'll use such wrapping in my seed class, then I'll personally would not, because seeding -mostly- does not require any business logic, and, in the n-tiers architecture, seeding can be considered as belonging to the DATA layer.

If ever, seeding had to apply some business logic, then you might want to do so. Maybe you'd prevent a certain salary from being inserted if the employee has less than 1 year experience or so.. this kind of logic

I wrote a little article long ago that walks through a simple separation of concerns scenario to explain we use it to begin with.

If you are interested you might check it out @ coderwall

Upvotes: 1

Darren Craig
Darren Craig

Reputation: 462

good questions.

The short answer to each of your questions is "it depends".

With your seeds - if you're sticking to the pattern strictly, then yes, your seeds should probably use the repository pattern. For me, however - my seeds tend to be for a quick, one-off database population and/or for testing purposes, so using Eloquent for this is probably okay.

With your Auth driver, again, if you want to stick strictly to the pattern, then yes, you should probably code to an interface - but ask yourself whether or not you're really going to change your Auth implementation in the future - and whether the extra work/cost is really worth it.

Each project is unique in requirements, budget and particularly the likelihood of change after it goes live. You just need to assess these factors and decide the best approach for each project.

Upvotes: 1

Related Questions