From 7bdccc0b60386cd8980f03ef6957ae903e0f5c9a Mon Sep 17 00:00:00 2001 From: Alberto Aldegheri Date: Thu, 7 Apr 2022 10:36:57 +0200 Subject: [PATCH] Added missing AsTransaction* and code cleanup (#60) * Added missing AsTransaction* and code cleanup - Added missing AsTransaction* internal calls - Fixed compilation warnings - Support for newer .NET SDKs * fix format * Align method names * Removed unused pragmas * Fix format Co-authored-by: Pascal Senn Co-authored-by: Normen Scheiber <46715105+nscheibe@users.noreply.github.com> --- global.json | 3 ++- .../TransactionCollectionTests.cs | 19 ++++++------------- src/Transactions/MongoTransactionClient.cs | 10 ++++++---- .../MongoTransactionCollection.cs | 4 +++- src/Transactions/MongoTransactionDatabase.cs | 9 +++++---- .../MongoTransactionFilteredCollection.cs | 19 +++++++++++++++++++ .../TransactionCollectionExtensions.cs | 6 ++++++ 7 files changed, 47 insertions(+), 23 deletions(-) create mode 100644 src/Transactions/MongoTransactionFilteredCollection.cs diff --git a/global.json b/global.json index 0470208..10b65be 100644 --- a/global.json +++ b/global.json @@ -1,5 +1,6 @@ { "sdk": { - "version": "6.0.100" + "version": "6.0.100", + "rollForward": "latestFeature" } } diff --git a/src/Transactions.Tests/TransactionCollectionTests.cs b/src/Transactions.Tests/TransactionCollectionTests.cs index eec783a..2cba1de 100644 --- a/src/Transactions.Tests/TransactionCollectionTests.cs +++ b/src/Transactions.Tests/TransactionCollectionTests.cs @@ -88,7 +88,7 @@ public async Task Transaction_Fails_WithoutCommit() await collection.InsertOneAsync(user); } } - catch (Exception ex) + catch { // ignored } @@ -430,23 +430,16 @@ await collection.InsertOneAsync( { async Task task() { - try + for (var j = 0; j < documentCount; j++) { - for (var j = 0; j < documentCount; j++) + using (var scope = new TransactionScope(Enabled)) { - using (var scope = new TransactionScope(Enabled)) - { - await collection.InsertOneAsync( - new User(Guid.NewGuid(), "Foo")); + await collection.InsertOneAsync( + new User(Guid.NewGuid(), "Foo")); - scope.Complete(); - } + scope.Complete(); } } - catch (Exception ex) - { - throw ex; - } } tasks.Add(task()); diff --git a/src/Transactions/MongoTransactionClient.cs b/src/Transactions/MongoTransactionClient.cs index be7bd34..c158cf5 100644 --- a/src/Transactions/MongoTransactionClient.cs +++ b/src/Transactions/MongoTransactionClient.cs @@ -1,3 +1,4 @@ +using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using MongoDB.Bson; @@ -56,7 +57,7 @@ public Task DropDatabaseAsync( return _client.DropDatabaseAsync(session, name, cancellationToken); } - public IMongoDatabase GetDatabase(string name, MongoDatabaseSettings settings = null) + public IMongoDatabase GetDatabase(string name, MongoDatabaseSettings? settings = null) { return _client.GetDatabase(name, settings).AsTransactionDatabase(); } @@ -267,23 +268,24 @@ public Task> WatchAsync( public IMongoClient WithReadConcern(ReadConcern readConcern) { - return _client.WithReadConcern(readConcern); + return _client.WithReadConcern(readConcern).AsTransactionClient(); } public IMongoClient WithReadPreference(ReadPreference readPreference) { - return _client.WithReadPreference(readPreference); + return _client.WithReadPreference(readPreference).AsTransactionClient(); } public IMongoClient WithWriteConcern(WriteConcern writeConcern) { - return _client.WithWriteConcern(writeConcern); + return _client.WithWriteConcern(writeConcern).AsTransactionClient(); } public ICluster Cluster => _client.Cluster; public MongoClientSettings Settings => _client.Settings; + [MethodImpl(MethodImplOptions.AggressiveInlining)] private bool TryGetSession(out IClientSessionHandle sessionHandle) => TransactionStore.TryGetSession(_client, out sessionHandle); } diff --git a/src/Transactions/MongoTransactionCollection.cs b/src/Transactions/MongoTransactionCollection.cs index ebad472..3c987ba 100644 --- a/src/Transactions/MongoTransactionCollection.cs +++ b/src/Transactions/MongoTransactionCollection.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using MongoDB.Bson; @@ -800,7 +801,7 @@ public Task> MapReduceAsync( public IFilteredMongoCollection OfType() where TDerivedDocument : T { - return _collection.OfType(); + return _collection.OfType().AsTransactionCollection(); } public ReplaceOneResult ReplaceOne( @@ -1072,6 +1073,7 @@ public IMongoCollection WithWriteConcern(WriteConcern writeConcern) public MongoCollectionSettings Settings => _collection.Settings; + [MethodImpl(MethodImplOptions.AggressiveInlining)] private bool TryGetSession(out IClientSessionHandle sessionHandle) => TransactionStore.TryGetSession(_collection.Database.Client, out sessionHandle); } diff --git a/src/Transactions/MongoTransactionDatabase.cs b/src/Transactions/MongoTransactionDatabase.cs index 962f76a..f260ff7 100644 --- a/src/Transactions/MongoTransactionDatabase.cs +++ b/src/Transactions/MongoTransactionDatabase.cs @@ -1,8 +1,8 @@ +using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using MongoDB.Bson; using MongoDB.Driver; -using static MongoDB.Extensions.Transactions.TransactionStore; namespace MongoDB.Extensions.Transactions { @@ -475,17 +475,17 @@ public Task> WatchAsync( public IMongoDatabase WithReadConcern(ReadConcern readConcern) { - return _database.WithReadConcern(readConcern); + return _database.WithReadConcern(readConcern).AsTransactionDatabase(); } public IMongoDatabase WithReadPreference(ReadPreference readPreference) { - return _database.WithReadPreference(readPreference); + return _database.WithReadPreference(readPreference).AsTransactionDatabase(); } public IMongoDatabase WithWriteConcern(WriteConcern writeConcern) { - return _database.WithWriteConcern(writeConcern); + return _database.WithWriteConcern(writeConcern).AsTransactionDatabase(); } public IMongoClient Client => _database.Client; @@ -494,6 +494,7 @@ public IMongoDatabase WithWriteConcern(WriteConcern writeConcern) public MongoDatabaseSettings Settings => _database.Settings; + [MethodImpl(MethodImplOptions.AggressiveInlining)] private bool TryGetSession(out IClientSessionHandle sessionHandle) => TransactionStore.TryGetSession(_database.Client, out sessionHandle); } diff --git a/src/Transactions/MongoTransactionFilteredCollection.cs b/src/Transactions/MongoTransactionFilteredCollection.cs new file mode 100644 index 0000000..9ab00dc --- /dev/null +++ b/src/Transactions/MongoTransactionFilteredCollection.cs @@ -0,0 +1,19 @@ +using MongoDB.Driver; + +namespace MongoDB.Extensions.Transactions +{ + public class MongoTransactionFilteredCollection + : MongoTransactionCollection + , IFilteredMongoCollection + { + private readonly IFilteredMongoCollection _filteredCollection; + + public MongoTransactionFilteredCollection(IFilteredMongoCollection filteredCollection) + : base(filteredCollection) + { + _filteredCollection = filteredCollection; + } + + public FilterDefinition Filter => _filteredCollection.Filter; + } +} diff --git a/src/Transactions/TransactionCollectionExtensions.cs b/src/Transactions/TransactionCollectionExtensions.cs index 7cb449e..a7f5663 100644 --- a/src/Transactions/TransactionCollectionExtensions.cs +++ b/src/Transactions/TransactionCollectionExtensions.cs @@ -9,5 +9,11 @@ public static IMongoCollection AsTransactionCollection( { return new MongoTransactionCollection(collection); } + + public static IFilteredMongoCollection AsTransactionCollection( + this IFilteredMongoCollection collection) + { + return new MongoTransactionFilteredCollection(collection); + } } }