gpslab/specification-query
Composer 安装命令:
composer require gpslab/specification-query
包简介
The simple infrastructure component for use a Doctrine specification as query in CQRS architecture
README 文档
README
Doctrine Specification as query in CQRS architecture
The simple infrastructure component for use a Doctrine Specification as query in CQRS architecture.
Installation
Pretty simple with Composer, run:
composer require gpslab/specification-query
Usage
You can use Specifications as a simple 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);
Custom query
You can create custom query for this case.
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); } }
And use it
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);
License
This bundle is under the MIT license. See the complete license in the file: LICENSE
统计信息
- 总下载量: 26
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-05-26