Exemplo n.º 1
0
        private void Agg_EventCreated(object sender, IEvent e)
        {
            SaveEvent(e, _conn);
            Aggregate agg = (Aggregate)sender;

            agg.Commit();
        }
Exemplo n.º 2
0
        public override async Task SaveChangesAsync(CancellationToken ctk = default)
        {
            if (Aggregate.UncommittedAggregateEvents.Any())
            {
                if (_chex == null)
                {
                    _session.Advanced.ClusterTransaction.CreateCompareExchangeValue <long>(_chexKey, Aggregate.Version);
                }
                else
                {
                    _session.Advanced.ClusterTransaction.UpdateCompareExchangeValue <long>(new CompareExchangeValue <long>(_chexKey, _chex.Index, Aggregate.Version));
                }

                foreach (var e in Aggregate.UncommittedDomainEvents)
                {
                    var eventType  = e.Event.GetType();
                    var outboxType = typeof(OutboxEvent <>).MakeGenericType(eventType);

                    var evt = (OutboxEvent)Activator.CreateInstance(outboxType);

                    evt.Id       = e.Metadata.EventId;
                    evt.Metadata = e.Metadata.Values.ToDictionary(x => x.Key, x => x.Value);
                    evt.SetEvent(e.Event);

                    await _session.StoreAsync(evt, ctk);
                }

                foreach (var e in Aggregate.UncommittedAggregateEvents)
                {
                    await _session.StoreAsync(e.ToStore(), ctk);
                }

                await _session.StoreAsync(Aggregate.State, AggregateHelper <TAggregate> .Name + "/" + Aggregate.Identifier, ctk);

                await _session.SaveChangesAsync(ctk);
            }

            Aggregate.Commit();
        }