From 8590103f713d28bdb58390c811fa553d2705c9ac Mon Sep 17 00:00:00 2001 From: Sebastian Date: Tue, 16 Jun 2020 11:44:53 +0200 Subject: [PATCH] Another catch. --- .../MongoDb/MongoExtensions.cs | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/backend/src/Squidex.Infrastructure.MongoDb/MongoDb/MongoExtensions.cs b/backend/src/Squidex.Infrastructure.MongoDb/MongoDb/MongoExtensions.cs index 681dc1884..580843db0 100644 --- a/backend/src/Squidex.Infrastructure.MongoDb/MongoDb/MongoExtensions.cs +++ b/backend/src/Squidex.Infrastructure.MongoDb/MongoDb/MongoExtensions.cs @@ -91,11 +91,13 @@ namespace Squidex.Infrastructure.MongoDb return find.Project(Builders.Projection.Exclude(exclude1).Exclude(exclude2)); } - public static async Task UpsertVersionedAsync(this IMongoCollection collection, TKey key, long oldVersion, long newVersion, Func, UpdateDefinition> updater) where T : IVersionedEntity where TKey : notnull + public static async Task UpsertVersionedAsync(this IMongoCollection collection, TKey key, long oldVersion, long newVersion, Func, UpdateDefinition> updater) + where TEntity : IVersionedEntity + where TKey : notnull { try { - var update = updater(Builders.Update.Set(x => x.Version, newVersion)); + var update = updater(Builders.Update.Set(x => x.Version, newVersion)); if (oldVersion > EtagVersion.Any) { @@ -116,7 +118,7 @@ namespace Squidex.Infrastructure.MongoDb if (existingVersion != null) { - var versionField = BsonClassMap.LookupClassMap(typeof(T)).GetMemberMap(nameof(IVersionedEntity.Version)).ElementName; + var versionField = GetVersionField(); throw new InconsistentStateException(existingVersion[versionField].AsInt64, oldVersion, ex); } @@ -128,7 +130,9 @@ namespace Squidex.Infrastructure.MongoDb } } - public static async Task UpsertVersionedAsync(this IMongoCollection collection, TKey key, long oldVersion, T doc) where T : IVersionedEntity where TKey : notnull + public static async Task UpsertVersionedAsync(this IMongoCollection collection, TKey key, long oldVersion, TEntity doc) + where TEntity : IVersionedEntity + where TKey : notnull { try { @@ -151,7 +155,9 @@ namespace Squidex.Infrastructure.MongoDb if (existingVersion != null) { - throw new InconsistentStateException(existingVersion[nameof(IVersionedEntity.Version)].AsInt64, oldVersion, ex); + var versionField = GetVersionField(); + + throw new InconsistentStateException(existingVersion[versionField].AsInt64, oldVersion, ex); } } else @@ -161,6 +167,13 @@ namespace Squidex.Infrastructure.MongoDb } } + private static string GetVersionField() + where TEntity : IVersionedEntity + where TKey : notnull + { + return BsonClassMap.LookupClassMap(typeof(TEntity)).GetMemberMap(nameof(IVersionedEntity.Version)).ElementName; + } + public static async Task ForEachPipelineAsync(this IAsyncCursorSource source, Func processor, CancellationToken cancellationToken = default) { using (var cursor = await source.ToCursorAsync(cancellationToken))