Beispiel #1
0
        private async Task <AppendResult> InsertEvent(EventCommit commit, bool versionCheck)
        {
            IClientSessionHandle session = null;

            if (UseTransactions)
            {
                session = await Database.Client.StartSessionAsync(new ClientSessionOptions());

                session.StartTransaction(new TransactionOptions());
            }

            var watch = Stopwatch.StartNew();

            commit.Ordinal = await identifier.Next("commit");

            AppendResult result = AppendResult.NoUpdate;

            try
            {
                var currentVersion = await GetVersion(commit.AggregateType, commit.AggregateId);

                if (versionCheck && currentVersion != commit.ExpectedPreviousVersion)
                {
                    logger.LogWarning("Event Version check requested and version was wrong . was: {0} expected: {1}",
                                      currentVersion, commit.ExpectedPreviousVersion);
                    //TODO If version check throw exception!
                    result = AppendResult.WrongVersion(commit.VersionCommit);
                }

                commit.VersionCommit = currentVersion + commit.Events.Count;

                await Collection.InsertOneAsync(commit);

                result = new AppendResult(commit.Id.ToString(), false, commit.VersionCommit, "SUCCESS");
            }
            catch (MongoWriteException e)
            {
                if (e.Message.Contains("E11000 duplicate key"))
                {
                    result = AppendResult.WrongVersion(commit.VersionCommit);
                }
                else
                {
                    logger.LogError(e, "Error when saving a commit for {type} {id}", commit.AggregateType,
                                    commit.AggregateId);
                    throw;
                }
            }
            finally
            {
                await CommitOrAbortTx(session, result);

                watch.Stop();
                logger.LogDebug("{Count} events for {Type} - {Id} handled. Result: {Result}.", commit.Events.Count,
                                commit.AggregateType, commit.AggregateId, result.CommitId);
            }

            return(result);
        }
Beispiel #2
0
        private async Task <AppendResult> InsertEvent(EventCommit commit, bool versionCheck)
        {
            commit.Ordinal = await identifier.Next("commit");

            try
            {
                await Collection.InsertOneAsync(commit);
            }
            catch (MongoWriteException e)
            {
                if (e.Message.Contains("E11000 duplicate key "))
                {
                    return(AppendResult.WrongVersion(commit.VersionCommit));
                }

                throw;
            }
            return(new AppendResult(false, commit.VersionCommit));
        }