コード例 #1
0
        public void Delete(string aggregateId, int version)
        {
            var stream        = _documentSession.Load <EventStream>(aggregateId);
            var actualVersion = stream.Events.Last().Version;

            if (actualVersion != version)
            {
                var exception = new EventStoreConcurrencyException(
                    string.Format("Expected version {0} does not match actual version {1}", version, actualVersion));
                exception.Data.Add("Existing stream", aggregateId);
                throw exception;
            }

            _documentSession.Delete(stream);
        }
コード例 #2
0
        public void Save <T>(string aggregateId, int expectedVersion, IEnumerable <IEvent> newEvents)
        {
            var eventStoreExpectedVersion = expectedVersion == -1 ? ExpectedVersion.NoStream : expectedVersion;

            try
            {
                _logger.Debug(this, "Saving " + typeof(T) + " to stream " + aggregateId);

                _eventStoreConnection.AppendToStreamAsync(aggregateId, eventStoreExpectedVersion, GetEventData(newEvents)).Wait();
            }
            catch (WrongExpectedVersionException ex)
            {
                // Wrap in a Regalo-defined exception so that callers don't have to worry what impl is in place
                var concurrencyException = new EventStoreConcurrencyException("Unable to save to EventStore", ex);
                throw concurrencyException;
            }
        }
コード例 #3
0
        //Dessa forma, os métodos HasConflict e HandleConcurrencyException podem ser sobrescritos para se adaptar a questões de negócio
        protected virtual void HandleConcurrencyException <TAggregate>(EventStoreConcurrencyException ex, TAggregate root)
            where TAggregate : AggregateRoot <Guid>
        {
            foreach (var changeEvent in root.Changes)
            {
                foreach (var storeEvent in ex.StoreEvents)
                {
                    if (HasConflict(changeEvent, storeEvent))
                    {
                        var msg = string.Format("Conflict between {0} and {1}",
                                                changeEvent, changeEvent);
                        throw new Exception(msg, ex);
                    }
                }
            }

            var actualVersion = root.Version - root.Changes.Count;

            actualVersion += actualVersion;
            _eventStore.AppendToStream <TAggregate>(root.Id, actualVersion, root.Changes, root.DomainEvents.ToArray());
        }
コード例 #4
0
        private void DeleteAggregateRow(Guid aggregateId, int version, SqlConnection connection)
        {
            var eventCommand = connection.CreateCommand();

            eventCommand.CommandType = CommandType.Text;
            eventCommand.CommandText = @"delete from AggregateRoot where Id = @AggregateId and [Version] = @Version;";

            var aggregateIdParameter = eventCommand.Parameters.Add("@AggregateId", SqlDbType.UniqueIdentifier);
            var versionParameter     = eventCommand.Parameters.Add("@Version", SqlDbType.Int);

            aggregateIdParameter.Value = aggregateId;
            versionParameter.Value     = version;

            var rowsDeleted = eventCommand.ExecuteNonQuery();

            if (rowsDeleted == 0)
            {
                var exception = new EventStoreConcurrencyException(
                    string.Format("Expected version {0} does not match actual version", version));
                exception.Data.Add("Existing stream", aggregateId);
                throw exception;
            }
        }