Comments (4)
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.
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.
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.
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)
- Repositories inside or outside Domain Services HOT 1
- Event Enriching and external changes to read-model data
- When, where and how to create Summary Events HOT 3
- Passing read models (value objects representing state) / domain service to aggregate methods HOT 6
- Unit testing value objects with internal datetime calculation HOT 14
- How to test application service command handlers dealing with read models? HOT 12
- Process Manager example with Symfony Messenger Command / Event Bus and ProophOS HOT 5
- Batch / Bulk operations handling multiple event-sourced aggregate roots HOT 3
- How to use factory methods on aggregates in CQRS - WRITE vs. READ model HOT 1
- How to keep read-models up-to-date when a name property was externally changed?
- How to upcast events with Prooph HOT 1
- Are CQRS commands part of the domain model? HOT 13
- Populate Projection with multiple tables HOT 2
- Where to call or pass a domain service? HOT 16
- How to implement the Equatable interface / Equals or SameValueAs method in value objects
- Domain Event Publisher for Doctrine Entities HOT 1
- Event Sourcing vs. Event-Driven Architecture (EDA)
- The repository pattern HOT 4
- Properties on Domain Events HOT 3
- PHP Command DTO with Symfony Constraints equivalent in Angular Forms HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from php-ddd.