Giter VIP home page Giter VIP logo

Comments (8)

Ocramius avatar Ocramius commented on August 16, 2024 1

fromArray_v2 all the way: I avoid public function __construct in my systems, as data structures have multiple ways of being instantiated. Reusing private logic is fine, but the ctor should really not be public.

from php-ddd.

webdevilopers avatar webdevilopers commented on August 16, 2024

Thanks for the feedback! Same here regarding private and named ctors. The example was just for demonstration.

At the bottom line do you even think about other developers of your team using the fromArray_v2 method to create invalid data? It maybe sounds a little bit paranoid though. :)

from php-ddd.

Ocramius avatar Ocramius commented on August 16, 2024
        $self = new self();         $self->quantity = $array['quantity'];         $self->subtotal = $array['subtotal'];         $self->total = $array['total'];         return $self;

This bit can be in any private method, if duplicate.

from php-ddd.

webdevilopers avatar webdevilopers commented on August 16, 2024

Sorry, but could you explain what you mean? I missed the point. :/

from php-ddd.

Ocramius avatar Ocramius commented on August 16, 2024

I took a snippet from the above code: you can make it a private method if validation duplication is a concern.

from php-ddd.

webdevilopers avatar webdevilopers commented on August 16, 2024

But how would you init the object then for instance in an event like this?

    public function customPrices(): CustomPrices
    {
        if (null === $this->customPrices) {
            $this->customPrices = new CustomPrices(array_map(function(array $rate) {
                return CustomPrice::fromArray($rate);
            }, $this->payload['customPrices']));
        }

        return $this->customPrices;
    }

from php-ddd.

Ocramius avatar Ocramius commented on August 16, 2024

I'd do that at initialisation, not when that data is requested. Or even better: keep it as value objects as much as possible as long as it's in memory

from php-ddd.

webdevilopers avatar webdevilopers commented on August 16, 2024

Thanks for explaining!

BTW the example was taken from the older version of @prooph event store. In the current version they skipped the serialization of Domain Event value objects:

final class Basket extends AggregateRoot
{
    public static function startShoppingSession(
        ShoppingSession $shoppingSession,
        BasketId $basketId)
    {
        //Start new aggregate lifecycle by creating an "empty" instance
        $self = new self();

        //Record the very first domain event of the new aggregate
        //Note: we don't pass the value objects directly to the event but use their
        //primitive counterparts. This makes it much easier to work with the events later
        //and we don't need complex serializers when storing events.
        $self->recordThat(ShoppingSessionStarted::occur($basketId->toString(), [
            'shopping_session' => $shoppingSession->toString()
        ]));

        //Return the new aggregate
        return $self;
    }

If I'm right either way the value object stays in memory until the apply method on the AR.

from php-ddd.

Related Issues (20)

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.