コード例 #1
0
        public static Message GenerateMessage(this PendingEvent entity,
                                              TypeResolver typeResolver,
                                              IJsonProcessor jsonProcessor)
        {
            Type type = typeResolver.TryResolveType(entity.EventType);

            ConstructorInfo constructor = typeof(StreamEvent <>)
                                          .MakeGenericType(type)
                                          .GetTypeInfo()
                                          .GetConstructor(new[] { typeof(Guid), typeof(long), typeof(DateTime), type });

            object data = constructor.Invoke(parameters: new object[]
            {
                entity.StreamId,
                entity.Version,
                new DateTime(entity.RaisedTimeUtc.Ticks, DateTimeKind.Utc),
                jsonProcessor.FromJson(entity.Payload, type),
            });

            return(new Message(id: entity.MessageId, data, entity.TracingProperties));
        }
コード例 #2
0
        private async Task SaveAndPublish(string stateType,
                                          Guid transaction,
                                          Guid streamId,
                                          long startVersion,
                                          ImmutableArray <object> events,
                                          TracingProperties tracingProperties = default)
        {
            await SaveEvents().ConfigureAwait(continueOnCapturedContext: false);
            await PublishPendingEvents().ConfigureAwait(continueOnCapturedContext: false);

            async Task SaveEvents()
            {
                using EventStoreContext context = _contextFactory.Invoke();

                for (int i = 0; i < events.Length; i++)
                {
                    object source = events[i];

                    var streamEvent = new StreamEvent(
                        stateType,
                        streamId,
                        version: startVersion + i,
                        raisedTimeUtc: DateTime.UtcNow,
                        eventType: _typeResolver.ResolveTypeName(source.GetType()),
                        payload: _jsonProcessor.ToJson(source),
                        messageId: $"{Guid.NewGuid()}",
                        tracingProperties.OperationId,
                        tracingProperties.Contributor,
                        tracingProperties.ParentId,
                        transaction);

                    context.Add(streamEvent);
                    context.Add(PendingEvent.Create(streamEvent));
                }

                List <UniqueProperty> uniqueProperties = await context
                                                         .Set <UniqueProperty>()
                                                         .Where(p => p.StateType == stateType && p.StreamId == streamId)
                                                         .AsNoTracking()
                                                         .ToListAsync()
                                                         .ConfigureAwait(continueOnCapturedContext: false);

                foreach ((string name, string value) in GetUniqueProperties(events))
                {
                    UniqueProperty property = uniqueProperties.Find(p => p.Name == name);

                    if (value == null)
                    {
                        if (property != null)
                        {
                            context.UniqueProperties.Remove(property);
                        }
                    }
                    else
                    {
                        if (property == null)
                        {
                            context.Add(new UniqueProperty(stateType, streamId, name, value));
                        }
                        else
                        {
                            property.SetValue(value);
                            context.Update(property);
                        }
                    }
                }

                await context.SaveChangesAsync().ConfigureAwait(continueOnCapturedContext: false);
            }

            Task PublishPendingEvents() => _publisher.PublishEvents(stateType, streamId);
        }
コード例 #3
0
ファイル: EventPublisher.cs プロジェクト: suhyeon/loom-dotnet
 private Message GenerateMessage(PendingEvent entity)
 {
     return(entity.GenerateMessage(_typeResolver, _jsonProcessor));
 }