Giter VIP home page Giter VIP logo

Comments (4)

yvoyer avatar yvoyer commented on July 17, 2024

Or ContractType is an interface that is in charge of creating the Contract.

inteface ContractType
{
    public function createContract(): EmploymentContract;
}

final class FixedContractType implements ContractType
{
    public function createContract(): EmploymentContract 
    {
        return new FixedContract($this->startDate, $this->endDate)
    }
}

final class PermanentContractType implements ContractType
{
    public function createContract(): EmploymentContract 
    {
        return new PermanentContract($this->startDate)
    }
}

final class FixedContract implements EmploymentContract extends AggregateRoot {}
final class PermanentContract implement EmploymentContract extends AggregateRoot {}

Each EmployementContract would generate distinct event FixedContractSigned and PermanentContractSigned.

Usually when using the Type concept, my development leads to a factory method, while the type is an interface. It allows unit tests to use NullType instead of a mock when needed, and adding new types is easier, as it do not change logics from other type. Or it do not lead to a switch case/ if-else.

This way the invariant are enforced upon the creation of the type. Would it be logical for your case?

from php-ddd.

webdevilopers avatar webdevilopers commented on July 17, 2024

Sorry for the late response @yvoyer . I had to think this through for a while.
I really like this approach. But currently each "sign" method has about 20 parameters and only differs passing a date for $endDate or NULL.
Refactoring the current EmploymentContract Aggregate Root would not be "worth it". The project is almost finished.

But I guess the next time I will try your approach from scratch.

Could I still use a single "Employment Contract Event Store"?

$whateverContract = $sharedContractTypeRepository->ofId($employmentContractId);
$whateverContract->modify($modifyContractCommand);

Or how would you handle the different types "fixed_term" and "permanent" here?

from php-ddd.

yvoyer avatar yvoyer commented on July 17, 2024

The same aggregate could still manage both types:

final class EmploymentContract extends AggregateRoot
{
    protected function onFixedContractWasSigned(FixedContractWasSigned $event): void
    {
        // ...
    }

    protected function onPermanentContractWasSigned(PermanentContractWasSigned $event): void
    {
        // ...
    }

    public static function signed(
        EmploymentContractId $anId,
        ContractType $aContractType
    ) {
        $aggregate = ...;
        $aggregate->applyEvents($aContractType->onSigning($anId));
    }
}

interface ContractType
{
    /**
     * @param EmploymentContractId $id
     * @return DomainEvent[] Events relative to the contract type
     */
    public function onSigning(EmploymentContractId $id): array;
}

final class FixedContractType implements ContractType
{
    private $startDate;
    private $endDate;

    public function __construct(\DateTimeInterface $startDate, \DateTimeInterface $endDate)
    {
        $this->startDate = $startDate;
        $this->endDate = $endDate;
    }

    public function onSigning(EmploymentContractId $id): array
    {
        return [new FixedContractWasSigned($id, $this->startDate, $this->endDate)];
    }
}

final class PermanentContractType implements ContractType
{
    private $startDate;

    public function __construct(\DateTimeInterface $startDate)
    {
        $this->startDate = $startDate;
    }

    public function onSigning(EmploymentContractId $id): array
    {
        return [new PermanentContractWasSigned($id, $this->startDate)];
    }
}

$repos = new EmploymentContractEventStore();
$fixedContract = EmploymentContract::signed(
    $fixedContractId = new EmploymentContractId(),
    new FixedContractType(
        new DateTimeImmutable('2000-01-01'),
        new DateTimeImmutable('2000-12-31')
    )
);
$repos->save($fixedContract);
$repos->getContract($fixedContractId); // Configured as fixed

$permanentContract = EmploymentContract::signed(
    $permanentContractId = new EmploymentContractId(),
    new PermanentContractType(new DateTimeImmutable('2000-01-01'))
);
$repos->save($permanentContract);
$repos->getContract($permanentContractId); // Configured as permanent

If you still need 2 aggregates returned by the same repository, wrap both repos in a single class (using the composite pattern). That way, when fetching an aggregate of type 1, use repos 1, when fetching aggregate of type 2, use repos 2.

Anything is feasible

from php-ddd.

pMononoke avatar pMononoke commented on July 17, 2024

You cannot establish the type of contract without a period of time.
ContractType should only be built with EmploymentPeriod.

EmploymentPeriod::withFixedDuraton ($start, $end)
EmploymentPeriod::withVariableDuraton ($start)

should expose a method

EmploymentPeriod->isFixedDuration ();

ContactType accepts EmploymentPeriod in the constructor and determines its own type based on the EmploymentPeriod-> isFixedDuration () method.
ContractType should be a simple object, the business roules for dates are in EmploymentPeriod

ContractType::fromEmplymentPeriod($emplymentPeriod);
ContractType->isFixedTerm(); // reflect internal status of EmploymentPeriod->isFixedDuration ()

your aggregate accepts EmplymentPeriod and internally determines the contactType

EmploymentContract::sign(EmploymentContractId $anId, EmploymentPeriod $anEmploymentPeriod);

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.