GithubHelp home page GithubHelp logo

bindto's People

Contributors

agiuliano avatar cirpo avatar garak avatar giorrrgio avatar iainmckay avatar liuggio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bindto's Issues

Automatic entity/object lookups

I'd like to open a discussion on a new piece of functionality we need in our app. I provide a rationale and some possible solutions.

Feedback would be appreciated before I start implementing this.

Intention

To provide an automatic system for resolving object references to instances.

Our service layer accepts a lot of commands/dto's which contain references to entities or value objects in our domain. We have a lot of repetitive lookup code that we would like to get rid of.

It is essentially the functionality provided by Symfony ParamConverter but without the requirement to pass a Request object.

This feels very much like reinventing the wheel when there is a battle tested implementation that already exists but we've discussed it internally and don't see a way around it.

Basic Use

This would work similarly to PR #11 in that you would annotate a property on a DTO. The binder would then create an instance of the registered converter to produce an object instance. If the conversion process fails a violation would be added to the violation list.

The converter would accept the object identifier and convert it by whatever means necessary to an instance of that object or fail trying.

Possible Solutions

I provide two possible solutions for making this feature work.

Example 1

In this example the naming and documentation of the properties are as if they are already instances. It is the most natural but may be a little confusing for the developer dispatching the DTO to the service layer as we're passing in object identifiers but the documentation (and auto-complete) says instances.

<?php
class MyDTO
{

    public $name;

    /**
     * @var ValueObject\Country
     * @Lookup(converter="country_converter")
    */
    public $country;

    /**
     * @var Entity\User
     * @Lookup(converter="doctrine_converter")
    */
    public $user;
}

$myDto = new MyDTO();

$result = $binder->bind([
    'name' => 'Some Person',
    'country' => 1,
    'user' => 1024
], $myDto);

if ($result->isValid()) {
    echo($myDto->user->getName());
}

Example 2

In this example the naming and documentation of the properties are more obvious for the consumer as to what the expectations on the service consuming the DTO are. Accessing the converted objects is more obtuse and lacks support for autocomplete.

<?php
class MyDTO
{

    public $name;

    /**
     * @var int
     * @Lookup(converter="country_converter")
    */
    public $countryId;

    /**
     * @var int
     * @Lookup(converter="doctrine_converter")
    */
    public $userId;
}

$myDto = new MyDTO();

$result = $binder->bind([
    'name' => 'Some Person',
    'countryId' => 1,
    'userId' => 1024
], $myDto);

if ($result->isValid()) {
    echo($myDto->get('user')->getName());
}

Converters

A converter would follow a very simple interface and would be registered with the Binder to be made available.

<?php
interface ConverterInterface
{

    /**
     * @param mixed $input
     * @param array options
     * @throws ConverterException
     * @returns mixed
    */
    public function convert($input, array $options);
}

Php dependency

I noticed that bindto depends on "phpunit/phpunit": "^5.0@dev", wich in turn depends on php 5.6 whereas binddto declares to depend on php >= 5.5
Which php version win?
Do we have to upgrade the php requirements or downgrade the phpunit dependency?

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.