微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将业务逻辑与PHP Doctrine 2分开

我使用symfony 2.3和PHP doctrine 2.

该计划有以下型号:

>实体订单 – 典型的客户订单
> entity BadOrderEntry(fields:id,order – 与Order的单向一对一关系,createdAt)
>工厂BadOrderEntryFactory用于创建实体BadOrderEntry
>存储库BadOrderEntryRepository,用于实体BadOrderEntry的搜索方法
> manager BadOrderEntryManager用于保存/编辑/删除实体BadOrderEntry的方法
> AND MAIN CLASS BadOrderList – 错误订单列表,此类代码

private $factory;
private $repository;
private $manager;

public function __construct(
    BadOrderEntryFactory $f,BadOrderEntryRepository $r,BadOrderEntryManager $m
) {
    $this->factory = $f;
    $this->repository = $r;
    $this->manager = $m;
}

public function has(Order $order)
{
    return $this->repository->existsByOrder($order);
}

public function add(Order $order)
{
    if (! $this->has($order)) {
        $entry = $this->factory->create($order);
        $this->manager->save($entry);
    }
}

public function remove(Order $order)
{
    $entry = $this->repository->findOneByOrder($order);
    if ($entry !== null) {
        $this->manager->delete($entry);
    }
}

我真的很喜欢这门课的设计.我想了很多.
一切都很美好.但!有一个问题:必须在事务中执行方法添加删除操作.

PHP Docrine 2中的事务代码如下所示:

<?PHP
$em->getConnection()->beginTransaction();
try {
    //... do some work
    $em->getConnection()->commit();
} catch (Exception $e) {
    $em->getConnection()->rollback();
    throw $e;
}

但是如何在BadOrderList中调用代码

我花了很多时间并根据数据库(相应的PHP Doctrine 2)删除,并再次创建它?
现在依赖是隐藏在类BadOrderEntryRepository和BadOrderEntryManager中.

如何在类BadOrderList中隐藏对事务机制的依赖?

在我们讨论之后,我会回答你的问题.
问题实际上不是“如何隐藏类BadOrderList中对事务机制的依赖?”,而是如何将模型与持久层分离? (在特定情况下的Doctrine2).

我尝试用一​​些代码来说明我的建议

class BadOrderEntry
// Bad - is too bad word to describe an order here. Why is it bad? Is it Declined? Cancelled?
{
   private $order;
   // some code
}
class BadOrderEntryFactory 
{ 
   // If there is not to much processing to build BadOrderEntry better use factory method like BadOrderEntry::fromOrder($order); 
}
class BadOrderEntryRepository 
{ 
   // here is some read model 
}
class BadOrderEntryManager  
// ITS a part of our model and shouldn't be coupled to ORM
{
  public function save(BadEntry $be) 
  {
    // some model events,model actions
    $this->doSave($be); // here we should hide our storage manipulation
  }

  protected function doSave($be) // it can be abstract,but may contain some basic storage actions  
  { 
  }

  // similar code for delete/remove and other model code
}
class ORMBadOrderEntryManager extends BadOrderEntryManager 
// IT'S NOT the part of your model. There is no business logic. There is only persistent logic and transaction manipulation
{ 
  protected $entityManager;

  // some constructor to inject doctrine entitymanager

  protected doSave($be)
  {
    $em = $this->entityManager;
    $em->getConnection()->beginTransaction(); // suspend auto-commit
    try {
      $em->persist($be);
      $em->flush();
      $em->getConnection()->commit();
    } catch (Exception $e) {
      $em->getConnection()->rollback();
      throw $e;
    }
  }
}
// You can also implement ODMBadOrderEntryManager,MemcacheBadOrderEntryManager etc.

因此,如果我们谈论目录结构,您的所有模型都可以移出捆绑并在任何地方使用.你的Bundle结构如下:

BadEntryBundle
|
+ Entity
| |
| --- BadOrderEntryEntity.PHP
|
+ ORM
| |
| --- ORMBadOrderEntryManager.PHP

然后,您只需将ORMBadOrderEntryManager注入BadOrderEntryList

原文地址:https://www.jb51.cc/php/134193.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐