dplase3140 2014-07-28 11:18
浏览 25

Laravel - '存储库模式'问题

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?

  • 写回答

2条回答 默认 最新

  • doulei2100 2014-07-28 11:32
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机数字电压表电路组成及框图
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line