Zend模型架构 [英] zend models architecture

查看:15
本文介绍了Zend模型架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在一个数据库中有两个表:projects 和 users.我创建了两个扩展 Zend_Db_Table_Abstract 的模型:Model_DbTable_UsersModel_DbTable_Projects.

Let's say I have two tables in a database: projects and users. I create two models, that extend Zend_Db_Table_Abstract: Model_DbTable_Users and Model_DbTable_Projects.

现在,在 Model_DbTable_Users 类中创建 Model_DbTable_Projects 的实例是否是一个好的模式?换句话说:是否可以在此模型中放置任何逻辑,还是应该创建另一个使用 Model_DbTable_UsersModel_DbTable_Projects 的类?

Now, is it a good pattern to create an instance of Model_DbTable_Projects inside the Model_DbTable_Users class ? In other words: is it OK to put any logic in this model, or should I create another class, that uses Model_DbTable_Users and Model_DbTable_Projects?

我习惯将所有逻辑放在模型中,扩展 Zend_Db_Table_Abstract,但在大型项目中它会使代码非常不干净.那么,你能给我任何关于模型架构的建议吗(文章中的链接会很棒!).

I use to put all the logic in models, that extend Zend_Db_Table_Abstract, but in large projects it can make code very unclean. So, can you give me any advice on models architecture(links on articles would be great!).

推荐答案

我是 Zend Framework 项目 1.0 版的项目负责人.我的贡献主要是在 Zend_Db 组件中.

I was the project lead for the Zend Framework project through version 1.0. My contributions were mainly in the Zend_Db component.

我经常建议人们应该使用域模型 模式并避免 贫血领域模型 反模式.请记住,表不是模型.

I frequently advise that people should use the Domain Model pattern and avoid the Anemic Domain Model antipattern. Remember that a Table is not a Model.

您的模型是用于封装业务逻辑的代码的类(不扩展基类).模型和表之间的关系不是 IS-A,而是 HAS-A(或 HAS-MANY).模型将数据库持久性视为实现细节.模型的使用者应该对您的数据库结构一无所知(这允许您在不更改模型界面的情况下更改数据库结构).

Your Model is a class (extending no base class) for code that encapsulates your business logic. The relationship between a Model and a Table isn't IS-A, it's HAS-A (or HAS-MANY). The Model treats database persistence as an implementation detail. The consumer of a Model should have no clue about your database structure (this allows you to change database structure without changing the Model's interface).

我基本上是在重复我对 Zend 框架中的模型的回答.

I'm basically repeating the answer I gave to Models in the Zend Framework.

这里还有一些阅读:

这篇关于Zend模型架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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