Commit da57138b authored by yangxiaodong's avatar yangxiaodong

add jobs services to DI.

parent d52ce288
namespace Microsoft.Extensions.DependencyInjection using System;
using Cap.Consistency.Job;
using Cap.Consistency.Store;
namespace Microsoft.Extensions.DependencyInjection
{ {
/// <summary> /// <summary>
/// Used to verify Consistency service was called on a ServiceCollection /// Used to verify Consistency service was called on a ServiceCollection
...@@ -12,5 +16,34 @@ ...@@ -12,5 +16,34 @@
} }
public IServiceCollection Services { get; private set; } public IServiceCollection Services { get; private set; }
private ConsistencyBuilder AddScoped(Type serviceType, Type concreteType) {
Services.AddScoped(serviceType, concreteType);
return this;
}
private ConsistencyBuilder AddSingleton<TService, TImplementation>()
where TService : class
where TImplementation : class, TService {
Services.AddSingleton<TService, TImplementation>();
return this;
}
/// <summary>
/// Adds an <see cref="IConsistencyMessageStore"/> .
/// </summary>
/// <typeparam name="T">The type for the <see cref="IConsistencyMessageStore"/> to add. </typeparam>
/// <returns>The current <see cref="ConsistencyBuilder"/> instance.</returns>
public virtual ConsistencyBuilder AddMessageStore<T>()
where T : class, IConsistencyMessageStore {
return AddScoped(typeof(IConsistencyMessageStore), typeof(T));
}
public virtual ConsistencyBuilder AddJobs<T>()
where T : class, IJob {
return AddSingleton<IJob, T>();
}
} }
} }
\ No newline at end of file
...@@ -6,10 +6,10 @@ using Cap.Consistency.Abstractions.ModelBinding; ...@@ -6,10 +6,10 @@ using Cap.Consistency.Abstractions.ModelBinding;
using Cap.Consistency.Consumer; using Cap.Consistency.Consumer;
using Cap.Consistency.Infrastructure; using Cap.Consistency.Infrastructure;
using Cap.Consistency.Internal; using Cap.Consistency.Internal;
using Cap.Consistency.Job;
using Cap.Consistency.Store; using Cap.Consistency.Store;
using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection.Extensions;
// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.DependencyInjection namespace Microsoft.Extensions.DependencyInjection
{ {
/// <summary> /// <summary>
...@@ -34,18 +34,43 @@ namespace Microsoft.Extensions.DependencyInjection ...@@ -34,18 +34,43 @@ namespace Microsoft.Extensions.DependencyInjection
/// <param name="services">The services available in the application.</param> /// <param name="services">The services available in the application.</param>
/// <param name="setupAction">An action to configure the <see cref="ConsistencyOptions"/>.</param> /// <param name="setupAction">An action to configure the <see cref="ConsistencyOptions"/>.</param>
/// <returns>An <see cref="ConsistencyBuilder"/> for application services.</returns> /// <returns>An <see cref="ConsistencyBuilder"/> for application services.</returns>
public static ConsistencyBuilder AddConsistency(this IServiceCollection services, Action<ConsistencyOptions> setupAction) { public static ConsistencyBuilder AddConsistency(
services.TryAddSingleton<ConsistencyMarkerService>(); this IServiceCollection services,
Action<ConsistencyOptions> setupAction) {
services.TryAddSingleton<ConsistencyMarkerService>();
services.Configure(setupAction); services.Configure(setupAction);
var IConsumerListenerServices = new Dictionary<Type, Type>(); AddConsumerServices(services);
services.TryAddSingleton<IConsumerExcutorSelector, ConsumerExcutorSelector>();
services.TryAddSingleton<IModelBinder, DefaultModelBinder>();
services.TryAddSingleton<IConsumerInvokerFactory, ConsumerInvokerFactory>();
services.TryAddSingleton<MethodMatcherCache>();
services.TryAddScoped<ConsistencyMessageManager>();
services.AddSingleton<IProcessingServer, ConsumerHandler>();
services.AddSingleton<IProcessingServer, JobProcessingServer>();
services.AddSingleton<IBootstrapper, DefaultBootstrapper>();
services.TryAddTransient<IJobProcessor, CronJobProcessor>();
services.TryAddSingleton<IJob, CapJob>();
services.TryAddTransient<DefaultCronJobRegistry>();
return new ConsistencyBuilder(services);
}
private static void AddConsumerServices(IServiceCollection services) {
var consumerListenerServices = new Dictionary<Type, Type>();
foreach (var rejectedServices in services) { foreach (var rejectedServices in services) {
if (rejectedServices.ImplementationType != null && typeof(IConsumerService).IsAssignableFrom(rejectedServices.ImplementationType)) if (rejectedServices.ImplementationType != null
IConsumerListenerServices.Add(typeof(IConsumerService), rejectedServices.ImplementationType); && 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); services.AddSingleton(service.Key, service.Value);
} }
...@@ -55,23 +80,6 @@ namespace Microsoft.Extensions.DependencyInjection ...@@ -55,23 +80,6 @@ namespace Microsoft.Extensions.DependencyInjection
services.AddSingleton(typeof(IConsumerService), type); services.AddSingleton(typeof(IConsumerService), type);
} }
} }
services.TryAddSingleton<IConsumerExcutorSelector, ConsumerExcutorSelector>();
services.TryAddSingleton<IModelBinder, DefaultModelBinder>();
services.TryAddSingleton<IConsumerInvokerFactory, ConsumerInvokerFactory>();
services.TryAddSingleton<MethodMatcherCache>();
services.TryAddSingleton(typeof(ITopicServer), typeof(ConsumerHandler));
return new ConsistencyBuilder(services);
}
public static ConsistencyBuilder AddMessageStore<T>(this ConsistencyBuilder build)
where T : class, IConsistencyMessageStore {
build.Services.AddScoped<IConsistencyMessageStore, T>();
build.Services.TryAddScoped<ConsistencyMessageManager>();
return build;
} }
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment