Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/Added wakeup method to create instances without call explicitly the constructor #26

Merged
merged 1 commit into from
Mar 5, 2021

Conversation

othercodes
Copy link
Owner

Creating an instance with new keyword will execute the logic inside the __constructor() but using ::wakeup() will create the instance, and fill the properties without executing the logic in the __constructor().

So, having and aggregate Order that registers the domain event OrderCreated in the constructor:

final class Order implements Aggregate
{
    use IsAggregate;

    private OrderId $id;
    private PaymentStatus $status;
    private array $orderLines;

    public function __construct(OrderId $id, PaymentStatus $status, array $orderLines)
    {
        $this->initialize([$id, $status, $orderLines]);
        $this->registerDomainEvent(new OrderCreated($this->id()->value()));
    }

    protected function invariantEachOrderLineMustBeTypeOfOrderLine(): bool
    {
        foreach ($this->orderLines as $orderLine) {
            if (!($orderLine instanceof OrderLine)) {
                throw new InvariantViolation("All order lines must be type of OrderLine");
            }
        }

        return true;
    }

    public static function create(OrderId $id, PaymentStatus $status, array $orderLines): self
    {
        return new self($id, $status, $orderLines);
    }
}

we can do:

// OrderCreated domain event is register since is register in __constructor().
$a = new Order(...) 

// aggregate constructor is NOT called so the domain event is not registered,
// initialize() is called so the invariants are also checked.
$a = Order::wakeup(...) 

This makes the usage of new more semantic as we are creating a REAL new instance of the Aggregate not just creating an object, we may need to register some domain events on the creation of the Aggregate.

@othercodes othercodes requested a review from segoddnja March 5, 2021 10:31
@othercodes othercodes self-assigned this Mar 5, 2021
@othercodes othercodes added the enhancement New feature or request label Mar 5, 2021
@codecov
Copy link

codecov bot commented Mar 5, 2021

Codecov Report

❗ No coverage uploaded for pull request base (develop@7daec89). Click here to learn what that means.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##             develop      #26   +/-   ##
==========================================
  Coverage           ?   91.29%           
  Complexity         ?      200           
==========================================
  Files              ?       26           
  Lines              ?      448           
  Branches           ?        0           
==========================================
  Hits               ?      409           
  Misses             ?       39           
  Partials           ?        0           
Flag Coverage Δ Complexity Δ
unittests 91.29% <0.00%> (?) 0.00% <0.00%> (?%)

Flags with carried forward coverage won't be shown. Click here to find out more.


Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7daec89...0945023. Read the comment docs.

@othercodes othercodes merged commit 6fdbf10 into develop Mar 5, 2021
@othercodes othercodes deleted the feature/WakeupMethodToAvoidConstructorCall branch March 5, 2021 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants