Skip to content

Creating aggregates and enforcing invariants spanning multiple aggregates #39

Description

@webdevilopers

Given is an event-sourced EmploymentContract holding an EmploymentPeriod with a start and end date.
It is then possible to "deploy employees of a contract to other work places".
But the DeploymentPeriod must be inside the EmploymentPeriod.

We want to keep our aggregates small and decided to make Deployment a separated event-sourced aggregate root (A+ES) too.

In a future solution we would create a separate microservice and make them event-driven. Then the could suggest a deployment, check the business rule and eventually fire a `DeploymentRejected" event or similar.

For now we are using CQRS and a deployEmployeeToWorkplace is handled by the command handler application service.

Currently we are thinking of three approaches ("Deployment" and "Solution" 1-3):

Currently we prefer solution 1 which creates the Deployment thought a factory method on the EmploymentContract which already will hold the EmploymentPeriod.
All rules - actually they are not invariants since this does not concern the "transaction" of the Deployment A+ES - can be validated here and return the Deployment.

The only thing we ask ourselves:
Could and should we prevent other team developers from skipping the factory method and calling the named constructor on Deployment directly?

final class Deployment1
{
    public static function with(DeploymentId $anId, EmploymentContractId $anEmploymentContractId,
                                DeploymentPeriod $aPeriod): Deployment
    {
        // No period range validation, final period was already validated and passed
    }
}

Should this be done in your code base in general or should these constructors just be available as is and the rules explained in pair-programming. What are your thoughts?

Final thoughts:
Whatever is the best solution, we are thinking of moving the responsibility away from the handler into a domain factory.

Similar topics:

Discussion started here:

Interesting reads:

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions