模型作为 Zend Framework 和服务层中的纯数据对象
将 ZF 中的模型视为纯数据对象(即具有一堆属性和 getter/setter 的类实例)已变得很常见。
class Model_User {
public $id;
public $name;
...
}
我想知道是否有意义,例如,在控制器中实例化这样的对象并将其传递给服务层,或者服务层本身是否应该负责用于实例化这些对象...您不会直接在控制器中实例化 Zend_DbTable
类,因此在控制器中实例化 Model_User
并将其传递给是否有意义一项服务?
It has become common to treat models in ZF as pure data objects, i.e. a class instance with a bunch of attributes and getters/setters.
class Model_User {
public $id;
public $name;
...
}
What I'm wondering is whether or not it makes sense to, for example, instantiate such an object in a controller and pass it to a service layer, or whether or not the service layer should itself be responsible for instantiating these objects... You would not instantiate a Zend_DbTable
class directly in the controller, so does it make much sense to instantiate a Model_User
in a controller to pass it to a service?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为这样做有什么问题。数据传输对象 (DTO) 旨在在层和/或模块之间传递信息。在我看来,这与使用数组没有什么不同,而且它肯定比使用数组更容易。
这是关于 DTO 的另一场讨论 - 什么是数据传输对象?
以及 Martin 对 DTO 的定义- http://martinfowler.com/eaaCatalog/dataTransferObject.html
I don't see a problem with doing it this way. Data Transport Objects (DTO) are meant to pass information between layers and/or modules. In my opinion this is no different that using arrays and it certainly is easier than using arrays.
Here is another discussion over DTOs - What is Data Transfer Object?
And Martin's definition on DTOs - http://martinfowler.com/eaaCatalog/dataTransferObject.html