douguizhuang8276 2018-09-11 08:19
浏览 62
已采纳

域映射 - 将模型从外部软件转换为内部结构

i'm currently working on a php-framework to abstract and make it fun and easy to work with an external software ($ES) my company consults. My approach is the hexagonal design pattern, which works great so far. My only concern is mapping (and where to map) entities from $ES to our internal structure.

Example: $externalSoftwareProduct (kind of a god class which handles everything) is mapped to $internalFrameworkProduct (and many other classes to split responsibilities). This happens in Repositories. In every repository method, i collect those entities from $ES and do

new $internalFrameworkProduct(some arguments here coming from 
$externalSoftwareProduct)

foreach of my collected entities which then gets returned. In those repositories there are only generic methods, like getById, getAll, you name it.

Now we use this framework in a customer project and extend those base classes with domain specific extension, like CustomerNameProductRepository. There you find domain specific methods like getProductsCustomerAlwaysNeeds and so on. At the end of those methods, we map the $internalFrameworkProduct to $customerSpecificProduct which holds data for easier access, which is needed. A method in this specific repository looks like this.

public function getProductsCustomerAlwaysNeeds()
{
   $dataStuff = parent::getSomeStuff();
   /** @var internalFrameworkProduct[] $products **/
   $products = magic();

   foreach($products as $product)
   {
     $customerProducts[] = $this->getCustomerSpecificProduct($product->getId());
   }

   return $customerProducts;
}

public function getCustomerSpecificProductById(int $productId)
{
   $externalSoftwareProduct = new externalSoftwareProduct($productId)
   $customerSpecificProduct = new CustomerSpecificProduct(some arguments here coming from $externalSoftwareProduct)

   return $customerSpecificProduct;
}

Now this works fine so far. The only problem is in unit tests. We are using phpunit + Mockery. In order to mock those new created instances we have to use mock(overload:externalSoftwareProduct) and mock(overload: CustomerSpecificProduct) which is always a pain (especially if you try to test this with multiple instances, which is needed from time to time).

How would you approach this? There must be a better way to get those 3 Pieces connected (externalSoftwareProduct, internalFrameworkProduct and CustomerSpecificProduct (which extends internalFrameworkProduct)).

I was thinking about using a factory for the CustomerSpecificProduct in order to just mock the factory and let it spit out my Products. But i feel like im overengineering such a simple task.

  • 写回答

1条回答 默认 最新

  • doushi5117 2018-09-11 17:38
    关注

    «.... mapping (and where to map) entities from $ES to our internal structure...»

    In the adapter you use to access the external software.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料