From 0cc7f5e5a09920f54929ee8f54da5c6552ec9d59 Mon Sep 17 00:00:00 2001 From: Savorboard Date: Mon, 23 Jul 2018 13:17:26 +0800 Subject: [PATCH] Refactoring to specification --- .../CAP.MongoDBCapOptionsExtension.cs | 2 +- .../CAP.Options.Extensions.cs | 1 + src/DotNetCore.CAP.MongoDB/CapPublisher.cs | 14 ++-- .../DotNetCore.CAP.MongoDB.csproj | 17 +++-- .../MongoDBCollectProcessor.cs | 7 +- .../MongoDBMonitoringApi.cs | 74 +++++++++++-------- src/DotNetCore.CAP.MongoDB/MongoDBStorage.cs | 24 +++--- .../MongoDBStorageConnection.cs | 33 ++++----- .../MongoDBStorageTransaction.cs | 20 +++-- src/DotNetCore.CAP.MongoDB/MongoDBUtil.cs | 16 ++-- .../MongoDBMonitoringApiTest.cs | 6 +- .../MongoDBStorageConnectionTest.cs | 8 +- .../MongoDBStorageTest.cs | 2 +- .../MongoDBTest.cs | 2 +- .../MongoDBUtilTest.cs | 7 +- 15 files changed, 122 insertions(+), 111 deletions(-) diff --git a/src/DotNetCore.CAP.MongoDB/CAP.MongoDBCapOptionsExtension.cs b/src/DotNetCore.CAP.MongoDB/CAP.MongoDBCapOptionsExtension.cs index 3bbb891..101a57c 100644 --- a/src/DotNetCore.CAP.MongoDB/CAP.MongoDBCapOptionsExtension.cs +++ b/src/DotNetCore.CAP.MongoDB/CAP.MongoDBCapOptionsExtension.cs @@ -7,7 +7,7 @@ namespace DotNetCore.CAP.MongoDB { public class MongoDBCapOptionsExtension : ICapOptionsExtension { - private Action _configure; + private readonly Action _configure; public MongoDBCapOptionsExtension(Action configure) { diff --git a/src/DotNetCore.CAP.MongoDB/CAP.Options.Extensions.cs b/src/DotNetCore.CAP.MongoDB/CAP.Options.Extensions.cs index af69c02..18c846f 100644 --- a/src/DotNetCore.CAP.MongoDB/CAP.Options.Extensions.cs +++ b/src/DotNetCore.CAP.MongoDB/CAP.Options.Extensions.cs @@ -2,6 +2,7 @@ using System; using DotNetCore.CAP; using DotNetCore.CAP.MongoDB; +// ReSharper disable once CheckNamespace namespace Microsoft.Extensions.DependencyInjection { public static class CapOptionsExtensions diff --git a/src/DotNetCore.CAP.MongoDB/CapPublisher.cs b/src/DotNetCore.CAP.MongoDB/CapPublisher.cs index 1cda633..74cd8b9 100644 --- a/src/DotNetCore.CAP.MongoDB/CapPublisher.cs +++ b/src/DotNetCore.CAP.MongoDB/CapPublisher.cs @@ -12,16 +12,18 @@ namespace DotNetCore.CAP.MongoDB { public class CapPublisher : CapPublisherBase, ICallbackPublisher { - private readonly IMongoClient _client; private readonly MongoDBOptions _options; private readonly IMongoDatabase _database; private bool _isInTransaction = true; - public CapPublisher(ILogger logger, IDispatcher dispatcher, - IMongoClient client, MongoDBOptions options, IServiceProvider provider) - : base(logger, dispatcher) + public CapPublisher( + ILogger logger, + IDispatcher dispatcher, + IMongoClient client, + MongoDBOptions options, + IServiceProvider provider) + : base(logger, dispatcher) { - _client = client; _options = options; _database = client.GetDatabase(_options.Database); ServiceProvider = provider; @@ -122,7 +124,6 @@ namespace DotNetCore.CAP.MongoDB return message.Id; } - private async Task PublishWithSessionAsync(string name, T contentObj, IClientSessionHandle session, string callbackName) { Guid operationId = default(Guid); @@ -159,7 +160,6 @@ namespace DotNetCore.CAP.MongoDB throw; } } - private async Task ExecuteAsync(IClientSessionHandle session, CapPublishedMessage message) { message.Id = await new MongoDBUtil().GetNextSequenceValueAsync(_database, _options.PublishedCollection, session); diff --git a/src/DotNetCore.CAP.MongoDB/DotNetCore.CAP.MongoDB.csproj b/src/DotNetCore.CAP.MongoDB/DotNetCore.CAP.MongoDB.csproj index ef1b0c1..1ae2980 100644 --- a/src/DotNetCore.CAP.MongoDB/DotNetCore.CAP.MongoDB.csproj +++ b/src/DotNetCore.CAP.MongoDB/DotNetCore.CAP.MongoDB.csproj @@ -1,13 +1,20 @@ - - - - - + netstandard2.0 + DotNetCore.CAP.MongoDB + $(PackageTags);MongoDB + + + + bin\$(Configuration)\netstandard2.0\DotNetCore.CAP.MongoDB.xml + 1701;1702;1705;CS1591 + + + + diff --git a/src/DotNetCore.CAP.MongoDB/MongoDBCollectProcessor.cs b/src/DotNetCore.CAP.MongoDB/MongoDBCollectProcessor.cs index a0519bc..167c4c2 100644 --- a/src/DotNetCore.CAP.MongoDB/MongoDBCollectProcessor.cs +++ b/src/DotNetCore.CAP.MongoDB/MongoDBCollectProcessor.cs @@ -9,16 +9,15 @@ namespace DotNetCore.CAP.MongoDB { public class MongoDBCollectProcessor : ICollectProcessor { - private readonly IMongoClient _client; private readonly MongoDBOptions _options; private readonly ILogger _logger; private readonly IMongoDatabase _database; private readonly TimeSpan _waitingInterval = TimeSpan.FromMinutes(5); - public MongoDBCollectProcessor(IMongoClient client, MongoDBOptions options, - ILogger logger) + public MongoDBCollectProcessor(ILogger logger, + MongoDBOptions options, + IMongoClient client) { - _client = client; _options = options; _logger = logger; _database = client.GetDatabase(_options.Database); diff --git a/src/DotNetCore.CAP.MongoDB/MongoDBMonitoringApi.cs b/src/DotNetCore.CAP.MongoDB/MongoDBMonitoringApi.cs index 7dfcff4..b8845b9 100644 --- a/src/DotNetCore.CAP.MongoDB/MongoDBMonitoringApi.cs +++ b/src/DotNetCore.CAP.MongoDB/MongoDBMonitoringApi.cs @@ -12,16 +12,15 @@ namespace DotNetCore.CAP.MongoDB { public class MongoDBMonitoringApi : IMonitoringApi { - private IMongoClient _client; - private MongoDBOptions _options; - private IMongoDatabase _database; + private readonly MongoDBOptions _options; + private readonly IMongoDatabase _database; public MongoDBMonitoringApi(IMongoClient client, MongoDBOptions options) { - _client = client ?? throw new ArgumentNullException(nameof(client)); + var mongoClient = client ?? throw new ArgumentNullException(nameof(client)); _options = options ?? throw new ArgumentNullException(nameof(options)); - _database = _client.GetDatabase(_options.Database); + _database = mongoClient.GetDatabase(_options.Database); } public StatisticsDto GetStatistics() @@ -87,12 +86,12 @@ namespace DotNetCore.CAP.MongoDB filter = filter & builder.Regex(x => x.Content, ".*" + queryDto.Content + ".*"); } - var result = - collection.Find(filter) - .SortByDescending(x => x.Added) - .Skip(queryDto.PageSize * queryDto.CurrentPage) - .Limit(queryDto.PageSize) - .ToList(); + var result = collection + .Find(filter) + .SortByDescending(x => x.Added) + .Skip(queryDto.PageSize * queryDto.CurrentPage) + .Limit(queryDto.PageSize) + .ToList(); return result; } @@ -130,30 +129,45 @@ namespace DotNetCore.CAP.MongoDB var endDate = DateTime.UtcNow; var groupby = new BsonDocument { - { "$group", new BsonDocument{ - { "_id", new BsonDocument { - { "Key", new BsonDocument { - { "$dateToString", new BsonDocument { - { "format", "%Y-%m-%d %H:00:00"}, - { "date", "$Added"} - }} - }} + { + "$group", new BsonDocument { + { "_id", new BsonDocument { + { "Key", new BsonDocument{ + { "$dateToString", new BsonDocument { + { "format", "%Y-%m-%d %H:00:00"}, + { "date", "$Added"} + } + } + } + } + } + }, + { "Count", new BsonDocument{{ "$sum", 1}}} + } + } + }; + + var match = new BsonDocument { + { "$match", new BsonDocument { + { "Added", new BsonDocument + { + { "$gt", endDate.AddHours(-24) } + } + }, + { "StatusName", + new BsonDocument + { + { "$eq", statusName} + } } - }, - { "Count", new BsonDocument{ - { "$sum", 1} - }} - }} + } + } }; - var match = new BsonDocument { { "$match", new BsonDocument { - { "Added", new BsonDocument { { "$gt", endDate.AddHours(-24) } } }, - { "StatusName", new BsonDocument { { "$eq", statusName} } - } } } }; - var pipeline = new BsonDocument[] { match, groupby }; + var pipeline = new[] { match, groupby }; var collection = _database.GetCollection(collectionName); - var result = collection.Aggregate(pipeline: pipeline).ToList(); + var result = collection.Aggregate(pipeline).ToList(); var dic = new Dictionary(); for (var i = 0; i < 24; i++) diff --git a/src/DotNetCore.CAP.MongoDB/MongoDBStorage.cs b/src/DotNetCore.CAP.MongoDB/MongoDBStorage.cs index 27084b8..aa924a5 100644 --- a/src/DotNetCore.CAP.MongoDB/MongoDBStorage.cs +++ b/src/DotNetCore.CAP.MongoDB/MongoDBStorage.cs @@ -16,9 +16,9 @@ namespace DotNetCore.CAP.MongoDB private readonly ILogger _logger; public MongoDBStorage(CapOptions capOptions, - MongoDBOptions options, - IMongoClient client, - ILogger logger) + MongoDBOptions options, + IMongoClient client, + ILogger logger) { _capOptions = capOptions; _options = options; @@ -44,26 +44,30 @@ namespace DotNetCore.CAP.MongoDB } var database = _client.GetDatabase(_options.Database); - var names = (await database.ListCollectionNamesAsync())?.ToList(); + var names = (await database.ListCollectionNamesAsync(cancellationToken: cancellationToken))?.ToList(); if (!names.Any(n => n == _options.ReceivedCollection)) { - await database.CreateCollectionAsync(_options.ReceivedCollection); + await database.CreateCollectionAsync(_options.ReceivedCollection, cancellationToken: cancellationToken); } - if (!names.Any(n => n == _options.PublishedCollection)) + + if (names.All(n => n != _options.PublishedCollection)) { - await database.CreateCollectionAsync(_options.PublishedCollection); + await database.CreateCollectionAsync(_options.PublishedCollection, cancellationToken: cancellationToken); } - if (!names.Any(n => n == "Counter")) + + if (names.All(n => n != "Counter")) { - await database.CreateCollectionAsync("Counter"); + await database.CreateCollectionAsync("Counter", cancellationToken: cancellationToken); var collection = database.GetCollection("Counter"); await collection.InsertManyAsync(new BsonDocument[] { new BsonDocument{{"_id", _options.PublishedCollection}, {"sequence_value", 0}}, new BsonDocument{{"_id", _options.ReceivedCollection}, {"sequence_value", 0}} - }); + }, cancellationToken: cancellationToken); } + + _logger.LogDebug("Ensuring all create database tables script are applied."); } } } \ No newline at end of file diff --git a/src/DotNetCore.CAP.MongoDB/MongoDBStorageConnection.cs b/src/DotNetCore.CAP.MongoDB/MongoDBStorageConnection.cs index 11e8d8e..76efca9 100644 --- a/src/DotNetCore.CAP.MongoDB/MongoDBStorageConnection.cs +++ b/src/DotNetCore.CAP.MongoDB/MongoDBStorageConnection.cs @@ -10,8 +10,8 @@ namespace DotNetCore.CAP.MongoDB { public class MongoDBStorageConnection : IStorageConnection { - private CapOptions _capOptions; - private MongoDBOptions _options; + private readonly CapOptions _capOptions; + private readonly MongoDBOptions _options; private readonly IMongoClient _client; private readonly IMongoDatabase _database; @@ -58,10 +58,6 @@ namespace DotNetCore.CAP.MongoDB return new MongoDBStorageTransaction(_client, _options); } - public void Dispose() - { - } - public async Task GetPublishedMessageAsync(int id) { var collection = _database.GetCollection(_options.PublishedCollection); @@ -72,13 +68,10 @@ namespace DotNetCore.CAP.MongoDB { var fourMinsAgo = DateTime.Now.AddMinutes(-4); var collection = _database.GetCollection(_options.PublishedCollection); - return await - collection.Find(x => - x.Retries < _capOptions.FailedRetryCount - && x.Added < fourMinsAgo - && (x.StatusName == StatusName.Failed || x.StatusName == StatusName.Scheduled)) - .Limit(200) - .ToListAsync(); + return await collection + .Find(x => x.Retries < _capOptions.FailedRetryCount && x.Added < fourMinsAgo && (x.StatusName == StatusName.Failed || x.StatusName == StatusName.Scheduled)) + .Limit(200) + .ToListAsync(); } public async Task GetReceivedMessageAsync(int id) @@ -92,12 +85,10 @@ namespace DotNetCore.CAP.MongoDB var fourMinsAgo = DateTime.Now.AddMinutes(-4); var collection = _database.GetCollection(_options.ReceivedCollection); - return await - collection.Find(x => - x.Retries < _capOptions.FailedRetryCount - && x.Added < fourMinsAgo - && (x.StatusName == StatusName.Failed || x.StatusName == StatusName.Scheduled) - ).Limit(200).ToListAsync(); + return await collection + .Find(x => x.Retries < _capOptions.FailedRetryCount && x.Added < fourMinsAgo && (x.StatusName == StatusName.Failed || x.StatusName == StatusName.Scheduled)) + .Limit(200) + .ToListAsync(); } public async Task StoreReceivedMessageAsync(CapReceivedMessage message) @@ -114,5 +105,9 @@ namespace DotNetCore.CAP.MongoDB return message.Id; } + public void Dispose() + { + + } } } \ No newline at end of file diff --git a/src/DotNetCore.CAP.MongoDB/MongoDBStorageTransaction.cs b/src/DotNetCore.CAP.MongoDB/MongoDBStorageTransaction.cs index 5ea0a56..aff5c76 100644 --- a/src/DotNetCore.CAP.MongoDB/MongoDBStorageTransaction.cs +++ b/src/DotNetCore.CAP.MongoDB/MongoDBStorageTransaction.cs @@ -7,17 +7,15 @@ namespace DotNetCore.CAP.MongoDB { internal class MongoDBStorageTransaction : IStorageTransaction { - private IMongoClient _client; private readonly MongoDBOptions _options; private readonly IMongoDatabase _database; private readonly IClientSessionHandle _session; public MongoDBStorageTransaction(IMongoClient client, MongoDBOptions options) { - _client = client; _options = options; _database = client.GetDatabase(options.Database); - _session = _client.StartSession(); + _session = client.StartSession(); _session.StartTransaction(); } @@ -41,10 +39,10 @@ namespace DotNetCore.CAP.MongoDB var collection = _database.GetCollection(_options.PublishedCollection); var updateDef = Builders.Update - .Set(x => x.Retries, message.Retries) - .Set(x => x.Content, message.Content) - .Set(x => x.ExpiresAt, message.ExpiresAt) - .Set(x => x.StatusName, message.StatusName); + .Set(x => x.Retries, message.Retries) + .Set(x => x.Content, message.Content) + .Set(x => x.ExpiresAt, message.ExpiresAt) + .Set(x => x.StatusName, message.StatusName); collection.FindOneAndUpdate(_session, x => x.Id == message.Id, updateDef); } @@ -59,10 +57,10 @@ namespace DotNetCore.CAP.MongoDB var collection = _database.GetCollection(_options.ReceivedCollection); var updateDef = Builders.Update - .Set(x => x.Retries, message.Retries) - .Set(x => x.Content, message.Content) - .Set(x => x.ExpiresAt, message.ExpiresAt) - .Set(x => x.StatusName, message.StatusName); + .Set(x => x.Retries, message.Retries) + .Set(x => x.Content, message.Content) + .Set(x => x.ExpiresAt, message.ExpiresAt) + .Set(x => x.StatusName, message.StatusName); collection.FindOneAndUpdate(_session, x => x.Id == message.Id, updateDef); } diff --git a/src/DotNetCore.CAP.MongoDB/MongoDBUtil.cs b/src/DotNetCore.CAP.MongoDB/MongoDBUtil.cs index 8d862ad..be7b33f 100644 --- a/src/DotNetCore.CAP.MongoDB/MongoDBUtil.cs +++ b/src/DotNetCore.CAP.MongoDB/MongoDBUtil.cs @@ -5,9 +5,9 @@ using MongoDB.Driver; namespace DotNetCore.CAP.MongoDB { - public class MongoDBUtil + internal class MongoDBUtil { - FindOneAndUpdateOptions _options = new FindOneAndUpdateOptions() + readonly FindOneAndUpdateOptions _options = new FindOneAndUpdateOptions() { ReturnDocument = ReturnDocument.After }; @@ -43,15 +43,9 @@ namespace DotNetCore.CAP.MongoDB var filter = new BsonDocument { { "_id", collectionName } }; var updateDef = Builders.Update.Inc("sequence_value", 1); - BsonDocument result; - if (session == null) - { - result = collection.FindOneAndUpdate(filter, updateDef, _options); - } - else - { - result = collection.FindOneAndUpdate(session, filter, updateDef, _options); - } + var result = session == null + ? collection.FindOneAndUpdate(filter, updateDef, _options) + : collection.FindOneAndUpdate(session, filter, updateDef, _options); if (result.TryGetValue("sequence_value", out var value)) { diff --git a/test/DotNetCore.CAP.MongoDB.Test/MongoDBMonitoringApiTest.cs b/test/DotNetCore.CAP.MongoDB.Test/MongoDBMonitoringApiTest.cs index a4b6817..31c0f26 100644 --- a/test/DotNetCore.CAP.MongoDB.Test/MongoDBMonitoringApiTest.cs +++ b/test/DotNetCore.CAP.MongoDB.Test/MongoDBMonitoringApiTest.cs @@ -12,9 +12,9 @@ namespace DotNetCore.CAP.MongoDB.Test { public class MongoDBMonitoringApiTest { - private MongoClient _client; - private MongoDBOptions _options; - private MongoDBMonitoringApi _api; + private readonly MongoClient _client; + private readonly MongoDBOptions _options; + private readonly MongoDBMonitoringApi _api; public MongoDBMonitoringApiTest() { diff --git a/test/DotNetCore.CAP.MongoDB.Test/MongoDBStorageConnectionTest.cs b/test/DotNetCore.CAP.MongoDB.Test/MongoDBStorageConnectionTest.cs index 1c02736..a258a5d 100644 --- a/test/DotNetCore.CAP.MongoDB.Test/MongoDBStorageConnectionTest.cs +++ b/test/DotNetCore.CAP.MongoDB.Test/MongoDBStorageConnectionTest.cs @@ -12,10 +12,10 @@ namespace DotNetCore.CAP.MongoDB.Test [TestCaseOrderer(PriorityOrderer.Name, PriorityOrderer.Assembly)] public class MongoDBStorageConnectionTest { - private MongoClient _client; - private MongoDBOptions _options; - private MongoDBStorage _storage; - private IStorageConnection _connection; + private readonly MongoClient _client; + private readonly MongoDBOptions _options; + private readonly MongoDBStorage _storage; + private readonly IStorageConnection _connection; public MongoDBStorageConnectionTest() { diff --git a/test/DotNetCore.CAP.MongoDB.Test/MongoDBStorageTest.cs b/test/DotNetCore.CAP.MongoDB.Test/MongoDBStorageTest.cs index 630956a..dbbe95e 100644 --- a/test/DotNetCore.CAP.MongoDB.Test/MongoDBStorageTest.cs +++ b/test/DotNetCore.CAP.MongoDB.Test/MongoDBStorageTest.cs @@ -9,7 +9,7 @@ namespace DotNetCore.CAP.MongoDB.Test { public class MongoDBStorageTest { - private MongoClient _client; + private readonly MongoClient _client; public MongoDBStorageTest() { diff --git a/test/DotNetCore.CAP.MongoDB.Test/MongoDBTest.cs b/test/DotNetCore.CAP.MongoDB.Test/MongoDBTest.cs index 1a91bba..096e04c 100644 --- a/test/DotNetCore.CAP.MongoDB.Test/MongoDBTest.cs +++ b/test/DotNetCore.CAP.MongoDB.Test/MongoDBTest.cs @@ -8,7 +8,7 @@ namespace DotNetCore.CAP.MongoDB.Test { public class MongoDBTest { - private MongoClient _client; + private readonly MongoClient _client; public MongoDBTest() { diff --git a/test/DotNetCore.CAP.MongoDB.Test/MongoDBUtilTest.cs b/test/DotNetCore.CAP.MongoDB.Test/MongoDBUtilTest.cs index 49e221b..0944a21 100644 --- a/test/DotNetCore.CAP.MongoDB.Test/MongoDBUtilTest.cs +++ b/test/DotNetCore.CAP.MongoDB.Test/MongoDBUtilTest.cs @@ -11,17 +11,16 @@ namespace DotNetCore.CAP.MongoDB.Test { public class MongoDBUtilTest { - private readonly MongoClient _client; private readonly IMongoDatabase _database; string _recieved = "ReceivedTest"; public MongoDBUtilTest() { - _client = new MongoClient(ConnectionUtil.ConnectionString); - _database = _client.GetDatabase("CAP_Test"); + var client = new MongoClient(ConnectionUtil.ConnectionString); + _database = client.GetDatabase("CAP_Test"); //Initialize MongoDB - if (!_database.ListCollectionNames().ToList().Any(x => x == "Counter")) + if (_database.ListCollectionNames().ToList().All(x => x != "Counter")) { var collection = _database.GetCollection("Counter"); collection.InsertOne(new BsonDocument { { "_id", _recieved }, { "sequence_value", 0 } });