From 308539032b6a4feee1d9d37a1d49de03be1cd984 Mon Sep 17 00:00:00 2001 From: Savorboard Date: Mon, 23 Jul 2018 16:22:58 +0800 Subject: [PATCH] =?UTF-8?q?tweak=20database=20configuration=20name=20=20to?= =?UTF-8?q?=20=E2=80=98DatabaseName=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CAP.MongoDBOptions.cs | 26 ++++++++++++++----- src/DotNetCore.CAP.MongoDB/CapPublisher.cs | 2 +- .../MongoDBCollectProcessor.cs | 4 +-- .../MongoDBMonitoringApi.cs | 2 +- src/DotNetCore.CAP.MongoDB/MongoDBStorage.cs | 8 +++--- .../MongoDBStorageConnection.cs | 2 +- .../MongoDBStorageTransaction.cs | 2 +- src/DotNetCore.CAP.MongoDB/MongoDBUtil.cs | 8 +++--- 8 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/DotNetCore.CAP.MongoDB/CAP.MongoDBOptions.cs b/src/DotNetCore.CAP.MongoDB/CAP.MongoDBOptions.cs index 93184aa..98ef69b 100644 --- a/src/DotNetCore.CAP.MongoDB/CAP.MongoDBOptions.cs +++ b/src/DotNetCore.CAP.MongoDB/CAP.MongoDBOptions.cs @@ -5,16 +5,30 @@ namespace DotNetCore.CAP.MongoDB { public class MongoDBOptions { - public const string DefaultDatabase = "Cap"; + /// + /// Gets or sets the database name to use when creating database objects. + /// Default value: "cap" + /// + public string DatabaseName { get; set; } = "cap"; /// - /// Gets or sets the database to use when creating database objects. - /// Default is . + /// MongoDB database connection string. + /// Default value: "mongodb://localhost:27017" /// - public string Database { get; set; } = DefaultDatabase; + public string DatabaseConnection { get; set; } = "mongodb://localhost:27017"; - public string ReceivedCollection { get; } = "Received"; + /// + /// MongoDB received message collection name. + /// Default value: "received" + /// + public string ReceivedCollection { get; set; } = "cap.received"; + + /// + /// MongoDB published message collection name. + /// Default value: "published" + /// + public string PublishedCollection { get; set; } = "cap.published"; - public string PublishedCollection { get; } = "Published"; + internal const string CounterCollection = "cap.counter"; } } \ No newline at end of file diff --git a/src/DotNetCore.CAP.MongoDB/CapPublisher.cs b/src/DotNetCore.CAP.MongoDB/CapPublisher.cs index 01f8746..475d947 100644 --- a/src/DotNetCore.CAP.MongoDB/CapPublisher.cs +++ b/src/DotNetCore.CAP.MongoDB/CapPublisher.cs @@ -28,7 +28,7 @@ namespace DotNetCore.CAP.MongoDB : base(logger, dispatcher) { _options = options; - _database = client.GetDatabase(_options.Database); + _database = client.GetDatabase(_options.DatabaseName); ServiceProvider = provider; } diff --git a/src/DotNetCore.CAP.MongoDB/MongoDBCollectProcessor.cs b/src/DotNetCore.CAP.MongoDB/MongoDBCollectProcessor.cs index eaca4ce..6b19878 100644 --- a/src/DotNetCore.CAP.MongoDB/MongoDBCollectProcessor.cs +++ b/src/DotNetCore.CAP.MongoDB/MongoDBCollectProcessor.cs @@ -23,13 +23,13 @@ namespace DotNetCore.CAP.MongoDB { _options = options; _logger = logger; - _database = client.GetDatabase(_options.Database); + _database = client.GetDatabase(_options.DatabaseName); } public async Task ProcessAsync(ProcessingContext context) { _logger.LogDebug( - $"Collecting expired data from collection [{_options.Database}].[{_options.PublishedCollection}]."); + $"Collecting expired data from collection [{_options.PublishedCollection}]."); var publishedCollection = _database.GetCollection(_options.PublishedCollection); var receivedCollection = _database.GetCollection(_options.ReceivedCollection); diff --git a/src/DotNetCore.CAP.MongoDB/MongoDBMonitoringApi.cs b/src/DotNetCore.CAP.MongoDB/MongoDBMonitoringApi.cs index 6146ff4..0553b1b 100644 --- a/src/DotNetCore.CAP.MongoDB/MongoDBMonitoringApi.cs +++ b/src/DotNetCore.CAP.MongoDB/MongoDBMonitoringApi.cs @@ -22,7 +22,7 @@ namespace DotNetCore.CAP.MongoDB var mongoClient = client ?? throw new ArgumentNullException(nameof(client)); _options = options ?? throw new ArgumentNullException(nameof(options)); - _database = mongoClient.GetDatabase(_options.Database); + _database = mongoClient.GetDatabase(_options.DatabaseName); } public StatisticsDto GetStatistics() diff --git a/src/DotNetCore.CAP.MongoDB/MongoDBStorage.cs b/src/DotNetCore.CAP.MongoDB/MongoDBStorage.cs index 3f1aaaf..24fa082 100644 --- a/src/DotNetCore.CAP.MongoDB/MongoDBStorage.cs +++ b/src/DotNetCore.CAP.MongoDB/MongoDBStorage.cs @@ -46,7 +46,7 @@ namespace DotNetCore.CAP.MongoDB return; } - var database = _client.GetDatabase(_options.Database); + var database = _client.GetDatabase(_options.DatabaseName); var names = (await database.ListCollectionNamesAsync(cancellationToken: cancellationToken))?.ToList(); if (!names.Any(n => n == _options.ReceivedCollection)) @@ -60,10 +60,10 @@ namespace DotNetCore.CAP.MongoDB cancellationToken: cancellationToken); } - if (names.All(n => n != "Counter")) + if (names.All(n => n != MongoDBOptions.CounterCollection)) { - await database.CreateCollectionAsync("Counter", cancellationToken: cancellationToken); - var collection = database.GetCollection("Counter"); + await database.CreateCollectionAsync(MongoDBOptions.CounterCollection, cancellationToken: cancellationToken); + var collection = database.GetCollection(MongoDBOptions.CounterCollection); await collection.InsertManyAsync(new[] { new BsonDocument {{"_id", _options.PublishedCollection}, {"sequence_value", 0}}, diff --git a/src/DotNetCore.CAP.MongoDB/MongoDBStorageConnection.cs b/src/DotNetCore.CAP.MongoDB/MongoDBStorageConnection.cs index 08bf04c..098ddf9 100644 --- a/src/DotNetCore.CAP.MongoDB/MongoDBStorageConnection.cs +++ b/src/DotNetCore.CAP.MongoDB/MongoDBStorageConnection.cs @@ -22,7 +22,7 @@ namespace DotNetCore.CAP.MongoDB _capOptions = capOptions; _options = options; _client = client; - _database = _client.GetDatabase(_options.Database); + _database = _client.GetDatabase(_options.DatabaseName); } public bool ChangePublishedState(int messageId, string state) diff --git a/src/DotNetCore.CAP.MongoDB/MongoDBStorageTransaction.cs b/src/DotNetCore.CAP.MongoDB/MongoDBStorageTransaction.cs index b648e0c..a2b2390 100644 --- a/src/DotNetCore.CAP.MongoDB/MongoDBStorageTransaction.cs +++ b/src/DotNetCore.CAP.MongoDB/MongoDBStorageTransaction.cs @@ -17,7 +17,7 @@ namespace DotNetCore.CAP.MongoDB public MongoDBStorageTransaction(IMongoClient client, MongoDBOptions options) { _options = options; - _database = client.GetDatabase(options.Database); + _database = client.GetDatabase(options.DatabaseName); _session = client.StartSession(); _session.StartTransaction(); } diff --git a/src/DotNetCore.CAP.MongoDB/MongoDBUtil.cs b/src/DotNetCore.CAP.MongoDB/MongoDBUtil.cs index c645dfd..3d3c1f7 100644 --- a/src/DotNetCore.CAP.MongoDB/MongoDBUtil.cs +++ b/src/DotNetCore.CAP.MongoDB/MongoDBUtil.cs @@ -19,10 +19,10 @@ namespace DotNetCore.CAP.MongoDB IClientSessionHandle session = null) { //https://www.tutorialspoint.com/mongodb/mongodb_autoincrement_sequence.htm - var collection = database.GetCollection("Counter"); + var collection = database.GetCollection(MongoDBOptions.CounterCollection); var updateDef = Builders.Update.Inc("sequence_value", 1); - var filter = new BsonDocument {{"_id", collectionName}}; + var filter = new BsonDocument { { "_id", collectionName } }; BsonDocument result; if (session == null) @@ -45,9 +45,9 @@ namespace DotNetCore.CAP.MongoDB public int GetNextSequenceValue(IMongoDatabase database, string collectionName, IClientSessionHandle session = null) { - var collection = database.GetCollection("Counter"); + var collection = database.GetCollection(MongoDBOptions.CounterCollection); - var filter = new BsonDocument {{"_id", collectionName}}; + var filter = new BsonDocument { { "_id", collectionName } }; var updateDef = Builders.Update.Inc("sequence_value", 1); var result = session == null