DRY:建议如何不重复代码?
好的,我的问题来了,我有一个具有这种方案的数据库
+-------------+ +------------+ +-----------+
+ Object + + car + + computer +
+-------------+ +------------+ +-----------+
+ id + + object_id + + object_id +
+ some_col + + max_speed + + CPU_speed +
+ type + + n_gears + + memory +
+-------------+ +------------+ +-----------+
因此,总结一下,例如,当我的用户输入计算机时,我创建一个对象记录,将所有共享字段保存到其中,然后创建一个计算机记录,其中包含计算机的所有特定数据已保存。
到目前为止一切顺利,我的问题来了,这样做我必须重复每个操作(以及每种类型对象的控制器方法视图)。所以现在在我的控制器中,我有这些操作:
+ new-car
+ new-computer
+ show-car
+ show-computer
+ edit-car
+ edit-computer
+ list-car
+ list-computer
因此,对于每种类型,我基本上复制所有代码并更改几行代码(即用计算机映射器替换汽车映射器,对于记录对象也是如此)。我不得不提一下 zend 框架的应用程序,它是用 PHP 编写的。
我想做更多“不要重复自己”的事情,而不必重复所有代码。知道如何更优雅吗?
Ok here come my question, I have a database with such scheme
+-------------+ +------------+ +-----------+
+ Object + + car + + computer +
+-------------+ +------------+ +-----------+
+ id + + object_id + + object_id +
+ some_col + + max_speed + + CPU_speed +
+ type + + n_gears + + memory +
+-------------+ +------------+ +-----------+
So to summarize when my user input a computer for example, I create a object record, save all shared fields into it and I create a computer record with all specific data for computers being saved.
So far so good, here comes my problem by doing so I would have to duplicate every action (and controller methods view for each type of objects). So right now in my Controller, I have those actions:
+ new-car
+ new-computer
+ show-car
+ show-computer
+ edit-car
+ edit-computer
+ list-car
+ list-computer
So for each type I basically duplicate all the code and change few lines of codes (i.e replacing the car mapper by computer mapper, same for record object). I have to mention the application you the zend framework and is in PHP.
I would like to do something more "Don't repeat yourself" and not having to duplicate all the code. Any idea how to it more elegantly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将与您分享我的基本 CRUD 课程。按原样提供。自己测试一下。可能会出现一些错误;)
然后是具体的实现:
希望这会有所帮助;)
I'll share with you my basic CRUD class. Supplied as-is. Test for yourself. Some bugs may be ;)
And then a concrete implementation:
Hope this helps ;)
您必须创建一个元表,其中包含每种类型对象的信息,即它具有哪些字段、如何显示每个字段(例如输入或选择)、它们的顺序等等。
然后您可以访问主控制器,只需将对象名称传递给它,然后它就会生成适当的 CRUD 表单。
You'll have to create a meta-table with information on each type of object, i.e. what fields it has, how to display each of them (input or select for example), their order and stuff like that.
Then you can access the main controller, only passing the object name to it and it it then should generate the appropriate CRUD forms.
我做了类似于 Tomáš Fejfar 的事情...
创建一个基本 CRUD 控制器和一个具有 CRUD 方法的基本模型类。您甚至可以使用适用于基本 CRUD 模板的所有内容类型的通用视图脚本。
在每个控制器中,您只需要顶部的一些变量即可进行必要的更改。
I did something similar to Tomáš Fejfar...
Create a base CRUD controller, and a base model class which has CRUD methods. You can even use generic view scripts that will work for all content types for basic CRUD templates.
In each controller, all you'd need is a few variables at the top to make any alterations necessary.