我是 Laravel 开发新手,我正在尝试像一名优秀的程序员一样理解和应用 SOLID 原则。所以我最近学习并应用了 laravel 中的存储库模式。
为此,我创建了一个目录存档并使用 psr-4 加载它,如下所示:
"Archive\\": "archive/"
然后我创建了一个名为 Repositories 的文件夹和另一个名为 Contracts 的文件夹。现在在 Contracts 文件夹中我有 interfaces 像 UserRepositoryInterface 和 ServicesRepositoryInterface 等等,在 Repositories 文件夹之外, 我有 DbUserRepository 和 DbServiceRepository 等实现。
我正在使用一个名为 DataServiceProvider 的 service provider ,我像这样绑定(bind)它们:
$this->app->bind(UserRepositoryInterface::class, DbUserRepository::class);
$this->app->bind(ServiceRepositoryInterface::class, DbServiceRepository::class);
这样我就可以在我的 Controller 中注入(inject) Contacts,比如 UserRepositoryInterface 和 ServiceRepositoryInterface,Laravel 会自动从 IoC 容器中解析我的依赖项.因此,如果将来我需要一个 FileUserRepository,我只需要创建该类并更改我的 service provider 中的绑定(bind),我的 Controller 就不会出现任何问题。
这是我从 Taylor 和 Jeffrey 那里学到的。但现在我正在尝试使用一个包 https://github.com/andersao/l5-repository对于我的项目。
据此,我将扩展我的 DbUserRepository 与它附带的 BaseRepository,如下所示:
namespace App;
use Prettus\Repository\Eloquent\BaseRepository;
class UserRepository extends BaseRepository {
/**
* Specify Model class name
*
* @return string
*/
function model()
{
return "App\\Post";
}
}
现在,显然我重用了 BaseRepository 附带的所有代码和功能,例如 all()、panginate($limit = null, $ columns = ['*'])、find($id) 等等,但现在我打破了控制反转原则,因为现在我必须将具体实现注入(inject)到我的 Controller 中?
我仍然是一名新手开发人员,并试图理解所有这些,并且在解释事情时可能在问题的某个地方出错了。在使用包的同时又能在 Controller 中保持松散耦合的最佳方法是什么?
最佳答案
没有理由你仍然不能实现你的接口(interface):
namespace App;
use Prettus\Repository\Eloquent\BaseRepository;
class DbUserRepository extends BaseRepository implements UserRepositoryInterface {
/**
* Specify Model class name
*
* @return string
*/
function model()
{
return "App\\Post";
}
}
但是,您现在遇到了一个问题;如果你换掉你的实现,你的 UserRepositoryInterface 中没有任何内容表明 BaseRepository 方法也必须被实现。如果你看一下 BaseRepository 类,你应该看到它实现了它自己的两个接口(interface):RepositoryInterface 和 RepositoryCriteriaInterface 和 'luckily' php 允许多接口(interface)继承,这意味着您可以按如下方式扩展您的UserRepositoryInterface:
interface UserRepositoryInterface extends RepositoryInterface, RepositoryCriteriaInterface {
// Declare UserRepositoryInterface methods
}
然后您可以正常绑定(bind)和使用您的界面:
$this->app->bind(UserRepositoryInterface::class, DbUserRepository::class);
关于php - 如果我在 Laravel 5 项目中使用 andersao/l5-repository 会破坏控制反转原则吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33216727/
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat
如果我使用ruby版本2.5.1和Rails版本2.3.18会怎样?我有基于rails2.3.18和ruby1.9.2p320构建的rails应用程序,我只想升级ruby的版本,而不是rails,这可能吗?我必须面对哪些挑战? 最佳答案 GitHub维护apublicfork它有针对旧Rails版本的分支,有各种变化,它们一直在运行。有一段时间,他们在较新的Ruby版本上运行较旧的Rails版本,而不是最初支持的版本,因此您可能会发现一些关于需要向后移植的有用提示。不过,他们现在已经有几年没有使用2.3了,所以充其量只能让更
啊,正则表达式有点困惑。我正在尝试删除字符串末尾所有可能的标点符号:ifstr[str.length-1]=='?'||str[str.length-1]=='.'||str[str.length-1]=='!'orstr[str.length-1]==','||str[str.length-1]==';'str.chomp!end我相信有更好的方法来做到这一点。有什么指点吗? 最佳答案 str.sub!(/[?.!,;]?$/,'')[?.!,;]-字符类。匹配这5个字符中的任何一个(注意,。在字符类中并不特殊)?-前一个字符或组
最近,我安装了OSXMavericks,它似乎弄乱了我的开发环境。我在运行“railsnewfirst_app”后收到此消息:Youruseraccountisn'tallowedtoinstalltothesystemRubygems.Youcancancelthisinstallationandrun:bundleinstall--pathvendor/bundletoinstallthegemsinto./vendor/bundle/,oryoucanenteryourpasswordandinstallthebundledgemstoRubygemsusingsudo.Pass
基本上,我只是试图在满足特定条件时停止程序运行其余行。unlessraw_information.firstputs"Noresultswerereturnedforthatquery"breakend然而,在程序运行之前我得到了这个错误:Invalidbreakcompileerror(SyntaxError)执行此操作的正确方法是什么? 最佳答案 abort("Noresultswerereturnedforthatquery")unlesscondition或unlessconditionabort("Noresultswer
如果用户是所有者,我有一个条件来检查说删除和文章。delete_articleifuser.owner?另一种方式是user.owner?&&delete_article选择它有什么好处还是它只是一种写作风格 最佳答案 性能不太可能成为该声明的问题。第一个要好得多-它更容易阅读。您future的自己和其他将开始编写代码的人会为此感谢您。 关于ruby-on-rails-如果条件与&&,是否有任何性能提升,我们在StackOverflow上找到一个类似的问题:
我有一个数组:array=['Footballs','Baseball','football','Soccer']而且我需要计算看到Football或Baseball的次数,无论大小写和复数形式如何。这是我尝试做的,但没有成功:array.count{|x|x.downcase.include?'football'||x.downcase.include?'baseball'}编写这段代码的正确或更好的方法是什么?我正在寻找3作为答案。 最佳答案 我会将count与一个block结合使用,该block根据与您正在寻找的约束相匹配的正
升级到OSXYosemite后,我现有的pow.cx安装不起作用。升级到最新的pow.cx无效。通过事件监视器重新启动它也没有成功。 最佳答案 卸载(!)并重新安装解决了这个问题。curlget.pow.cx/uninstall.sh|shcurlget.pow.cx|sh 关于ruby-on-rails-OSXYosemite更新破坏了pow.cx,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/q