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));
            }
        }
예제 #2
0
        public TAggregate GetById <TAggregate>(String id)
            where TAggregate : AggregateRoot
        {
            if (String.IsNullOrEmpty(id))
            {
                throw new ArgumentException("Aggregate id was not specified.");
            }
            Snapshot snapshot = null;

            if (_snapshotRepository != null)
            {
                snapshot = _snapshotRepository.Load <TAggregate>(id);
            }
            var obj = snapshot == null
                ? AggregateCreator.CreateAggregateRoot <TAggregate>()
                : (TAggregate)snapshot.Payload;

            var fromVersion = snapshot == null ? 0 : snapshot.StreamVersion + 1;
            var stream      = _transitionStorage.OpenStream(id, fromVersion, int.MaxValue);

            obj.LoadFromTransitionStream(stream);
            return(obj);
        }