From da57138ba5d8ca1ca1fbbc12031dce0af97abf0c Mon Sep 17 00:00:00 2001 From: yangxiaodong Date: Wed, 21 Jun 2017 16:07:42 +0800 Subject: [PATCH] add jobs services to DI. --- .../ConsistencyBuilder.cs | 35 +++++++++++- .../ServiceCollectionExtensions.cs | 56 +++++++++++-------- 2 files changed, 66 insertions(+), 25 deletions(-) diff --git a/src/Cap.Consistency/Microsoft.Extensions.DependencyInjection/ConsistencyBuilder.cs b/src/Cap.Consistency/Microsoft.Extensions.DependencyInjection/ConsistencyBuilder.cs index 30d7c7b..9245cb9 100644 --- a/src/Cap.Consistency/Microsoft.Extensions.DependencyInjection/ConsistencyBuilder.cs +++ b/src/Cap.Consistency/Microsoft.Extensions.DependencyInjection/ConsistencyBuilder.cs @@ -1,4 +1,8 @@ -namespace Microsoft.Extensions.DependencyInjection +using System; +using Cap.Consistency.Job; +using Cap.Consistency.Store; + +namespace Microsoft.Extensions.DependencyInjection { /// /// Used to verify Consistency service was called on a ServiceCollection @@ -12,5 +16,34 @@ } public IServiceCollection Services { get; private set; } + + private ConsistencyBuilder AddScoped(Type serviceType, Type concreteType) { + Services.AddScoped(serviceType, concreteType); + return this; + } + + private ConsistencyBuilder AddSingleton() + where TService : class + where TImplementation : class, TService { + Services.AddSingleton(); + return this; + } + + /// + /// Adds an . + /// + /// The type for the to add. + /// The current instance. + public virtual ConsistencyBuilder AddMessageStore() + where T : class, IConsistencyMessageStore { + + return AddScoped(typeof(IConsistencyMessageStore), typeof(T)); + } + + public virtual ConsistencyBuilder AddJobs() + where T : class, IJob { + + return AddSingleton(); + } } } \ 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 index a93893a..561683d 100644 --- a/src/Cap.Consistency/Microsoft.Extensions.DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/Cap.Consistency/Microsoft.Extensions.DependencyInjection/ServiceCollectionExtensions.cs @@ -6,10 +6,10 @@ using Cap.Consistency.Abstractions.ModelBinding; using Cap.Consistency.Consumer; using Cap.Consistency.Infrastructure; using Cap.Consistency.Internal; +using Cap.Consistency.Job; using Cap.Consistency.Store; using Microsoft.Extensions.DependencyInjection.Extensions; -// ReSharper disable once CheckNamespace namespace Microsoft.Extensions.DependencyInjection { /// @@ -34,18 +34,43 @@ namespace Microsoft.Extensions.DependencyInjection /// 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(); + public static ConsistencyBuilder AddConsistency( + this IServiceCollection services, + Action setupAction) { + services.TryAddSingleton(); services.Configure(setupAction); - var IConsumerListenerServices = new Dictionary(); + AddConsumerServices(services); + + services.TryAddSingleton(); + services.TryAddSingleton(); + services.TryAddSingleton(); + services.TryAddSingleton(); + + services.TryAddScoped(); + + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + + services.TryAddTransient(); + services.TryAddSingleton(); + services.TryAddTransient(); + + return new ConsistencyBuilder(services); + } + + private static void AddConsumerServices(IServiceCollection services) { + var consumerListenerServices = new Dictionary(); foreach (var rejectedServices in services) { - if (rejectedServices.ImplementationType != null && typeof(IConsumerService).IsAssignableFrom(rejectedServices.ImplementationType)) - IConsumerListenerServices.Add(typeof(IConsumerService), rejectedServices.ImplementationType); + if (rejectedServices.ImplementationType != null + && typeof(IConsumerService).IsAssignableFrom(rejectedServices.ImplementationType)) + + consumerListenerServices.Add(typeof(IConsumerService), rejectedServices.ImplementationType); } - foreach (var service in IConsumerListenerServices) { + foreach (var service in consumerListenerServices) { services.AddSingleton(service.Key, service.Value); } @@ -55,23 +80,6 @@ namespace Microsoft.Extensions.DependencyInjection services.AddSingleton(typeof(IConsumerService), type); } } - - services.TryAddSingleton(); - services.TryAddSingleton(); - services.TryAddSingleton(); - services.TryAddSingleton(); - - services.TryAddSingleton(typeof(ITopicServer), typeof(ConsumerHandler)); - - return new ConsistencyBuilder(services); - } - - - public static ConsistencyBuilder AddMessageStore(this ConsistencyBuilder build) - where T : class, IConsistencyMessageStore { - build.Services.AddScoped(); - build.Services.TryAddScoped(); - return build; } } } \ No newline at end of file