gautema / CQRSlite

A lightweight framework to help creating CQRS and Eventsourcing applications in C#
Other
1.1k stars 266 forks source link

Question about events and new commands #108

Closed alansbraga closed 2 years ago

alansbraga commented 2 years ago

Hi!

I'm having the following problem: One of my aggregate send an event, when this event happens I need to create a command to another aggregate to update it. When I do that the software freezes on Session.Commit when the "await _repository.Save" command is executed. _trackedAggregates is now with 2 itens inside it and it tries to execute save again for the first aggregate

How are you dealing with this situation?

Thanks in advance!

gautema commented 2 years ago

Hi. Sorry for the late reply. Did you solve this? I haven't seen it myself since I send events in a callback from the event store async, but can try to find a solution if you haven't solved the issue

alansbraga commented 2 years ago

Hi. No problem...

I've created a Session with the original code and changed it to the code below. I've saved the tracked aggregates and clear the list before save it. I was trying to figure it out how to send the command to the end of the queue, but I couldn't find a way, so I did this... (One detail: I'm using mediatr for commands and events) Is there and example somewhere how do you do? I didn't get it...

    public async Task Commit(CancellationToken cancellationToken = default)
    {
        try
        {
            // changed this line
            var agregacoes = _trackedAggregates.Values.ToArray();
            _trackedAggregates.Clear();

            foreach (var descriptor in agregacoes)
            {
                await _repository.Save(descriptor.Aggregate, descriptor.Version, cancellationToken).ConfigureAwait(false);
            }
        }
        finally
        {
            _trackedAggregates.Clear();
        }
    }
gautema commented 2 years ago

Great that you found a solution. I'll take a look and see if there is anything I can or should do to solve this in a better way.

gautema commented 2 years ago

Latest commit fixes this problem. I'll try to upload a nuget package very soon