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 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题