Esempio n. 1
0
        public virtual async Task AppendEvent <TStream>(Guid aggregateId, IEvent @event, int?expectedVersion = null, Func <StreamState, Task> action = null)
        {
            var version = 1;

            var events = await GetEvents(aggregateId);

            var versions = events.Select(e => e.Version).ToList();

            if (expectedVersion.HasValue)
            {
                if (versions.Contains(expectedVersion.Value))
                {
                    throw new Exception($"Version '{expectedVersion.Value}' already exists for stream '{aggregateId}'");
                }
            }
            else
            {
                version = versions.DefaultIfEmpty(0).Max() + 1;
            }

            var stream = new StreamState
            {
                AggregateId = aggregateId,
                Version     = version,
                Type        = MessageBrokersHelper.GetTypeName(@event.GetType()),
                Data        = @event == null ? "{}" : JsonConvert.SerializeObject(@event, JSON_SERIALIZER_SETTINGS)
            };

            await _store.Add(stream);

            if (action != null)
            {
                await action(stream);
            }
        }
Esempio n. 2
0
 public virtual async Task Publish <T>(T @event) where T : IEvent
 {
     using (var p = new ProducerBuilder <string, string>(_kafkaOptions.Producer).Build())
     {
         await p.ProduceAsync(_kafkaOptions.Topic,
                              new Message <string, string>
         {
             Key   = MessageBrokersHelper.GetTypeName <T>(),
             Value = JsonConvert.SerializeObject(@event)
         });
     }
 }
        public virtual async Task Commit <TEvent>(TEvent @event) where TEvent : IEvent
        {
            var outboxMessage = new OutboxMessage
            {
                Type = MessageBrokersHelper.GetTypeName <TEvent>(),
                Data = @event == null ? "{}" : JsonConvert.SerializeObject(@event, new JsonSerializerSettings
                {
                    TypeNameHandling = TypeNameHandling.All
                })
            };

            await Commit(outboxMessage);
        }
        public virtual async Task AppendEvent <TStream>(Guid streamId, IEvent @event, int?expectedVersion = null, Func <StreamState, Task> action = null)
        {
            var version = 1;

            var events = await GetEvents(streamId);

            var versions = events.Select(e => e.Version).ToList();

            if (expectedVersion.HasValue)
            {
                if (versions.Contains(expectedVersion.Value))
                {
                    throw new Exception($"Version '{expectedVersion.Value}' already exists for stream '{streamId}'");
                }
            }
            else
            {
                version = versions.DefaultIfEmpty(0).Max() + 1;
            }

            var stream = new StreamState
            {
                AggregateId = streamId,
                Version     = version,
                Type        = MessageBrokersHelper.GetTypeName(@event.GetType()),
                Data        = @event == null ? "{}" : JsonConvert.SerializeObject(@event, new JsonSerializerSettings
                {
                    TypeNameHandling = TypeNameHandling.All
                })
            };

            await _streamStates.InsertOneAsync(stream);

            if (action != null)
            {
                await action(stream);
            }
        }
        private Action <RawRabbit.Configuration.Exchange.IExchangeDeclarationBuilder> GetExchangeDeclaration(Type type)
        {
            var name = MessageBrokersHelper.GetTypeName(type);

            return(GetExchangeDeclaration(name));
        }