唔,我是那个一直写React&Redux项目的Weny
上次抽离ListView感觉后端上还可以再偷懒 嘿嘿嘿所以就有了
随着项目越复杂,ORM关系会像下图一样复杂的一团糟。
(单向正方形线条代表:A一对多B(正方形所在位置))
(双向正方形线条代表:A多对多B、部分多态多对多)
那么像偷懒的前端兼后端工程狮就出现了。
设计了一种API设计模式,可以在一对多,多对多关联模型中的数据获取中拥有更多的通用代码量。
很简单:
Params:
Token
FatherModelType
FatherModelIds
SortBy
OrderBy
Page
Item
那我就来演示下
Fake Common Language
//This is a Questions Controller 's func
public function unity()
{
//get School's Questions
// User's Questions
// ...'s Questions
//First get the input
//maybe Http Method is Post/Get
//maybe Route conf file func's params
...
UnityParams APIParams= new UnityParams();
//UnityParams is a Class
//UnityParams
////String token
////String fatherModelType
////String SortBy
////String OrderBy
////int Page
////int page
if(Input('token')!=null)
//Input() is a func to get Params by key
//do some auth
if(Input('FatherModelType')!=null)
{
APIParams.token=Input('....')
}else{
//may return bad request 400
}
....
switch(APIParams.fatherModelType)
{
case 'user':
//user's questions
//database query...
break;
case 'school':
//sch`请输入代码`ool's questions
break;
default:
//return bad request 400
}
...
}
route设计上可以像这样
/:fatherModeltype/:fatherModelIds/childrenModel?token=baba..&....
或者在route配置中
route('/school/questions','QuestionsController@unity('school')')
route('/user/questions','QuestionsController@unity('user')')
把以往
/School/questions
/User/questions
/..../questions
全部丢到一个Controller里处理
唔,就是这样的一个思路,大家按照自己的业务逻辑来设计就好了。
唔,这个模式就是我在偷懒的过程中总结的。自从用了这个UnityAPI我又可以撩好一会妹子了。嘿嘿嘿