Code Monkey home page Code Monkey logo

specification-query's Introduction

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

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

specification-query's People

Contributors

peter-gribanov avatar peter279k avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

specification-query's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.