Laravel4:从长远来看,不使用存储库模式会伤害我的项目? [英] Laravel4: Will not using the repository pattern hurt my project in the long run?

查看:115
本文介绍了Laravel4:从长远来看,不使用存储库模式会伤害我的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读泰勒·奥特维尔的书,他建议他使用存储库模式。我得到了这个理论。这很容易切换实现和解耦你的代码。我也得到代码。而通过一些其他的实现,可以通过 App :: bind()切换是非常好的。但是我在过去两个小时里一直在想,我应该如何处理我正在建立的新CRM的事情。几乎没有代码,但它可能会变得很大。



我宁愿简单地使用雄辩的模型和通过控制器注入的收集。如果我没有错,那应该使一切都可以测试。允许嘲讽等。



存储库模式是否可以为可扩展性提供任何好处?在需要更多服务器或资源的情况下。



为什么有人想要代替雄辩的ORM一旦使用它?我的意思是大多数项目需要一个关系数据库。如果我将构建一个 UserRepositoryInterface 和一个实现 DbUserReponsitory ,我看不到我什么时候需要或想要切换还有别的东西这将计入我项目中的大多数实体(订单,公司,产品,...)。也许使用另一个ORM取代雄辩?但是我没有看到自己在这个项目中执行此操作。



还是我缺少一些东西?我的主要关注点是可以使用易于测试的代码。没有真的很好地测试到目前为止:)

解决方案

这并不一定意味着忽略Repository模式会给您长期以来头痛,但它确实使得重新分解代码更容易,因此您可以轻松地编写测试存储库并将其与控制器绑定,以轻松测试您的业务逻辑。



所以即使你不太可能会取代雄辩或任何其他的部分,将大量的逻辑和操作封装在存储库中仍然会对你有所帮助。


I'm reading Taylor Otwell's book in which he suggests using the repository pattern. I get the theory behind it. That it's easy to switch implementations and decouples your code. I get the code too. And it is very nice to be able to switch by App::bind() with some other implementation. But I've been thinking for the last two hours how I should approach things for a new CRM I'm building. Almost no code yet but it might end up big.

I would prefer to simply use eloquent models and collection injected through the controller. That should make everything testable if I'm not mistaken.. . Allowing for mocks and such.

Does the repository pattern maybe offer any benefits towards scalability? In cases when more servers or resources are needed..

And why would anybody want to replace the Eloquent ORM once committed to its use? I mean most projects need a relational database. If I would build a UserRepositoryInterface and an implementation DbUserReponsitory, I cannot see when I would ever need or want to switch that with something else. This would count for most entities in my project (order, company, product, ...). Maybe to use another ORM to replace Eloquent? But I don't see myself doing this for this project.

Or am I missing something? My main concern is to have code that is readable and easy to test. Haven't really been testing that well up to now :)

解决方案

It does not necessarily mean that ignoring the Repository pattern will give you headaches on the long run, but it certainly makes re-factoring code much easier thus you can easily write Test repositories and bind them with your controllers to easily test your business logic.

So even if it is not likely that you will replace Eloquent or any other part, encapsulating chunks of logic and operations in repositories will still be beneficial for you.

这篇关于Laravel4:从长远来看,不使用存储库模式会伤害我的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆