From 90f14f6562466287222dfd1c673c82c3793ade7d Mon Sep 17 00:00:00 2001 From: yangxiaodong Date: Mon, 17 Jul 2017 11:28:56 +0800 Subject: [PATCH] cleanup code. --- .../CAP.EFOptions.cs | 1 + .../CAP.Options.Extensions.cs | 1 + .../CAP.SqlServerCapOptionsExtension.cs | 3 ++- .../CAP.SqlServerOptions.cs | 5 ++-- .../SqlServerFetchedMessage.cs | 1 + .../SqlServerStorage.cs | 8 +++---- .../SqlServerStorageConnection.cs | 4 +--- .../SqlServerStorageTransaction.cs | 13 ++++------ .../CAP.KafkaCapOptionsExtension.cs | 5 ++-- src/DotNetCore.CAP.Kafka/CAP.KafkaOptions.cs | 1 + .../CAP.Options.Extensions.cs | 1 + .../CAP.Options.Extensions.cs | 1 + .../CAP.RabbiMQOptions.cs | 5 ++-- .../CAP.RabbitMQCapOptionsExtension.cs | 3 ++- .../PublishQueueExecutor.cs | 24 +++++++++---------- .../RabbitMQConsumerClient.cs | 2 +- .../IQueueExecutor.Publish.Base.cs | 2 +- src/DotNetCore.CAP/IQueueExecutor.Subscibe.cs | 3 ++- src/DotNetCore.CAP/QueueExecutorFactory.cs | 13 ++++------ 19 files changed, 49 insertions(+), 47 deletions(-) diff --git a/src/DotNetCore.CAP.EntityFrameworkCore/CAP.EFOptions.cs b/src/DotNetCore.CAP.EntityFrameworkCore/CAP.EFOptions.cs index a742b2a..68aa524 100644 --- a/src/DotNetCore.CAP.EntityFrameworkCore/CAP.EFOptions.cs +++ b/src/DotNetCore.CAP.EntityFrameworkCore/CAP.EFOptions.cs @@ -1,5 +1,6 @@ using System; +// ReSharper disable once CheckNamespace namespace DotNetCore.CAP { public class EFOptions diff --git a/src/DotNetCore.CAP.EntityFrameworkCore/CAP.Options.Extensions.cs b/src/DotNetCore.CAP.EntityFrameworkCore/CAP.Options.Extensions.cs index 1f2f807..0c1b6f8 100644 --- a/src/DotNetCore.CAP.EntityFrameworkCore/CAP.Options.Extensions.cs +++ b/src/DotNetCore.CAP.EntityFrameworkCore/CAP.Options.Extensions.cs @@ -2,6 +2,7 @@ using DotNetCore.CAP; using Microsoft.EntityFrameworkCore; +// ReSharper disable once CheckNamespace namespace Microsoft.Extensions.DependencyInjection { public static class CapOptionsExtensions diff --git a/src/DotNetCore.CAP.EntityFrameworkCore/CAP.SqlServerCapOptionsExtension.cs b/src/DotNetCore.CAP.EntityFrameworkCore/CAP.SqlServerCapOptionsExtension.cs index 8f60f34..204cc49 100644 --- a/src/DotNetCore.CAP.EntityFrameworkCore/CAP.SqlServerCapOptionsExtension.cs +++ b/src/DotNetCore.CAP.EntityFrameworkCore/CAP.SqlServerCapOptionsExtension.cs @@ -3,11 +3,12 @@ using DotNetCore.CAP.EntityFrameworkCore; using DotNetCore.CAP.Processor; using Microsoft.Extensions.DependencyInjection; +// ReSharper disable once CheckNamespace namespace DotNetCore.CAP { public class SqlServerCapOptionsExtension : ICapOptionsExtension { - private Action _configure; + private readonly Action _configure; public SqlServerCapOptionsExtension(Action configure) { diff --git a/src/DotNetCore.CAP.EntityFrameworkCore/CAP.SqlServerOptions.cs b/src/DotNetCore.CAP.EntityFrameworkCore/CAP.SqlServerOptions.cs index 40a2d4f..a0f3fa2 100644 --- a/src/DotNetCore.CAP.EntityFrameworkCore/CAP.SqlServerOptions.cs +++ b/src/DotNetCore.CAP.EntityFrameworkCore/CAP.SqlServerOptions.cs @@ -1,10 +1,11 @@ -namespace DotNetCore.CAP +// ReSharper disable once CheckNamespace +namespace DotNetCore.CAP { public class SqlServerOptions : EFOptions { /// /// Gets or sets the database's connection string that will be used to store database entities. /// - public string ConnectionString { get; set; } //= "Server=DESKTOP-M9R8T31;Initial Catalog=Test;User Id=sa;Password=P@ssw0rd;MultipleActiveResultSets=True"; + public string ConnectionString { get; set; } } } \ No newline at end of file diff --git a/src/DotNetCore.CAP.EntityFrameworkCore/SqlServerFetchedMessage.cs b/src/DotNetCore.CAP.EntityFrameworkCore/SqlServerFetchedMessage.cs index e93167d..6e9d8e2 100644 --- a/src/DotNetCore.CAP.EntityFrameworkCore/SqlServerFetchedMessage.cs +++ b/src/DotNetCore.CAP.EntityFrameworkCore/SqlServerFetchedMessage.cs @@ -66,6 +66,7 @@ namespace DotNetCore.CAP.EntityFrameworkCore } catch { + // ignored } } } diff --git a/src/DotNetCore.CAP.EntityFrameworkCore/SqlServerStorage.cs b/src/DotNetCore.CAP.EntityFrameworkCore/SqlServerStorage.cs index b5b73bb..8be57eb 100644 --- a/src/DotNetCore.CAP.EntityFrameworkCore/SqlServerStorage.cs +++ b/src/DotNetCore.CAP.EntityFrameworkCore/SqlServerStorage.cs @@ -11,8 +11,8 @@ namespace DotNetCore.CAP.EntityFrameworkCore { public class SqlServerStorage : IStorage { - private IServiceProvider _provider; - private ILogger _logger; + private readonly IServiceProvider _provider; + private readonly ILogger _logger; public SqlServerStorage( IServiceProvider provider, @@ -43,7 +43,7 @@ namespace DotNetCore.CAP.EntityFrameworkCore protected virtual string CreateDbTablesScript(string schema) { - var batchSQL = + var batchSql = $@" IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '{schema}') BEGIN @@ -96,7 +96,7 @@ CREATE TABLE [{schema}].[Published]( ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] END GO"; - return batchSQL; + return batchSql; } } } \ No newline at end of file diff --git a/src/DotNetCore.CAP.EntityFrameworkCore/SqlServerStorageConnection.cs b/src/DotNetCore.CAP.EntityFrameworkCore/SqlServerStorageConnection.cs index 96929c6..eee5ac7 100644 --- a/src/DotNetCore.CAP.EntityFrameworkCore/SqlServerStorageConnection.cs +++ b/src/DotNetCore.CAP.EntityFrameworkCore/SqlServerStorageConnection.cs @@ -95,15 +95,13 @@ VALUES(@Name,@Group,@Content,@Retries,@Added,@ExpiresAt,@StatusName);"; private async Task FetchNextMessageCoreAsync(string sql, object args = null) { - FetchedMessage fetched = null; - using (var connection = new SqlConnection(_options.ConnectionString)) { using (var transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted)) { try { - fetched = await connection.QueryFirstOrDefaultAsync(sql, args, transaction); + var fetched = await connection.QueryFirstOrDefaultAsync(sql, args, transaction); if (fetched == null) return null; diff --git a/src/DotNetCore.CAP.EntityFrameworkCore/SqlServerStorageTransaction.cs b/src/DotNetCore.CAP.EntityFrameworkCore/SqlServerStorageTransaction.cs index e0cdcf3..81dd805 100644 --- a/src/DotNetCore.CAP.EntityFrameworkCore/SqlServerStorageTransaction.cs +++ b/src/DotNetCore.CAP.EntityFrameworkCore/SqlServerStorageTransaction.cs @@ -9,20 +9,17 @@ namespace DotNetCore.CAP.EntityFrameworkCore { public class SqlServerStorageTransaction : IStorageTransaction, IDisposable { - private readonly SqlServerStorageConnection _connection; - private readonly SqlServerOptions _options; private readonly string _schema; - private IDbTransaction _dbTransaction; - private IDbConnection _dbConnection; + private readonly IDbTransaction _dbTransaction; + private readonly IDbConnection _dbConnection; public SqlServerStorageTransaction(SqlServerStorageConnection connection) { - _connection = connection; - _options = _connection.Options; - _schema = _options.Schema; + var options = connection.Options; + _schema = options.Schema; - _dbConnection = new SqlConnection(_options.ConnectionString); + _dbConnection = new SqlConnection(options.ConnectionString); _dbTransaction = _dbConnection.BeginTransaction(IsolationLevel.ReadCommitted); } diff --git a/src/DotNetCore.CAP.Kafka/CAP.KafkaCapOptionsExtension.cs b/src/DotNetCore.CAP.Kafka/CAP.KafkaCapOptionsExtension.cs index 42df3f8..2dbf888 100644 --- a/src/DotNetCore.CAP.Kafka/CAP.KafkaCapOptionsExtension.cs +++ b/src/DotNetCore.CAP.Kafka/CAP.KafkaCapOptionsExtension.cs @@ -2,11 +2,12 @@ using DotNetCore.CAP.Kafka; using Microsoft.Extensions.DependencyInjection; +// ReSharper disable once CheckNamespace namespace DotNetCore.CAP { public class KafkaCapOptionsExtension : ICapOptionsExtension { - private Action _configure; + private readonly Action _configure; public KafkaCapOptionsExtension(Action configure) { @@ -20,7 +21,7 @@ namespace DotNetCore.CAP var kafkaOptions = new KafkaOptions(); _configure(kafkaOptions); services.AddSingleton(kafkaOptions); - + services.AddSingleton(); services.AddTransient(); } diff --git a/src/DotNetCore.CAP.Kafka/CAP.KafkaOptions.cs b/src/DotNetCore.CAP.Kafka/CAP.KafkaOptions.cs index 55b055b..34cf92d 100644 --- a/src/DotNetCore.CAP.Kafka/CAP.KafkaOptions.cs +++ b/src/DotNetCore.CAP.Kafka/CAP.KafkaOptions.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; +// ReSharper disable once CheckNamespace namespace DotNetCore.CAP { /// diff --git a/src/DotNetCore.CAP.Kafka/CAP.Options.Extensions.cs b/src/DotNetCore.CAP.Kafka/CAP.Options.Extensions.cs index c562251..65314f4 100644 --- a/src/DotNetCore.CAP.Kafka/CAP.Options.Extensions.cs +++ b/src/DotNetCore.CAP.Kafka/CAP.Options.Extensions.cs @@ -1,6 +1,7 @@ using System; using DotNetCore.CAP; +// ReSharper disable once CheckNamespace namespace Microsoft.Extensions.DependencyInjection { public static class CapOptionsExtensions diff --git a/src/DotNetCore.CAP.RabbitMQ/CAP.Options.Extensions.cs b/src/DotNetCore.CAP.RabbitMQ/CAP.Options.Extensions.cs index 9320be4..4d59112 100644 --- a/src/DotNetCore.CAP.RabbitMQ/CAP.Options.Extensions.cs +++ b/src/DotNetCore.CAP.RabbitMQ/CAP.Options.Extensions.cs @@ -1,6 +1,7 @@ using System; using DotNetCore.CAP; +// ReSharper disable once CheckNamespace namespace Microsoft.Extensions.DependencyInjection { public static class CapOptionsExtensions diff --git a/src/DotNetCore.CAP.RabbitMQ/CAP.RabbiMQOptions.cs b/src/DotNetCore.CAP.RabbitMQ/CAP.RabbiMQOptions.cs index ddce147..09a90ef 100644 --- a/src/DotNetCore.CAP.RabbitMQ/CAP.RabbiMQOptions.cs +++ b/src/DotNetCore.CAP.RabbitMQ/CAP.RabbiMQOptions.cs @@ -1,4 +1,5 @@ -namespace DotNetCore.CAP +// ReSharper disable once CheckNamespace +namespace DotNetCore.CAP { public class RabbitMQOptions { @@ -34,7 +35,7 @@ public string HostName { get; set; } = "localhost"; /// The topic exchange type. - internal string EXCHANGE_TYPE = "topic"; + internal const string ExchangeType = "topic"; /// /// Password to use when authenticating to the server. diff --git a/src/DotNetCore.CAP.RabbitMQ/CAP.RabbitMQCapOptionsExtension.cs b/src/DotNetCore.CAP.RabbitMQ/CAP.RabbitMQCapOptionsExtension.cs index ee677c8..14eedbb 100644 --- a/src/DotNetCore.CAP.RabbitMQ/CAP.RabbitMQCapOptionsExtension.cs +++ b/src/DotNetCore.CAP.RabbitMQ/CAP.RabbitMQCapOptionsExtension.cs @@ -2,11 +2,12 @@ using DotNetCore.CAP.RabbitMQ; using Microsoft.Extensions.DependencyInjection; +// ReSharper disable once CheckNamespace namespace DotNetCore.CAP { public class RabbitMQCapOptionsExtension : ICapOptionsExtension { - private Action _configure; + private readonly Action _configure; public RabbitMQCapOptionsExtension(Action configure) { diff --git a/src/DotNetCore.CAP.RabbitMQ/PublishQueueExecutor.cs b/src/DotNetCore.CAP.RabbitMQ/PublishQueueExecutor.cs index 80f0cfb..3e439e2 100644 --- a/src/DotNetCore.CAP.RabbitMQ/PublishQueueExecutor.cs +++ b/src/DotNetCore.CAP.RabbitMQ/PublishQueueExecutor.cs @@ -11,7 +11,7 @@ namespace DotNetCore.CAP.RabbitMQ public class PublishQueueExecutor : BasePublishQueueExecutor { private readonly ILogger _logger; - private readonly RabbitMQOptions _rabbitMqOptions; + private readonly RabbitMQOptions _rabbitMQOptions; public PublishQueueExecutor(IStateChanger stateChanger, IOptions options, @@ -19,21 +19,21 @@ namespace DotNetCore.CAP.RabbitMQ : base(stateChanger, logger) { _logger = logger; - _rabbitMqOptions = options.Value; + _rabbitMQOptions = options.Value; } public override Task PublishAsync(string keyName, string content) { var factory = new ConnectionFactory() { - HostName = _rabbitMqOptions.HostName, - UserName = _rabbitMqOptions.UserName, - Port = _rabbitMqOptions.Port, - Password = _rabbitMqOptions.Password, - VirtualHost = _rabbitMqOptions.VirtualHost, - RequestedConnectionTimeout = _rabbitMqOptions.RequestedConnectionTimeout, - SocketReadTimeout = _rabbitMqOptions.SocketReadTimeout, - SocketWriteTimeout = _rabbitMqOptions.SocketWriteTimeout + HostName = _rabbitMQOptions.HostName, + UserName = _rabbitMQOptions.UserName, + Port = _rabbitMQOptions.Port, + Password = _rabbitMQOptions.Password, + VirtualHost = _rabbitMQOptions.VirtualHost, + RequestedConnectionTimeout = _rabbitMQOptions.RequestedConnectionTimeout, + SocketReadTimeout = _rabbitMQOptions.SocketReadTimeout, + SocketWriteTimeout = _rabbitMQOptions.SocketWriteTimeout }; try @@ -43,8 +43,8 @@ namespace DotNetCore.CAP.RabbitMQ { var body = Encoding.UTF8.GetBytes(content); - channel.ExchangeDeclare(_rabbitMqOptions.TopicExchangeName, _rabbitMqOptions.EXCHANGE_TYPE); - channel.BasicPublish(exchange: _rabbitMqOptions.TopicExchangeName, + channel.ExchangeDeclare(_rabbitMQOptions.TopicExchangeName, RabbitMQOptions.ExchangeType); + channel.BasicPublish(exchange: _rabbitMQOptions.TopicExchangeName, routingKey: keyName, basicProperties: null, body: body); diff --git a/src/DotNetCore.CAP.RabbitMQ/RabbitMQConsumerClient.cs b/src/DotNetCore.CAP.RabbitMQ/RabbitMQConsumerClient.cs index 9ca8a86..7e19de1 100644 --- a/src/DotNetCore.CAP.RabbitMQ/RabbitMQConsumerClient.cs +++ b/src/DotNetCore.CAP.RabbitMQ/RabbitMQConsumerClient.cs @@ -46,7 +46,7 @@ namespace DotNetCore.CAP.RabbitMQ _connection = _connectionFactory.CreateConnection(); _channel = _connection.CreateModel(); - _channel.ExchangeDeclare(exchange: _exchageName, type: _rabbitMQOptions.EXCHANGE_TYPE); + _channel.ExchangeDeclare(exchange: _exchageName, type: _rabbitMQOptions.ExchangeType); _channel.QueueDeclare(_queueName, exclusive: false); } diff --git a/src/DotNetCore.CAP/IQueueExecutor.Publish.Base.cs b/src/DotNetCore.CAP/IQueueExecutor.Publish.Base.cs index 074fd66..10fd2eb 100644 --- a/src/DotNetCore.CAP/IQueueExecutor.Publish.Base.cs +++ b/src/DotNetCore.CAP/IQueueExecutor.Publish.Base.cs @@ -13,7 +13,7 @@ namespace DotNetCore.CAP private readonly IStateChanger _stateChanger; private readonly ILogger _logger; - public BasePublishQueueExecutor(IStateChanger stateChanger, + protected BasePublishQueueExecutor(IStateChanger stateChanger, ILogger logger) { _stateChanger = stateChanger; diff --git a/src/DotNetCore.CAP/IQueueExecutor.Subscibe.cs b/src/DotNetCore.CAP/IQueueExecutor.Subscibe.cs index e771455..503f2f3 100644 --- a/src/DotNetCore.CAP/IQueueExecutor.Subscibe.cs +++ b/src/DotNetCore.CAP/IQueueExecutor.Subscibe.cs @@ -116,7 +116,8 @@ namespace DotNetCore.CAP } catch (SubscriberNotFoundException ex) { - throw ex; + _logger.LogError("Can not be found subscribe method of name: " + receivedMessage.Name); + return OperateResult.Failed(ex); } catch (Exception ex) { diff --git a/src/DotNetCore.CAP/QueueExecutorFactory.cs b/src/DotNetCore.CAP/QueueExecutorFactory.cs index 7790167..3e84684 100644 --- a/src/DotNetCore.CAP/QueueExecutorFactory.cs +++ b/src/DotNetCore.CAP/QueueExecutorFactory.cs @@ -17,16 +17,11 @@ namespace DotNetCore.CAP public IQueueExecutor GetInstance(MessageType messageType) { - var _queueExectors = _serviceProvider.GetServices(); + var queueExectors = _serviceProvider.GetServices(); - if (messageType == MessageType.Publish) - { - return _queueExectors.FirstOrDefault(x => typeof(BasePublishQueueExecutor).IsAssignableFrom(x.GetType())); - } - else - { - return _queueExectors.FirstOrDefault(x => !typeof(BasePublishQueueExecutor).IsAssignableFrom(x.GetType())); - } + return messageType == MessageType.Publish + ? queueExectors.FirstOrDefault(x => x is BasePublishQueueExecutor) + : queueExectors.FirstOrDefault(x => !(x is BasePublishQueueExecutor)); } } } \ No newline at end of file