在阅读T.Otwell关于Laravel中良好设计模式的书之后,在Laravel4中创建应用程序时,我发现自己正在为应用程序中的每个表创建存储库.

I ended up with the following table structure:

  • Students: id, name
  • Courses: id, name, teacher_id
  • 教师:ID,姓名
  • 作业(job):ID、名称、Course_id
  • Scores (acts as a pivot between students and assignments): student_id, assignment_id, scores

I have repository classes with find, create, update and delete methods for all of these tables. Each repository has an Eloquent model which interacts with the database. Relationships are defined in the model per Laravel's documentation: http://laravel.com/docs/eloquent#relationships.

When creating a new course, all I do is calling the create method on the Course Repository. That course has assignments, so when creating one, I also want to create an entry in the score's table for each student in the course. I do this through the Assignment Repository. This implies the assignment repository communicates with two Eloquent models, with the Assignment and Student model.

我的问题是:由于这个应用程序的大小可能会增加,并且会引入更多的关系,与存储库中的不同Eloquent 模型通信是一种好做法,还是应该使用其他存储库(我的意思是从分配存储库中调用其他存储库)来实现这一点,还是应该在Eloquent 模型中一起完成?

Also, is it good practice to use the scores table as a pivot between assignments and students or should it be done somewhere else?

推荐答案

请记住,您是在征求意见:D

这是我的:

TL;DR: Yes, that's fine.

You're doing fine!

I do exactly what you are doing often and find it works great.

I often, however, organize repositories around business logic instead of having a repo-per-table. This is useful as it's a point of view centered around how your application should solve your "business problem".

课程是一个"实体",有属性(标题、id等),甚至还有其他实体(作业(job),它们有自己的属性,可能还有实体).

您的"课程"存储库应该能够返回课程和课程属性/作业(job)(包括作业(job)).

You can accomplish that with Eloquent, luckily.

(我通常每个表都有一个存储库,但有些存储库的使用量比其他存储库多得多,因此方法也多得多.例如,如果您的应用程序更多地以课程为中心,而不是课程的作业(job)集合,那么您的"课程"存储库可能比您的作业(job)存储库功能更全面).

棘手的部分

I often use repositories inside of my repositories in order to do some database actions.

任何为了处理数据而实现Eloquent的存储库都可能返回Eloquent模型.有鉴于此,如果你的课程模型使用内置关系来检索或保存作业(job)(或任何其他用例),那也没关系.我们的"实施"是建立在Eloquent 的基础上的.

From a practical point of view, this makes sense. We're unlikely to change data sources to something Eloquent can't handle (to a non-sql data source).

ORMS

至少对我来说,这种设置最棘手的部分是确定Eloquent 到底是在帮助我们还是在伤害我们.ORM是一个棘手的主题,因为虽然从实用的Angular 来看,它们对我们帮助很大,但它们也会将"业务逻辑实体"代码与执行数据检索的代码结合起来.

这会混淆存储库的责任到底是处理数据还是处理实体(业务域实体)的检索/更新.

此外,它们还充当您传递给视图的对象.如果以后必须避免在存储库中使用有说服力的模型,则需要确保传递给视图的变量具有相同的行为方式或具有相同的可用方法,否则更改数据源将滚动到更改视图,并且您已经(部分)失go 了首先将逻辑抽象到存储库的目的-项目的可维护性会下降.

Anyway, these are somewhat incomplete thoughts. They are, as stated, merely my opinion, which happens to be the result of reading Domain Driven Design and watching videos like "uncle bob's" keynote at Ruby Midwest within the last year.

Laravel相关问答推荐

使用MAATWebSite/EXCEL导入Exel时,不会创建Laravel模型

查询Laravel中的图形

如何在 Laravel 中使用多对一变形关系

Laravel 获取具有多对多关系的中间表数据

Laravel 9:AWS S3 检索要流式传输的视频

如何在 laravel 5.8 中显示自定义消息?

如何在 Laravel 中填充以后执行的条件

如何在 Laravel Eloquent Collection 数组之前添加一个键值对?

我的composer 不会完全用 Laravel 4 更新它会被artisan卡住

在 Laravel 5 中添加新配置文件不起作用

使用 Eloquent (Laravel) 在 Group By 之前排序

在 laravel 中更改时区

这个上下文的流程应该是怎样的? (根据 Select 的付款方式,将处理付款的代码放在哪里?)

带有时间戳的 Laravel 5.1 eloquent::attach() 方法

PHP Laravel:如何获取客户端浏览器/设备?

升级到 laravel 5.3 后错误 datetime format: 1292 Incorrect datetime value: '0000-00-00 00:00:00'

在 Laravel 测试用例中模拟一个 http 请求并解析路由参数

找不到框laravel/homestead

Laravel Route 模型与关系绑定

Laravel 更新查询