Exemplo n.º 1
0
        public async Task Commit(ISession session, ICommand command)
        {
            using (var connection = await _dataStoreConnectionFactory.Connect())
            {
                using (var transaction = connection.BeginTransaction(TransactionIsolationLevel))
                {
                    try
                    {
                        using (await _databaseScopeMutex.LockAsync())
                        {
                            _activeDatabaseScopes[session.Key] = new DatabaseScope(connection, transaction);
                        }

                        foreach (var descriptor in session.TrackedAggregates)
                        {
                            if (descriptor.IsMarkedForDeletion)
                            {
                                await _repository.Delete(descriptor.Aggregate.Id);

                                await _eventPublisher.Publish(new AggregateDeletedEvent(descriptor.Aggregate.Id, command.Id, command.UserId));
                            }
                            else
                            {
                                await _repository.Save(descriptor.Aggregate, command.UserId, session.Key);
                            }
                        }

                        await _sessionTracker.CommitSession(command);

                        transaction.Commit();
                    }
                    catch (Exception e) // Todo: For testing - remove later
                    {
                        transaction.Rollback();
                        await _sessionTracker.RollbackSession(session.Key);

                        await _eventPublisher.Publish(new SessionRolledBackEvent(command.Id, command.UserId));

                        throw;
                    }
                    finally
                    {
                        await Cleanup(session);
                    }
                }
            }

            await _eventPublisher.Publish(new SessionCommittedEvent(command.Id, command.UserId));
        }
Exemplo n.º 2
0
 public static void Delete <TRoot>(this IAggregateRootRepository <TRoot> repository, string uri)
     where TRoot : IAggregateRoot
 {
     repository.Delete(new[] { uri });
 }