(数据访问)服务类的结构是什么
我了解到我应该使用服务类将实体持久保存到数据库中,而不是将此类逻辑放入模型/控制器中。我目前的服务类类似于
class Application_DAO_User {
protected $user;
public function __construct(User $user) {
$this->user = $user
}
public function edit($name, ...) {
$this->user->name = $name;
...
$this->em->flush();
}
}
我想知道这是否应该是服务类的结构?其中服务对象代表实体/模型?或者也许我应该在每次想要进行编辑时传递一个 User 对象,例如
public static function edit($user, $name) {
$user->name = $name;
$this->em->flush();
}
我正在使用 Doctrine 2 & Zend Framework,但这不重要
I learnt that I should be using service classes to persist entities into the database instead of putting such logic in models/controllers. I currently made my service class something like
class Application_DAO_User {
protected $user;
public function __construct(User $user) {
$this->user = $user
}
public function edit($name, ...) {
$this->user->name = $name;
...
$this->em->flush();
}
}
I wonder if this should be the structure of a service class? where a service object represents a entity/model? Or maybe I should pass a User object everytime I want to do a edit like
public static function edit($user, $name) {
$user->name = $name;
$this->em->flush();
}
I am using Doctrine 2 & Zend Framework, but it shouldn't matter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你应该首先考虑你想对用户对象做什么。例如,如果您只想创建、更新和删除用户记录(CRUD),我可以想象这种类型的 API:
我将方法分离到实体管理器,您可以创建如下所示的内容或完全跳过分离的方法。使用这些方法,您可以进行额外的检查(例如,如果您想持久化,可以检查对象是否已经持久化)。
I think you should first consider what you'd like to do with the user objects. For example if you only want to create, update and delete user records (CRUD) I can imagine this type of API:
I seperated the methods to the entity manager, you can create something like below or skip the seperated methods at all. With these methods you could do additional checkups (for example if you want to persist there can be a check to look if the object is already persisted).