public void Save(AggregateRoot aggregate)
        {
            if (String.IsNullOrEmpty(aggregate.Id))
            {
                throw new ArgumentException("Aggregate id was not specified.");
            }

            var transition = aggregate.CreateTransition(_dataTypeRegistry);

            using (var stream = _transitionStorage.OpenStream(transition.Id.StreamId))
            {
                stream.Write(transition);
            }

            if (_eventBus != null)
            {
                _eventBus.Publish(transition.Events.Select(e => (IEvent)e.Data).ToList <IEvent>());
            }

            if (_snapshotRepository != null)
            {
                aggregate.Version += 1; // we have to increase aggregate version, because of new transition increase aggregate version
                _snapshotRepository.Save(new Snapshot(aggregate.Id, transition.Id.Version, aggregate));
            }
        }
示例#2
0
 public async Task Save <T>(T aggregate)
     where T : AggregateRoot
 {
     if (IsSnapshotable(typeof(T)))
     {
         await _snapshotRepository.Save(aggregate);
     }
     else
     {
         await _eventRepository.Save(aggregate);
     }
 }
        private void Update(string userId, Action <UserAR> updateAction)
        {
            var snapshot     = _snapshotRepository.Load(userId);
            var startVersion = snapshot == null ? 0 : snapshot.StreamVersion + 1;
            var stream       = _eventStore.OpenStream(userId, startVersion, int.MaxValue);
            var user         = new UserAR(snapshot, stream);

            updateAction(user);
            var originalVersion = stream.GetVersion();

            _eventStore.AppendToStream(userId, originalVersion, user.Changes);
            var newVersion = originalVersion + 1;

            if (newVersion % SnapshotInterval == 0)
            {
                _snapshotRepository.Save(new Snapshot(userId, newVersion, user.State));
            }
        }