gpslab/specification-query

CQRS 架构中使用 Doctrine 规范作为查询的简单基础设施组件

v1.0.0 2017-06-23 11:38 UTC

This package is auto-updated.

Last update: 2024-08-29 03:27:16 UTC


README

Latest Stable Version Total Downloads Build Status Coverage Status Scrutinizer Code Quality SensioLabs Insight StyleCI License

CQRS 架构中的 Doctrine 规范查询

简单的基础设施组件,用于将 Doctrine 规范 作为 CQRS 架构中的查询。

安装

使用 Composer 非常简单,运行

composer require gpslab/specification-query

用法

您可以将规范用作简单的查询。

use GpsLab\Component\Query\Bus\HandlerLocatedQueryBus;
use GpsLab\Component\Query\Handler\Locator\DirectBindingQueryHandlerLocator;
use GpsLab\Component\Query\Specification\SpecificationQueryHandler;
use GpsLab\Component\Query\Specification\ObviousSpecificationQuery;

// register query handler in handler locator
$locator = new DirectBindingQueryHandlerLocator();
$locator->registerHandler(ObviousSpecificationQuery::class, [new SpecificationQueryHandler($em), 'handleSpecification']);

// create bus with query handler locator
$bus = new HandlerLocatedQueryBus($locator);


// specification for get contact with id = 123
$spec = Spec::eq('id', 123);

// cache the result by 1 hour
$modifier = Spec::cache(3600);

// make specification query
$query = new ObviousSpecificationQuery('AcmeDemo:Contact', $spec, $modifier);


// get contact
$contact = $query_bus->handle($query);

自定义查询

您可以为这种情况创建自定义查询。

class ContactWithIdentityQuery implements SpecificationQuery
{
    /**
     * @var int
     */
    private $id;

    /**
     * @param int $id
     */
    public function __construct($id)
    {
        $this->id = $id;
    }

    /**
     * @return string
     */
    public function entity()
    {
        return 'AcmeDemo:Contact';
    }

    /**
     * @return Specification
     */
    public function spec()
    {
        return Spec::eq('id', $this->id);
    }

    /**
     * @return ResultModifier|null
     */
    public function modifier()
    {
        return Spec::cache(3600);
    }
}

然后使用它

use GpsLab\Component\Query\Bus\HandlerLocatedQueryBus;
use GpsLab\Component\Query\Handler\Locator\DirectBindingQueryHandlerLocator;
use GpsLab\Component\Query\Specification\SpecificationQueryHandler;
use GpsLab\Component\Query\Specification\ObviousSpecificationQuery;

// register query handler in handler locator
$locator = new DirectBindingQueryHandlerLocator();
$locator->registerHandler(ContactWithIdentityQuery::class, [new SpecificationQueryHandler($em), 'handleSpecification']);

// create bus with query handler locator
$bus = new HandlerLocatedQueryBus($locator);


// make specification query
$query = new ContactWithIdentityQuery(123);


// get contact
$contact = $query_bus->handle($query);

许可证

此捆绑包受 MIT 许可证 的约束。请参阅文件 LICENSE 中的完整许可证。