Browse Source

add jobs services to DI.

master
yangxiaodong 7 years ago
parent
commit
da57138ba5
2 changed files with 66 additions and 25 deletions
  1. +34
    -1
      src/Cap.Consistency/Microsoft.Extensions.DependencyInjection/ConsistencyBuilder.cs
  2. +32
    -24
      src/Cap.Consistency/Microsoft.Extensions.DependencyInjection/ServiceCollectionExtensions.cs

+ 34
- 1
src/Cap.Consistency/Microsoft.Extensions.DependencyInjection/ConsistencyBuilder.cs View File

@@ -1,4 +1,8 @@
namespace Microsoft.Extensions.DependencyInjection
using System;
using Cap.Consistency.Job;
using Cap.Consistency.Store;

namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// 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<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>();
}
}
}

+ 32
- 24
src/Cap.Consistency/Microsoft.Extensions.DependencyInjection/ServiceCollectionExtensions.cs View File

@@ -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
{
/// <summary>
@@ -34,18 +34,43 @@ namespace Microsoft.Extensions.DependencyInjection
/// <param name="services">The services available in the application.</param>
/// <param name="setupAction">An action to configure the <see cref="ConsistencyOptions"/>.</param>
/// <returns>An <see cref="ConsistencyBuilder"/> for application services.</returns>
public static ConsistencyBuilder AddConsistency(this IServiceCollection services, Action<ConsistencyOptions> setupAction) {
services.TryAddSingleton<ConsistencyMarkerService>();
public static ConsistencyBuilder AddConsistency(
this IServiceCollection services,
Action<ConsistencyOptions> setupAction) {

services.TryAddSingleton<ConsistencyMarkerService>();
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) {
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<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;
}
}
}

Loading…
Cancel
Save