Replies: 4 comments
-
Have you like tried this? public class BuyTradeManagerSaga : AggregateSaga<BuyTradeManagerSaga, TradeSagaId, TradeSagaLocator>,
ISagaIsStartedBy<TradingEngine.Order.Transaction, TransactionId, BuyTransactionStartedEvent>
{
private TransactionId transactionId;
private OrderBookId orderBookId;
private PortfolioId portfolioId;
private long totalItems;
private long pricePerItem;
public BuyTradeManagerSaga(TradeSagaId id) : base(id)
{
}
public Task HandleAsync(IDomainEvent<Transaction, TransactionId, BuyTransactionStartedEvent> domainEvent, ISagaContext sagaContext, CancellationToken cancellationToken)
{
transactionId = domainEvent.AggregateEvent.TransactionId;
orderBookId = domainEvent.AggregateEvent.OrderBookId;
portfolioId = domainEvent.AggregateEvent.PortfolioId;
pricePerItem = domainEvent.AggregateEvent.PricePerItem;
totalItems = domainEvent.AggregateEvent.TotalItems;
// Emit(domainEvent.AggregateEvent); <--- can't do this
Publish(new ReserveCashCommand(portfolioId, transactionId, totalItems * pricePerItem));
return Task.CompletedTask;
}
//public void Apply(BuyTransactionStartedEvent @event)
//{
//transactionId = @event.TransactionId;
// orderBookId = @event.OrderBookId;
//portfolioId = @event.PortfolioId;
// pricePerItem = @event.PricePerItem;
// totalItems = @event.TotalItems;
//}
} |
Beta Was this translation helpful? Give feedback.
-
How would the state of saga be reconstructed for subsequent events? It has to be via event sourcing or snapshotting, neither of which is happening when HandleAsync finishes. |
Beta Was this translation helpful? Give feedback.
-
I could be wrong, but from my understanding, Saga basically works like an Aggregate+ and the pretty part is because it is not attached to IApply, it can hold the state where it is altered by multiple source of aggregate, @rasmus might have to jump in and correct me. When ever you load a uncompleted saga, it will always replay the things that happened to it However this require's you to NOT mark the saga as completed, and always load the correct saga via the saga id. |
Beta Was this translation helpful? Give feedback.
-
Saga by its nature responds to events and can issue new commands. If you
were to replay it back, those commands would be reissued and can
potentially take different logic path (dates logic). This is the same
reason aggregates split up methods that process commands vs Apply methods
that take events to state change the aggregate. They are devoid of any
logic and are basically state accumulators.
If you want to rematerialize saga there really only 2 options. Snapshot
serialize it after it processes event /command its handing (this is what
axon framework does). Alternatively use event sourcing to replay events and
derive current state.
Correct me if I'm wrong, EventFlow uses the later strategy
…On Thu., Aug. 1, 2019, 2:29 a.m. Kori, ***@***.***> wrote:
I could be wrong, but from my understanding, Saga basically works like an
Aggregate+ and the pretty part is because it is not attached to IApply, it
can hold the state where it is altered by multiple source of aggregate,
@rasmus <https://github.com/rasmus> might have to jump in and correct me.
When ever you load a *uncompleted* saga, it will always *replay* the
things that happened to it
Loading
<https://github.com/eventflow/EventFlow/blob/70619f11fd8d2c0d2425794990dfe90d6e7440a8/Source/EventFlow/Sagas/DispatchToSagas.cs#L161>
Replay
<https://github.com/eventflow/EventFlow/blob/70619f11fd8d2c0d2425794990dfe90d6e7440a8/Source/EventFlow/Sagas/SagaUpdater.cs#L53>
However this require's you to *NOT* mark the saga as completed, and
always load the correct saga via the saga id.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#670?email_source=notifications&email_token=AAINFWAUXQ7BSCC64OVP2PTQCJ7EPA5CNFSM4IH7DHD2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD3JOX3Q#issuecomment-517139438>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAINFWHLL4QRMPWPFJGUOSDQCJ7EPANCNFSM4IH7DHDQ>
.
|
Beta Was this translation helpful? Give feedback.
-
I have an event that is emitted by an aggregate. A saga is started in the process and triggers off command to another aggregate. However, it also uses the same event to update its internal state (via Emit). The problem is that to Emit, I need to use an event that is tied via generics to the Saga aggregate - I can't use the one that started the saga. Am I right that I would need to create a copy of the event tied to the Saga even though it's really the same type of event I've already received? This seems to be a reoccurring theme in EventFlow where commands and events are inherently tied to the aggregates, limiting their reusability in other scenarios, especially Sagas which can be regarded as the orchestrator of events & commands from multiple places.
Beta Was this translation helpful? Give feedback.
All reactions