From eda2ef1305ad752cd9ed8769cbe010d912b3c148 Mon Sep 17 00:00:00 2001 From: yangxiaodong Date: Mon, 5 Jun 2017 11:36:57 +0800 Subject: [PATCH] refactor --- .../ConsistencyBuilder.cs | 16 ++++ .../ServiceCollectionExtensions.cs | 79 +++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 src/Cap.Consistency/Microsoft.Extensions.DependencyInjection/ConsistencyBuilder.cs create mode 100644 src/Cap.Consistency/Microsoft.Extensions.DependencyInjection/ServiceCollectionExtensions.cs diff --git a/src/Cap.Consistency/Microsoft.Extensions.DependencyInjection/ConsistencyBuilder.cs b/src/Cap.Consistency/Microsoft.Extensions.DependencyInjection/ConsistencyBuilder.cs new file mode 100644 index 0000000..30d7c7b --- /dev/null +++ b/src/Cap.Consistency/Microsoft.Extensions.DependencyInjection/ConsistencyBuilder.cs @@ -0,0 +1,16 @@ +namespace Microsoft.Extensions.DependencyInjection +{ + /// + /// Used to verify Consistency service was called on a ServiceCollection + /// + public class ConsistencyMarkerService { } + + public class ConsistencyBuilder + { + public ConsistencyBuilder(IServiceCollection services) { + Services = services; + } + + public IServiceCollection Services { get; private set; } + } +} \ No newline at end of file diff --git a/src/Cap.Consistency/Microsoft.Extensions.DependencyInjection/ServiceCollectionExtensions.cs b/src/Cap.Consistency/Microsoft.Extensions.DependencyInjection/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..279ee7d --- /dev/null +++ b/src/Cap.Consistency/Microsoft.Extensions.DependencyInjection/ServiceCollectionExtensions.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.Reflection; +using Cap.Consistency; +using Cap.Consistency.Abstractions.ModelBinding; +using Cap.Consistency.Consumer; +using Cap.Consistency.Infrastructure; +using Cap.Consistency.Internal; +using Cap.Consistency.Store; +using Microsoft.Extensions.DependencyInjection.Extensions; + +// ReSharper disable once CheckNamespace +namespace Microsoft.Extensions.DependencyInjection +{ + /// + /// Contains extension methods to for configuring consistence services. + /// + public static class ServiceCollectionExtensions + { + /// + /// Adds and configures the consistence services for the consitence. + /// + /// The services available in the application. + /// An for application services. + public static ConsistencyBuilder AddConsistency(this IServiceCollection services) { + services.AddConsistency(x => new ConsistencyOptions()); + + return new ConsistencyBuilder(services); + } + + /// + /// Adds and configures the consistence services for the consitence. + /// + /// The services available in the application. + /// An action to configure the . + /// An for application services. + public static ConsistencyBuilder AddConsistency(this IServiceCollection services, Action setupAction) { + services.TryAddSingleton(); + + services.TryAddSingleton(); + + services.Configure(setupAction); + + + var IConsumerListenerServices = new Dictionary(); + foreach (var rejectedServices in services) { + if (rejectedServices.ImplementationType != null && typeof(IConsumerService).IsAssignableFrom(rejectedServices.ImplementationType)) + IConsumerListenerServices.Add(typeof(IConsumerService), rejectedServices.ImplementationType); + } + + foreach (var service in IConsumerListenerServices) { + services.AddSingleton(service.Key, service.Value); + } + + var types = Assembly.GetEntryAssembly().ExportedTypes; + foreach (var type in types) { + if (typeof(IConsumerService).IsAssignableFrom(type)) { + services.AddSingleton(typeof(IConsumerService), type); + } + } + + services.TryAddSingleton(); + services.TryAddSingleton(); + services.TryAddSingleton(); + services.TryAddSingleton(); + + services.TryAddSingleton(typeof(ITopicRouteHandler), typeof(ConsumerHandler)); + + return new ConsistencyBuilder(services); + } + + + public static ConsistencyBuilder AddMessageStore(this ConsistencyBuilder build) + where T : class, IConsistencyMessageStore { + build.Services.AddScoped(); + return build; + } + } +} \ No newline at end of file