php提交订单业务逻辑,将业务逻辑与PHP Doctrine 2分开

在我们讨论之后,我会回答你的问题.

问题实际上不是“如何隐藏类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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值