Example #1
0
        public async Task Commit()
        {
            await _pending.ForEach(async (aggregateType, aggregateID, events) =>
            {
                var aggregatePath = AggregatePath(_root, aggregateType, aggregateID);
                var lines         = events
                                    .Select(e => JsonConvert.SerializeObject(e, JsonSerializerSettings))
                                    .ToArray();

                await _fileSystem.AppendFileLines(aggregatePath, lines);
            });


            var eventsForProjection = _pending.AllEvents.ToArray();

            foreach (var projection in _projections)
            {
                foreach (var @event in eventsForProjection)
                {
                    projection.Apply(@event);

                    var projectionPath = Path.Combine(_root, projection.For.Name + ".json");
                    var projectionJson = JsonConvert.SerializeObject(projection.ToMemento(), Formatting.Indented, JsonSerializerSettings);

                    await _fileSystem.WriteFile(projectionPath, async stream =>
                    {
                        using (var writer = new StreamWriter(stream))
                            await writer.WriteAsync(projectionJson);
                    });
                }
            }

            _pending.Clear();
        }
Example #2
0
        private void PerformCommit()
        {
            _pendingEvents.ForEach((aggregateType, aggregateID, events) =>
            {
                _storeEvents.AddEvents(aggregateType, aggregateID, events);

                foreach (var projection in _projections)
                {
                    foreach (var @event in events)
                    {
                        projection.Apply(@event);
                    }
                }
            });

            _pendingEvents.Clear();
        }