Skip to content

Commit

Permalink
Added missing AsTransaction* and code cleanup (#60)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Normen Scheiber <[email protected]>
  • Loading branch information
3 people authored Apr 7, 2022
1 parent 8f2f2c2 commit 7bdccc0
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 23 deletions.
3 changes: 2 additions & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"sdk": {
"version": "6.0.100"
"version": "6.0.100",
"rollForward": "latestFeature"
}
}
19 changes: 6 additions & 13 deletions src/Transactions.Tests/TransactionCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public async Task Transaction_Fails_WithoutCommit()
await collection.InsertOneAsync(user);
}
}
catch (Exception ex)
catch
{
// ignored
}
Expand Down Expand Up @@ -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());
Expand Down
10 changes: 6 additions & 4 deletions src/Transactions/MongoTransactionClient.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Bson;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -267,23 +268,24 @@ public Task<IChangeStreamCursor<TResult>> WatchAsync<TResult>(

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);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Transactions/MongoTransactionCollection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Bson;
Expand Down Expand Up @@ -800,7 +801,7 @@ public Task<IAsyncCursor<TResult>> MapReduceAsync<TResult>(
public IFilteredMongoCollection<TDerivedDocument> OfType<TDerivedDocument>()
where TDerivedDocument : T
{
return _collection.OfType<TDerivedDocument>();
return _collection.OfType<TDerivedDocument>().AsTransactionCollection();
}

public ReplaceOneResult ReplaceOne(
Expand Down Expand Up @@ -1072,6 +1073,7 @@ public IMongoCollection<T> WithWriteConcern(WriteConcern writeConcern)

public MongoCollectionSettings Settings => _collection.Settings;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool TryGetSession(out IClientSessionHandle sessionHandle) =>
TransactionStore.TryGetSession(_collection.Database.Client, out sessionHandle);
}
Expand Down
9 changes: 5 additions & 4 deletions src/Transactions/MongoTransactionDatabase.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -475,17 +475,17 @@ public Task<IChangeStreamCursor<TResult>> WatchAsync<TResult>(

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;
Expand All @@ -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);
}
Expand Down
19 changes: 19 additions & 0 deletions src/Transactions/MongoTransactionFilteredCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using MongoDB.Driver;

namespace MongoDB.Extensions.Transactions
{
public class MongoTransactionFilteredCollection<T>
: MongoTransactionCollection<T>
, IFilteredMongoCollection<T>
{
private readonly IFilteredMongoCollection<T> _filteredCollection;

public MongoTransactionFilteredCollection(IFilteredMongoCollection<T> filteredCollection)
: base(filteredCollection)
{
_filteredCollection = filteredCollection;
}

public FilterDefinition<T> Filter => _filteredCollection.Filter;
}
}
6 changes: 6 additions & 0 deletions src/Transactions/TransactionCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@ public static IMongoCollection<T> AsTransactionCollection<T>(
{
return new MongoTransactionCollection<T>(collection);
}

public static IFilteredMongoCollection<T> AsTransactionCollection<T>(
this IFilteredMongoCollection<T> collection)
{
return new MongoTransactionFilteredCollection<T>(collection);
}
}
}

0 comments on commit 7bdccc0

Please sign in to comment.