Exemplo n.º 1
0
        /// <summary>
        /// Saves the specified events.
        /// </summary>
        /// <param name="events">The events.</param>
        /// <param name="ct">The cancellation token.</param>
        /// <returns>A <see cref="Task" />.</returns>
        public async Task Save(IEnumerable <IEvent> events, CancellationToken ct)
        {
            ct.ThrowIfCancellationRequested();
            foreach (var @event in events)
            {
                var indexName = IndexName + "-" + @event.GetType().Name.ToLowerInvariant();
                // create the index if it doesn't exist
                if (!_client.IndexExists(indexName).Exists)
                {
                    _client.CreateIndex(indexName);
                }

                var wrappedEvent = new ElasticWrappedEvent
                {
                    Event         = @event,
                    EventTypeName = @event.GetType().FullName,
                    Version       = @event.Version,
                    AggregateId   = @event.Id,
                    Id            = Guid.NewGuid()
                };

                await _client.IndexAsync(wrappedEvent, i => i
                                         .Index(indexName)
                                         .Id(wrappedEvent.Id.ToString())
                                         .Refresh(Refresh.True)
                                         .Timeout(TimeSpan.FromSeconds(30)));

                await _publisher.Publish(@event);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Recasts the event.
        /// </summary>
        /// <param name="event">The event.</param>
        /// <returns>An <see cref="IEvent" />.</returns>
        public IEvent RecastEvent(ElasticWrappedEvent @event)
        {
            var typedEvent =
                LocatorAssembly
                .GetTypes()
                .FirstOrDefault(t => t.GetInterfaces().Contains(typeof(IEvent)) &&
                                t.FullName == @event.EventTypeName);

            return((IEvent)JsonConvert.DeserializeObject(@event.Event.ToString(), typedEvent));
        }