(数据访问)服务类的结构是什么

发布于 2024-10-08 23:16:19 字数 616 浏览 0 评论 0原文

我了解到我应该使用服务类将实体持久保存到数据库中,而不是将此类逻辑放入模型/控制器中。我目前的服务类类似于

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心碎的声音 2024-10-15 23:16:19

我认为你应该首先考虑你想对用户对象做什么。例如,如果您只想创建、更新和删除用户记录(CRUD),我可以想象这种类型的 API:

<?php
public function create (array $data = null)
{
    $user = new User($data);
    $this->_persist($user)
         ->_flush();

    return $user;
}

public function update (User $user, array $data)
{
    foreach ($data as $name => $value) {
        $user->$name = $value;
    }

    $this->_flush();

    return $user;
}

public function delete (User $user)
{
    $this->_remove($user)
         ->_flush();
}

我将方法分离到实体管理器,您可以创建如下所示的内容或完全跳过分离的方法。使用这些方法,您可以进行额外的检查(例如,如果您想持久化,可以检查对象是否已经持久化)。

protected function _persist ($obj)
{
    $this->_em->persist($obj);
    return $this;
}

protected function _detach ($obj)
{
    $this->_em->detach($obj);
    return $this;
}

protected function _remove ($obj)
{
    $this->_em->remove($obj);
    return $this;
}

protected function _flush ()
{
    $this->_em->flush();
    return $this;
}

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:

<?php
public function create (array $data = null)
{
    $user = new User($data);
    $this->_persist($user)
         ->_flush();

    return $user;
}

public function update (User $user, array $data)
{
    foreach ($data as $name => $value) {
        $user->$name = $value;
    }

    $this->_flush();

    return $user;
}

public function delete (User $user)
{
    $this->_remove($user)
         ->_flush();
}

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).

protected function _persist ($obj)
{
    $this->_em->persist($obj);
    return $this;
}

protected function _detach ($obj)
{
    $this->_em->detach($obj);
    return $this;
}

protected function _remove ($obj)
{
    $this->_em->remove($obj);
    return $this;
}

protected function _flush ()
{
    $this->_em->flush();
    return $this;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文