【发布时间】:2019-08-06 10:42:43
【问题描述】:
问题
数据传输对象 (DTO) 能否引用域模型的值对象 (VO)?
上下文
在我的域中,我有一个从集合中导入聚合的导入器。该集合由进口商所依赖的收集器构建的 DTO 组成。由于导入器和收集器都是我的域的服务(接口),DTO 可以引用域值对象,还是我应该坚持使用原语并仅在处理集合(聚合的构建)时将它们转换为值对象?
收集器实现,其中构建了由域模型中的值对象构成的 DTO
<?php
/**
* i-MSCP Patcher plugin
*
* @author Laurent Declercq <l.declercq@nuxwin.com>
* @copyright (C) 2019 Laurent Declercq <l.declercq@nuxwin.com>
* @license i-MSCP License <https://www.i-mscp.net/license-agreement.html>
*/
/**
* @noinspection
* PhpUnhandledExceptionInspection
* PhpDocMissingThrowsInspection
*/
declare(strict_types=1);
namespace iMSCP\Plugin\Patcher\Infrastructure\Domain\Service\Component\Importer;
use iMSCP\Plugin\Patcher\Domain\Model\Component\ComponentBuild;
use iMSCP\Plugin\Patcher\Domain\Model\Component\ComponentName;
use iMSCP\Plugin\Patcher\Domain\Model\Component\ComponentVersion;
use iMSCP\Plugin\Patcher\Domain\Service\Component\Importer\ComponentCollector;
use iMSCP\Plugin\Patcher\Domain\Service\Component\Importer\DTO\ComponentDTO;
use iMSCP\Plugin\Patcher\Domain\Service\Component\Importer\DTO\ComponentDTOCollection;
use iMSCP_Config_Handler_File as MergedConfig;
use iMSCP_Plugin_Manager as PluginManager;
use RuntimeException;
use Throwable;
/**
* Class DefaultComponentCollector
* @package iMSCP\Plugin\Patcher\Infrastructure\Domain\Service\Component\Importer
*/
class DefaultComponentCollector implements ComponentCollector
{
/**
* @var MergedConfig
*/
private $mergedConfig;
/**
* @var PluginManager
*/
private $pluginManager;
/**
* DefaultComponentCollector constructor.
*
* @param MergedConfig $mergedConfig
* @param PluginManager $pluginManager
*/
public function __construct(
MergedConfig $mergedConfig, PluginManager $pluginManager
)
{
$this->mergedConfig = $mergedConfig;
$this->pluginManager = $pluginManager;
}
/**
* @inheritDoc
*/
public function collect(ComponentDTOCollection $collection): void
{
try {
// Core
$collection->add(new ComponentDTO(
ComponentName::fromString('core'),
ComponentVersion::fromString($this->mergedConfig['Version']),
ComponentBuild::fromString($this->mergedConfig['Build'])
));
// Plugins
$this->collectComponentsFromCorePluginManager($collection);
} catch (Throwable $e) {
throw new RuntimeException(sprintf(
"Couldn't collect list of components: %s", $e->getMessage()
), $e->getCode(), $e);
}
}
/**
* Collects components from the i-MSCP core plugin manager.
*
* @param ComponentDTOCollection $collection
* @return void
*/
private function collectComponentsFromCorePluginManager(
ComponentDTOCollection $collection
): void
{
$pluginList = $this->pluginManager->pluginGetList('all', false);
foreach ($pluginList as $pluginName) {
$pluginInfo = $this->pluginManager
->pluginGetInfo($pluginName)
->toArray();
$componentDTO = new ComponentDTO(
ComponentName::fromString($pluginInfo['name']),
ComponentVersion::fromString($pluginInfo['version']),
ComponentBuild::fromString((string)$pluginInfo['build'])
);
$collection->add($componentDTO);
}
}
}
【问题讨论】:
标签: collections domain-driven-design dto value-objects