예제 #1
0
        public async Task <AsyncExecutedResult <EventStreamSavedResult> > SaveAsync(EventStream stream)
        {
            try
            {
                using (var connection = new NpgsqlConnection(connectionString))
                {
                    await connection.ExecuteAsync(BuildSql(saveSql, stream.AggregateRootId), ToStreamDescriptor(stream));

                    return(AsyncExecutedResult <EventStreamSavedResult> .Success(EventStreamSavedResult.Success));
                }
            }
            catch (NpgsqlException ex)
            {
                if (ex.ErrorCode == 23505 && ex.Message.Contains(versionUniqueIndexName))
                {
                    return(AsyncExecutedResult <EventStreamSavedResult> .Success(EventStreamSavedResult.DuplicatedEvent));
                }

                if (ex.ErrorCode == 23505 && ex.Message.Contains(commandIdUniqueIndexName))
                {
                    return(AsyncExecutedResult <EventStreamSavedResult> .Success(EventStreamSavedResult.DuplicatedCommand));
                }

                return(AsyncExecutedResult <EventStreamSavedResult> .Failed(ex, EventStreamSavedResult.Failed));
            }
            catch (Exception ex)
            {
                return(AsyncExecutedResult <EventStreamSavedResult> .Failed(ex, EventStreamSavedResult.Failed));
            }
        }
예제 #2
0
        public Task <AsyncExecutedResult <IReadOnlyList <EventStream> > > GetAllAsync(
            string aggregateRootTypeName,
            string aggregateRootId,
            long minVersion = -1L,
            long maxVersion = long.MaxValue)
        {
            try
            {
                var streams = GetAll().Where(e =>
                                             e.AggregateRootTypeName == aggregateRootTypeName &&
                                             e.AggregateRootId == aggregateRootId &&
                                             e.Version >= minVersion &&
                                             e.Version <= maxVersion);

                if (streams?.Count() > 0)
                {
                    return(Task.FromResult(AsyncExecutedResult <IReadOnlyList <EventStream> > .Success(streams.ToList())));
                }

                return(Task.FromResult(AsyncExecutedResult <IReadOnlyList <EventStream> > .Success(null)));
            }
            catch (Exception ex)
            {
                return(Task.FromResult(AsyncExecutedResult <IReadOnlyList <EventStream> > .Failed(ex)));
            }
        }
예제 #3
0
        public async Task <AsyncExecutedResult <IReadOnlyList <EventStream> > > GetAllAsync(
            string aggregateRootTypeName,
            string aggregateRootId,
            long minVersion = -1L,
            long maxVersion = long.MaxValue)
        {
            try
            {
                using (var connection = new NpgsqlConnection(connectionString))
                {
                    var descriptors = await connection.QueryAsync <EventStreamDescriptor>(
                        BuildSql(getAllSql, aggregateRootId),
                        new { AggregateRootTypeName = aggregateRootTypeName, AggregateRootId = aggregateRootId, MinVersion = minVersion, MaxVersion = maxVersion });

                    if (descriptors?.Count() > 0)
                    {
                        return(AsyncExecutedResult <IReadOnlyList <EventStream> > .Success(descriptors.Select(d => ToStream(d)).ToList()));
                    }

                    return(AsyncExecutedResult <IReadOnlyList <EventStream> > .Success(null));
                }
            }
            catch (Exception ex)
            {
                return(AsyncExecutedResult <IReadOnlyList <EventStream> > .Failed(ex));
            }
        }
예제 #4
0
        public async Task <AsyncExecutedResult <EventStreamSavedResult> > SaveAsync(EventStream stream)
        {
            if (GetAll().Any(e => e.AggregateRootId == stream.AggregateRootId && e.Version == stream.Version))
            {
                return(AsyncExecutedResult <EventStreamSavedResult> .Success(EventStreamSavedResult.DuplicatedEvent));
            }

            if (GetAll().Any(e => e.AggregateRootId == stream.AggregateRootId && e.CommandId == stream.CommandId))
            {
                return(AsyncExecutedResult <EventStreamSavedResult> .Success(EventStreamSavedResult.DuplicatedCommand));
            }

            try
            {
                await collection.InsertOneAsync(session, stream);

                await session.CommitTransactionAsync();

                return(AsyncExecutedResult <EventStreamSavedResult> .Success(EventStreamSavedResult.Success));
            }
            catch (Exception ex)
            {
                return(AsyncExecutedResult <EventStreamSavedResult> .Failed(ex, EventStreamSavedResult.Failed));
            }
        }
        public async Task <AsyncExecutedResult <long> > GetAsync(string aggregateRootTypeName, string aggregateRootId)
        {
            try
            {
                using (var connection = new MySqlConnection(connectionString))
                {
                    var version = await connection.QueryFirstOrDefaultAsync <long>(
                        BuildSql(getSql, aggregateRootId),
                        new { AggregateRootTypeName = aggregateRootTypeName, AggregateRootId = aggregateRootId });

                    return(AsyncExecutedResult <long> .Success(version));
                }
            }
            catch (Exception ex)
            {
                return(AsyncExecutedResult <long> .Failed(ex));
            }
        }
예제 #6
0
        public Task <AsyncExecutedResult <EventStream> > GetByVersionAsync(string aggregateRootId, long version)
        {
            try
            {
                var stream = GetAll().FirstOrDefault(e => e.AggregateRootId == aggregateRootId && e.Version == version);

                if (stream != null)
                {
                    return(Task.FromResult(AsyncExecutedResult <EventStream> .Success(stream)));
                }

                return(Task.FromResult(AsyncExecutedResult <EventStream> .Success(null)));
            }
            catch (Exception ex)
            {
                return(Task.FromResult(AsyncExecutedResult <EventStream> .Failed(ex)));
            }
        }
        public Task <AsyncExecutedResult <long> > GetAsync(string aggregateRootTypeName, string aggregateRootId)
        {
            try
            {
                var descriptor = GetAll().FirstOrDefault(d => d.AggregateRootTypeName == aggregateRootTypeName && d.AggregateRootId == aggregateRootId);

                if (descriptor != null)
                {
                    return(Task.FromResult(AsyncExecutedResult <long> .Success(descriptor.Version)));
                }

                return(Task.FromResult(AsyncExecutedResult <long> .Success(0)));
            }
            catch (Exception ex)
            {
                return(Task.FromResult(AsyncExecutedResult <long> .Failed(ex)));
            }
        }
예제 #8
0
        public async Task <AsyncExecutedResult <EventStream> > GetByVersionAsync(string aggregateRootId, long version)
        {
            try
            {
                using (var connection = new NpgsqlConnection(connectionString))
                {
                    var descriptor = await connection.QueryFirstOrDefaultAsync <EventStreamDescriptor>(
                        BuildSql(getByVersionSql, aggregateRootId),
                        new { AggregateRootId = aggregateRootId, Version = version });

                    if (descriptor != null)
                    {
                        return(AsyncExecutedResult <EventStream> .Success(ToStream(descriptor)));
                    }

                    return(AsyncExecutedResult <EventStream> .Success(null));
                }
            }
            catch (Exception ex)
            {
                return(AsyncExecutedResult <EventStream> .Failed(ex));
            }
        }