CommandHandler - How to instantiate new aggregate with parameterized constructor #901
Answered
by
rasmus
dbrassard-genetec
asked this question in
Q&A
ContextWe have a Command that creates a new aggregate. Examplepublic class CreateMyAggregateCommand : Command<MyAggregate, MyAggregateId, IExecutionResult>
{
public CreateMyAggregateCommand (MyAggregateId id) : base(id)
{
}
}
public class CreateMyAggregateCommandHandler : CommandHandler<MyAggregate, MyAggregateId, IExecutionResult, CompleteProductUpdateCommand>
{
public override Task<IExecutionResult> ExecuteCommandAsync(MyAggregate aggregate, CreateMyAggregateCommand command, CancellationToken cancellationToken)
{
// Here the aggregate instance is constructed using the default constructor.
return Task.FromResult(executionResult);
}
}Questions
|
Answered by
rasmus
Feb 24, 2022
Replies: 1 comment 1 reply
|
Since you really don't know if the aggregate is already created at the time or it will be created by some competing thread I don't think doing it in the command handler is a good idea. Any information you would want to pass to the aggregate could as easily be passed via any methods and if you want some special factory for your aggregates, create a custom implementation for |
1 reply
Answer selected by
dbrassard-genetec
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since you really don't know if the aggregate is already created at the time or it will be created by some competing thread I don't think doing it in the command handler is a good idea. Any information you would want to pass to the aggregate could as easily be passed via any methods and if you want some special factory for your aggregates, create a custom implementation for
IAggregateFactory