From 05125fc6ae658cb887165952cb8c5b781a596906 Mon Sep 17 00:00:00 2001 From: yangxiaodong Date: Fri, 23 Jun 2017 16:47:19 +0800 Subject: [PATCH] Refactor. --- .../IProcessor.Producer.cs | 4 +-- .../RabbitMQProducerClient.cs | 4 +-- .../Abstractions/IConsumerServiceSelector.cs | 28 +++++++++++++++++++ .../ConsistencyOptions.cs => CAP.Options.cs} | 2 +- .../CAP.ServiceCollectionExtensions.cs | 9 +++--- .../{TopicContext.cs => CapStartContext.cs} | 6 ++-- src/DotNetCore.CAP/IBootstrapper.Default.cs | 4 +-- .../IConsumerHandler.Default.cs | 8 +++--- .../IConsumerExcutorSelector.cs | 12 -------- ...cs => IConsumerServiceSelector.Default.cs} | 23 +++++++++++---- .../Internal/MethodMatcherCache.cs | 6 ++-- .../Job/CronJobRegistry.Default.cs | 4 +-- .../Job/IProcessingServer.Job.cs | 4 +-- 13 files changed, 72 insertions(+), 42 deletions(-) create mode 100644 src/DotNetCore.CAP/Abstractions/IConsumerServiceSelector.cs rename src/DotNetCore.CAP/{Infrastructure/ConsistencyOptions.cs => CAP.Options.cs} (91%) rename src/DotNetCore.CAP/{TopicContext.cs => CapStartContext.cs} (67%) delete mode 100644 src/DotNetCore.CAP/Infrastructure/IConsumerExcutorSelector.cs rename src/DotNetCore.CAP/Internal/{ConsumerExcutorSelector.cs => IConsumerServiceSelector.Default.cs} (71%) diff --git a/src/DotNetCore.CAP.Kafka/IProcessor.Producer.cs b/src/DotNetCore.CAP.Kafka/IProcessor.Producer.cs index 316a10c..27e8773 100644 --- a/src/DotNetCore.CAP.Kafka/IProcessor.Producer.cs +++ b/src/DotNetCore.CAP.Kafka/IProcessor.Producer.cs @@ -16,7 +16,7 @@ namespace DotNetCore.CAP.Kafka { public class KafkaJobProcessor : IJobProcessor { - private readonly ConsistencyOptions _options; + private readonly CapOptions _options; private readonly CancellationTokenSource _cts; private readonly IServiceProvider _provider; @@ -26,7 +26,7 @@ namespace DotNetCore.CAP.Kafka private TimeSpan _pollingDelay; public KafkaJobProcessor( - IOptions options, + IOptions options, ILogger logger, IServiceProvider provider) { diff --git a/src/DotNetCore.CAP.RabbitMQ/RabbitMQProducerClient.cs b/src/DotNetCore.CAP.RabbitMQ/RabbitMQProducerClient.cs index 42a26b7..45ae39d 100644 --- a/src/DotNetCore.CAP.RabbitMQ/RabbitMQProducerClient.cs +++ b/src/DotNetCore.CAP.RabbitMQ/RabbitMQProducerClient.cs @@ -9,10 +9,10 @@ namespace DotNetCore.CAP.RabbitMQ { public class RabbitMQProducerClient : ICapProducerService { - private readonly ConsistencyOptions _options; + private readonly CapOptions _options; private readonly ILogger _logger; - public RabbitMQProducerClient(IOptions options, ILoggerFactory loggerFactory) + public RabbitMQProducerClient(IOptions options, ILoggerFactory loggerFactory) { _options = options.Value; _logger = loggerFactory.CreateLogger(nameof(RabbitMQProducerClient)); diff --git a/src/DotNetCore.CAP/Abstractions/IConsumerServiceSelector.cs b/src/DotNetCore.CAP/Abstractions/IConsumerServiceSelector.cs new file mode 100644 index 0000000..d987025 --- /dev/null +++ b/src/DotNetCore.CAP/Abstractions/IConsumerServiceSelector.cs @@ -0,0 +1,28 @@ +using System.Collections.Generic; +using DotNetCore.CAP.Abstractions; + +namespace DotNetCore.CAP.Abstractions +{ + /// + /// Defines an interface for selecting an cosumer service method to invoke for the current message. + /// + public interface IConsumerServiceSelector + { + /// + /// Selects a set of candidates for the current message associated with + /// . + /// + /// The associated with the current message. + /// A set of candidates or null. + IReadOnlyList SelectCandidates(CapStartContext context); + + /// + /// Selects the best candidate from for the + /// current message associated with . + /// + /// topic or exchange router key. + /// the set of candidates. + /// + ConsumerExecutorDescriptor SelectBestCandidate(string key, IReadOnlyList candidates); + } +} \ No newline at end of file diff --git a/src/DotNetCore.CAP/Infrastructure/ConsistencyOptions.cs b/src/DotNetCore.CAP/CAP.Options.cs similarity index 91% rename from src/DotNetCore.CAP/Infrastructure/ConsistencyOptions.cs rename to src/DotNetCore.CAP/CAP.Options.cs index 3a0fe12..c1c24f9 100644 --- a/src/DotNetCore.CAP/Infrastructure/ConsistencyOptions.cs +++ b/src/DotNetCore.CAP/CAP.Options.cs @@ -5,7 +5,7 @@ namespace DotNetCore.CAP.Infrastructure /// /// Represents all the options you can use to configure the system. /// - public class ConsistencyOptions + public class CapOptions { public string BrokerUrlList { get; set; } = "localhost:9092"; diff --git a/src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs b/src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs index 8a3ebc0..009ad00 100644 --- a/src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs +++ b/src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Reflection; using DotNetCore.CAP; +using DotNetCore.CAP.Abstractions; using DotNetCore.CAP.Abstractions.ModelBinding; using DotNetCore.CAP.Infrastructure; using DotNetCore.CAP.Internal; @@ -22,7 +23,7 @@ namespace Microsoft.Extensions.DependencyInjection /// An for application services. public static CapBuilder AddConsistency(this IServiceCollection services) { - services.AddConsistency(x => new ConsistencyOptions()); + services.AddConsistency(x => new CapOptions()); return new CapBuilder(services); } @@ -31,18 +32,18 @@ namespace Microsoft.Extensions.DependencyInjection /// Adds and configures the consistence services for the consitence. /// /// The services available in the application. - /// An action to configure the . + /// An action to configure the . /// An for application services. public static CapBuilder AddConsistency( this IServiceCollection services, - Action setupAction) + Action setupAction) { services.TryAddSingleton(); services.Configure(setupAction); AddConsumerServices(services); - services.TryAddSingleton(); + services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); diff --git a/src/DotNetCore.CAP/TopicContext.cs b/src/DotNetCore.CAP/CapStartContext.cs similarity index 67% rename from src/DotNetCore.CAP/TopicContext.cs rename to src/DotNetCore.CAP/CapStartContext.cs index 90a3990..9b0884d 100644 --- a/src/DotNetCore.CAP/TopicContext.cs +++ b/src/DotNetCore.CAP/CapStartContext.cs @@ -3,13 +3,13 @@ using System.Threading; namespace DotNetCore.CAP { - public class TopicContext + public class CapStartContext { - public TopicContext() + public CapStartContext() { } - public TopicContext(IServiceProvider provider, CancellationToken cancellationToken) + public CapStartContext(IServiceProvider provider, CancellationToken cancellationToken) { ServiceProvider = provider; CancellationToken = cancellationToken; diff --git a/src/DotNetCore.CAP/IBootstrapper.Default.cs b/src/DotNetCore.CAP/IBootstrapper.Default.cs index 81f1239..8ae8331 100644 --- a/src/DotNetCore.CAP/IBootstrapper.Default.cs +++ b/src/DotNetCore.CAP/IBootstrapper.Default.cs @@ -17,7 +17,7 @@ namespace DotNetCore.CAP private Task _bootstrappingTask; public DefaultBootstrapper( - IOptions options, + IOptions options, ICapMessageStore storage, IApplicationLifetime appLifetime, IServiceProvider provider) @@ -41,7 +41,7 @@ namespace DotNetCore.CAP }); } - protected ConsistencyOptions Options { get; } + protected CapOptions Options { get; } protected ICapMessageStore Storage { get; } diff --git a/src/DotNetCore.CAP/IConsumerHandler.Default.cs b/src/DotNetCore.CAP/IConsumerHandler.Default.cs index b25d3ab..83cf97a 100644 --- a/src/DotNetCore.CAP/IConsumerHandler.Default.cs +++ b/src/DotNetCore.CAP/IConsumerHandler.Default.cs @@ -20,13 +20,13 @@ namespace DotNetCore.CAP private readonly ILogger _logger; private readonly MethodMatcherCache _selector; - private readonly ConsistencyOptions _options; + private readonly CapOptions _options; private readonly ICapMessageStore _messageStore; private readonly CancellationTokenSource _cts; public event EventHandler MessageReceieved; - private TopicContext _context; + private CapStartContext _context; private Task _compositeTask; private bool _disposed; @@ -37,7 +37,7 @@ namespace DotNetCore.CAP ILoggerFactory loggerFactory, ICapMessageStore messageStore, MethodMatcherCache selector, - IOptions options) { + IOptions options) { _selector = selector; _logger = loggerFactory.CreateLogger(); _loggerFactory = loggerFactory; @@ -54,7 +54,7 @@ namespace DotNetCore.CAP } public void Start() { - _context = new TopicContext(_serviceProvider, _cts.Token); + _context = new CapStartContext(_serviceProvider, _cts.Token); var matchs = _selector.GetCandidatesMethods(_context); diff --git a/src/DotNetCore.CAP/Infrastructure/IConsumerExcutorSelector.cs b/src/DotNetCore.CAP/Infrastructure/IConsumerExcutorSelector.cs deleted file mode 100644 index a23bfd5..0000000 --- a/src/DotNetCore.CAP/Infrastructure/IConsumerExcutorSelector.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.Collections.Generic; -using DotNetCore.CAP.Abstractions; - -namespace DotNetCore.CAP.Infrastructure -{ - public interface IConsumerExcutorSelector - { - IReadOnlyList SelectCandidates(TopicContext context); - - ConsumerExecutorDescriptor SelectBestCandidate(string key, IReadOnlyList executeDescriptor); - } -} \ No newline at end of file diff --git a/src/DotNetCore.CAP/Internal/ConsumerExcutorSelector.cs b/src/DotNetCore.CAP/Internal/IConsumerServiceSelector.Default.cs similarity index 71% rename from src/DotNetCore.CAP/Internal/ConsumerExcutorSelector.cs rename to src/DotNetCore.CAP/Internal/IConsumerServiceSelector.Default.cs index 14bfece..d2fcaf0 100644 --- a/src/DotNetCore.CAP/Internal/ConsumerExcutorSelector.cs +++ b/src/DotNetCore.CAP/Internal/IConsumerServiceSelector.Default.cs @@ -3,26 +3,39 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; using DotNetCore.CAP.Abstractions; -using DotNetCore.CAP.Infrastructure; using Microsoft.Extensions.DependencyInjection; namespace DotNetCore.CAP.Internal { - public class ConsumerExcutorSelector : IConsumerExcutorSelector + /// + /// A default implementation. + /// + public class DefaultConsumerServiceSelector : IConsumerServiceSelector { private readonly IServiceProvider _serviceProvider; - public ConsumerExcutorSelector(IServiceProvider serviceProvider) + /// + /// Creates a new . + /// + /// + public DefaultConsumerServiceSelector(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; } - public ConsumerExecutorDescriptor SelectBestCandidate(string key, IReadOnlyList executeDescriptor) + /// + /// + /// + /// + /// + /// + public ConsumerExecutorDescriptor SelectBestCandidate(string key, + IReadOnlyList executeDescriptor) { return executeDescriptor.FirstOrDefault(x => x.Attribute.Name == key); } - public IReadOnlyList SelectCandidates(TopicContext context) + public IReadOnlyList SelectCandidates(CapStartContext context) { var consumerServices = context.ServiceProvider.GetServices(); diff --git a/src/DotNetCore.CAP/Internal/MethodMatcherCache.cs b/src/DotNetCore.CAP/Internal/MethodMatcherCache.cs index 06aef65..da742d5 100644 --- a/src/DotNetCore.CAP/Internal/MethodMatcherCache.cs +++ b/src/DotNetCore.CAP/Internal/MethodMatcherCache.cs @@ -7,14 +7,14 @@ namespace DotNetCore.CAP.Internal { public class MethodMatcherCache { - private readonly IConsumerExcutorSelector _selector; + private readonly IConsumerServiceSelector _selector; - public MethodMatcherCache(IConsumerExcutorSelector selector) + public MethodMatcherCache(IConsumerServiceSelector selector) { _selector = selector; } - public ConcurrentDictionary GetCandidatesMethods(TopicContext routeContext) + public ConcurrentDictionary GetCandidatesMethods(CapStartContext routeContext) { if (Entries.Count == 0) { diff --git a/src/DotNetCore.CAP/Job/CronJobRegistry.Default.cs b/src/DotNetCore.CAP/Job/CronJobRegistry.Default.cs index e124f73..88a4eea 100644 --- a/src/DotNetCore.CAP/Job/CronJobRegistry.Default.cs +++ b/src/DotNetCore.CAP/Job/CronJobRegistry.Default.cs @@ -5,9 +5,9 @@ namespace DotNetCore.CAP.Job { public class DefaultCronJobRegistry : CronJobRegistry { - private readonly ConsistencyOptions _options; + private readonly CapOptions _options; - public DefaultCronJobRegistry(IOptions options) + public DefaultCronJobRegistry(IOptions options) { _options = options.Value; diff --git a/src/DotNetCore.CAP/Job/IProcessingServer.Job.cs b/src/DotNetCore.CAP/Job/IProcessingServer.Job.cs index bffbc83..3248bcc 100644 --- a/src/DotNetCore.CAP/Job/IProcessingServer.Job.cs +++ b/src/DotNetCore.CAP/Job/IProcessingServer.Job.cs @@ -18,7 +18,7 @@ namespace DotNetCore.CAP private IServiceProvider _provider; private IJobProcessor[] _processors; private CancellationTokenSource _cts; - private ConsistencyOptions _options; + private CapOptions _options; private ProcessingContext _context; private DefaultCronJobRegistry _defaultJobRegistry; private Task _compositeTask; @@ -29,7 +29,7 @@ namespace DotNetCore.CAP ILoggerFactory loggerFactory, IServiceProvider provider, DefaultCronJobRegistry defaultJobRegistry, - IOptions options) + IOptions options) { _logger = logger; _loggerFactory = loggerFactory;