devatmaliance/repository

该软件包最新版本(v3.11)没有可用的许可证信息。

v3.11 2024-05-20 06:23 UTC

README

用于使用公共接口添加、更新和从大师中删除记录的仓库

使用 RepositoryService

  1. 创建

$service = new RepositoryService( $this->request->rawBody, new RepositoryYii, new ActionReceiverDTO, new RepositoryEntity, new ServiceReceiverMapperDTO )

  1. $this->request->rawBody = json new RepositoryYii - 类,执行所有必要的同步操作以同步创建、更新、删除3个最重要的点,以便理解如何使用此类

    • public const SCENARIO = 'master_model_changes';
    • private ActionDTOInterface $actionDTO;
    • private RepositoryEntityInterface $repositoryEntity;

SCENARIO = 'master_model_changes' - 用于防止触发可能已绑定到模型之一的操作,操作通过API请求向添加、更改或删除记录发送请求,如果此功能通过表单或控制台使用,则使用该功能。

private ActionDTOInterface $actionDTO;

这是我们DTO,它在接收到JSON文件时被填充,也传递给initDTO,ServiceReceiverMapperDTOInterface的第二个参数,该参数用于映射字段到我们的模型。

示例

$this->actionDTO->initDTO($this->body, $this->mapperDTO);

实体类

  • 库类,其中列出了当前的所有模型,以便我们可以在entity中使用它们在发送到JSON和微服务处理时。这里我们列出了所有DTO,我们需要根据主表更改字段,如果不指定,则将使用默认字段

class ServiceReceiverMapperDTO implements ServiceReceiverMapperDTOInterface {

public function mapDtos(): array
{
   return [
        Entity::DEVICE => DeviceDTO::class
    ];
}

public function getMapDto(string $entity): array
{
    $dto = $this->mapDtos()[$entity] ?? [];
    return $dto ? ['fields' => $dto] : [];
}

}

public function initDTO(\stdClass $source, ?ServiceReceiverMapperDTOInterface $mapperDTO): void { $dtoMap = isset($source->entity) ? $mapperDTO->getMapDto($source->entity) : []; $this->loadSource($source, $dtoMap);

    if (!$this->validate()) {
        throw new ModelNotValidateException($this);
    }
}

private RepositoryEntityInterface $repositoryEntity - 这里我们填充数组Entity => Model,我们的微服务将与它们一起工作。例如:我们仅同步两个实体并将它们关联到服务中的模型,以便执行操作。

class RepositoryEntity implements RepositoryEntityInterface {

public function entities(): array
{
    return [
        Entity::DEVICE => new Device(),
        Entity::FILTERS => new Filters(),
    ];
}

}

  1. $repositoryService->execute() - 调用此函数,发生记录到我们的数据库并返回包含模型字段的数组。