Beispiel #1
0
        public void Update(IContainSagaData saga, string versionFieldName, int version)
        {
            var collection = GetCollection(saga.GetType());

            var fbuilder = Builders <BsonDocument> .Filter;
            var filter   = fbuilder.Eq("_id", saga.Id) & fbuilder.Eq(versionFieldName, version);



            var bsonDoc  = saga.ToBsonDocument();
            var ubuilder = Builders <BsonDocument> .Update;
            var update   = ubuilder.Inc(versionFieldName, 1);

            foreach (var field in bsonDoc.Where(field => field.Name != versionFieldName && field.Name != "_id"))
            {
                update = update.Set(field.Name, field.Value);
            }


            var modifyResult = collection.FindOneAndUpdate(
                filter,
                update,
                new FindOneAndUpdateOptions <BsonDocument> {
                IsUpsert = false, ReturnDocument = ReturnDocument.After
            });

            if (modifyResult == null)
            {
                throw new SagaMongoDbConcurrentUpdateException(version);
            }
        }
        public async Task Save(
            IContainSagaData sagaData,
            SagaCorrelationProperty correlationProperty,
            SynchronizedStorageSession session,
            ContextBag context)
        {
            var sagaTypeName = sagaData.GetType().Name;

            var sagaDataWithVersion = sagaData as IHaveDocumentVersion;

            if (sagaDataWithVersion == null)
            {
                throw new InvalidOperationException(
                          string.Format("Saga type {0} does not implement IHaveDocumentVersion", sagaTypeName));
            }

            sagaDataWithVersion.DocumentVersion = 0;
            sagaDataWithVersion.ETag            = sagaData.ComputeETag();

            if (correlationProperty != null)
            {
                await this.EnsureUniqueIndex(sagaData, correlationProperty).ConfigureAwait(false);
            }

            var collection = this.mongoDatabase.GetCollection <BsonDocument>(sagaTypeName);
            await collection.InsertOneAsync(sagaData.ToBsonDocument()).ConfigureAwait(false);
        }
Beispiel #3
0
        public async Task Save(IContainSagaData sagaData, SagaCorrelationProperty correlationProperty, ISynchronizedStorageSession session, ContextBag context, CancellationToken cancellationToken = default)
        {
            var storageSession = (StorageSession)session;
            var sagaDataType   = sagaData.GetType();

            var document = sagaData.ToBsonDocument();

            document.Add(versionElementName, 0);

            await storageSession.InsertOneAsync(sagaDataType, document, cancellationToken).ConfigureAwait(false);
        }
Beispiel #4
0
        public async Task Update(IContainSagaData sagaData, ISynchronizedStorageSession session, ContextBag context, CancellationToken cancellationToken = default)
        {
            var storageSession = (StorageSession)session;
            var sagaDataType   = sagaData.GetType();

            var version  = storageSession.RetrieveVersion(sagaDataType);
            var document = sagaData.ToBsonDocument().SetElement(new BsonElement(versionElementName, version + 1));

            var result = await storageSession.ReplaceOneAsync(sagaDataType, filterBuilder.Eq(idElementName, sagaData.Id)& filterBuilder.Eq(versionElementName, version), document, cancellationToken).ConfigureAwait(false);

            if (result.ModifiedCount != 1)
            {
                throw new Exception($"The '{sagaDataType.Name}' saga with id '{sagaData.Id}' was updated by another process or no longer exists.");
            }
        }
        public void Update(IContainSagaData saga, string versionFieldName, int version)
        {
            var collection = _db.GetCollection(GetCollectionName(saga.GetType()));

            var query = Query.And(Query.EQ("_id", saga.Id), Query.EQ(versionFieldName, version));

            var bsonDoc = saga.ToBsonDocument();
            var update  = new UpdateBuilder().Inc(versionFieldName, 1);

            foreach (var field in bsonDoc.Where(field => field.Name != versionFieldName && field.Name != "_id"))
            {
                update.Set(field.Name, field.Value);
            }

            var modifyResult = collection.FindAndModify(query, SortBy.Null, update, true, false);

            if (modifyResult.ModifiedDocument == null)
            {
                throw new SagaMongoDbConcurrentUpdateException(version);
            }
        }