这是在 MVC 应用程序上构建模型的好方法吗?
这更多的是“我做得对吗?”问题。
我正在使用 MVC 框架(Zend Framework),想知道这是否是构建模型的最佳方式。
在这种情况下,我有一个用户组模型,在编码一段时间后,我意识到在我的控制器上我正在重复一些东西,例如查询以返回可见组。
$model->fetchAll( array( 'enabled = 1', 'visible = 1' ) );
为了减少代码重复,我在此模型上创建了一个方法,该方法返回可见组的查询。
基于同样的情况,我还创建了许多其他方法,如隐藏组、禁用组等。
这是避免代码重复的最佳方法吗?我担心的是,如果我继续在控制器上插入该代码,如果可见组的业务规则将来发生变化,我会很头疼。
同时我还担心,如果我遵循这种新方法,我最终会得到一个充满返回查询方法的臃肿模型。
遇到这种情况你如何处理?
感谢您的帮助!
This is more a "Am i doing it right?" question.
I am using a MVC framework ( Zend Framework ) and was wondering if this is the best way to structure a model.
In this case i have an user groups model and, after coding for sometime, i realized that on my controllers i was repeating some stuff like a query to return visible groups.
$model->fetchAll( array( 'enabled = 1', 'visible = 1' ) );
In order to reduce code repetition, i created a method on this model that returns the query for visible groups.
Based on this same situation, i also created many other methods like hidden groups, disabled groups etc.
Is this the best way to avoid code duplication? My concern is that if i kept inserting that code on controllers i would have a big headache if the business rule for visible group changes in the future.
At the same time i am also concerned that, if i follow this new approach, i will end up with a bloated model full of methods to return queries.
How do you deal with this kind of situation?
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你正在编写查询来获取控制器中的数据,那么mvc的模型层在哪里?我会编写一个单独的层来获取、添加、编辑我的域模型等。你可以这里查看 php 中的存储库模式 。
if u are writing queries to fetch data in controllers where is Model layer of mvc then?. i would write a seperate layer that would get, add, edit etc. my domain models. u can have a look at repository pattern in php here.