void IEventSourceCommandRepository <SpotCharter, SpotCharterId> .Save(SpotCharter instance)
        {
            SpotCharterId         id          = instance.Id;
            IList <EventInstance> eventStream = this.repository.ContainsKey(id) ? this.repository[id].ToList() : new List <EventInstance>();

            IEventSourcedAggregate <SpotCharterId> spotEventsInterface = instance;

            foreach (var @event in spotEventsInterface.Events)
            {
                eventStream.Add(new EventInstance {
                    JsonString = JsonConvert.SerializeObject(@event), Type = @event.GetType()
                });
            }

            this.repository[id] = eventStream;
            if (this.dispatcher != null)
            {
                foreach (var @event in spotEventsInterface.Events)
                {
                    this.dispatcher.Publish(new DispatchEvent {
                        EventName = @event.EventName, Source = @event.Source, Timestamp = @event.Timestamp, Version = @event.Version, Payload = JsonConvert.SerializeObject(@event)
                    });
                }
            }
        }
Exemplo n.º 2
0
 private void GenerateSut(Boolean initialize = true)
 {
     snapshottable    = sut = new AggregateTestSampleAggregate1();
     sourcedAggregate = sut;
     if (initialize)
     {
         sut.Init("AggregateTestSampleAggregate1_42");
     }
 }
        /// <summary>
        /// Replay the event to the aggregate and increase version
        /// </summary>
        /// <typeparam name="TAggregate"></typeparam>
        /// <param name="aggregate"></param>
        /// <param name="event"></param>
        public static void Replay <TAggregate>(this TAggregate aggregate, IDomainEvent @event)
            where TAggregate : IEventSourcedAggregate

        {
            ((dynamic)aggregate).Apply((dynamic)@event);
            //RedirectToApply.InvokeEventOptional(aggregate, @event); // dynamic seems faster
            IEventSourcedAggregate eventSourcedAggregate = (IEventSourcedAggregate)aggregate;

            eventSourcedAggregate.Version++;
        }
Exemplo n.º 4
0
        GenerateSnapshotAsync(Guid aggregateId, Type aggregateType, IEventSourcedAggregate rehydratedAggregate)
        {
            Snapshot            snap        = null;
            const int           newSequence = 1;
            List <IDomainEvent> events      = new List <IDomainEvent>();

            events = EventStoreAzureDbContext.Client.CreateDocumentQuery <Event>(EventStoreAzureDbContext.EventsDatabaseLink)
                     .Where(@event => @event.AggregateId == aggregateId && @event.AggregateType == aggregateType.AssemblyQualifiedName)
                     .Select(EventStoreManager.GetRehydratedEventFromDbEvent).ToList();

            object stateProp =
                aggregateType.GetAllProperties().FirstOrDefault(p => p.PropertyType.IsSubclassOf(typeof(AggregateState)))
                ??
                (object)aggregateType.GetAllFields().FirstOrDefault(f => f.FieldType.IsSubclassOf(typeof(AggregateState)));

            AggregateState state = null;

            if (stateProp is PropertyInfo propInfo)
            {
                state = propInfo.GetValue(rehydratedAggregate) as AggregateState;
            }
            else if (stateProp is FieldInfo fieldInfo)
            {
                state = fieldInfo.GetValue(rehydratedAggregate) as AggregateState;
            }

            if (state != null)
            {
                snap = new Snapshot(
                    aggregateId: aggregateId,
                    aggregateType: aggregateType.AssemblyQualifiedName,
                    aggregateState: state,
                    snapshotBehaviorType: typeof(NumericSnapshotBehavior).AssemblyQualifiedName,
                    snapshotTime: DateTime.Now);

                var feedResponse = await EventStoreAzureDbContext.Client.CreateDocumentQuery <Event>(EventStoreAzureDbContext.EventsDatabaseLink)
                                   .Where(@event => @event.AggregateId == aggregateId && @event.AggregateType == aggregateType.AssemblyQualifiedName)
                                   .AsDocumentQuery().ExecuteNextAsync <Document>().ConfigureAwait(false);

                await feedResponse
                .DoForEachAsync(async e => await EventStoreAzureDbContext.Client.DeleteDocumentAsync(documentLink : e.SelfLink).ConfigureAwait(false))
                .ConfigureAwait(false);
            }
            return(snap, newSequence, events);
        }
Exemplo n.º 5
0
        protected int CalculateExpectedVersion <T>(IEventSourcedAggregate eventSourcedAggregate, List <T> events)
        {
            var expectedVersion = eventSourcedAggregate.Version - events.Count;

            return(expectedVersion);
        }