-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redesigned registration of SNS handlers
- Loading branch information
Showing
3 changed files
with
108 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 65 additions & 9 deletions
74
src/Kralizek.Lambda.Template.Sns/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,94 @@ | ||
using System; | ||
using Amazon.Lambda.SNSEvents; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
|
||
namespace Kralizek.Lambda | ||
{ | ||
public static class ServiceCollectionExtensions | ||
{ | ||
[Obsolete("Use `services.UseNotificationHandler<TNotification,THandler>().UseParallelExecution(maxDegreeOfParallelism);` instead.")] | ||
public static IServiceCollection ConfigureSnsParallelExecution(this IServiceCollection services, int maxDegreeOfParallelism) | ||
{ | ||
services.Configure<ParallelSnsExecutionOptions>(option => option.MaxDegreeOfParallelism = maxDegreeOfParallelism); | ||
|
||
return services; | ||
} | ||
|
||
public static IServiceCollection UseNotificationHandler<TNotification, THandler>(this IServiceCollection services, bool enableParallelExecution = false) | ||
public static INotificationHandlerConfigurator<TNotification> WithParallelExecution<TNotification>(this INotificationHandlerConfigurator<TNotification> configurator, int? maxDegreeOfParallelism = null) | ||
where TNotification : class | ||
where THandler : class, INotificationHandler<TNotification> | ||
{ | ||
services.AddOptions(); | ||
ArgumentNullException.ThrowIfNull(configurator); | ||
|
||
services.AddSingleton<INotificationSerializer, DefaultJsonNotificationSerializer>(); | ||
if (maxDegreeOfParallelism <= 1) throw new ArgumentOutOfRangeException(nameof(maxDegreeOfParallelism), $"{nameof(maxDegreeOfParallelism)} must be greater than 1"); | ||
|
||
if (enableParallelExecution) | ||
configurator.Services.AddTransient<IEventHandler<SNSEvent>, ParallelSnsEventHandler<TNotification>>(); | ||
|
||
if (maxDegreeOfParallelism.HasValue) | ||
{ | ||
services.AddTransient<IEventHandler<SNSEvent>, ParallelSnsEventHandler<TNotification>>(); | ||
configurator.Services.Configure<ParallelSnsExecutionOptions>(options => options.MaxDegreeOfParallelism = maxDegreeOfParallelism.Value); | ||
} | ||
else | ||
|
||
return configurator; | ||
} | ||
|
||
public static IServiceCollection UseCustomNotificationSerializer<TSerializer>(this IServiceCollection services, ServiceLifetime lifetime = ServiceLifetime.Singleton) | ||
where TSerializer : INotificationSerializer | ||
{ | ||
ArgumentNullException.ThrowIfNull(services); | ||
|
||
services.Add(ServiceDescriptor.Describe(typeof(INotificationSerializer), typeof(TSerializer), lifetime)); | ||
|
||
return services; | ||
} | ||
|
||
[Obsolete("Use `services.UseNotificationHandler<TNotification,THandler>().UseParallelExecution();` instead.")] | ||
public static IServiceCollection UseNotificationHandler<TNotification, THandler>(this IServiceCollection services, bool enableParallelExecution) | ||
where TNotification : class | ||
where THandler : class, INotificationHandler<TNotification> | ||
{ | ||
var configurator = UseNotificationHandler<TNotification, THandler>(services); | ||
|
||
if (enableParallelExecution) | ||
{ | ||
services.AddTransient<IEventHandler<SNSEvent>, SnsEventHandler<TNotification>>(); | ||
configurator.WithParallelExecution(); | ||
} | ||
|
||
return services; | ||
} | ||
|
||
public static INotificationHandlerConfigurator<TNotification> UseNotificationHandler<TNotification, THandler>(this IServiceCollection services) | ||
where TNotification : class | ||
where THandler : class, INotificationHandler<TNotification> | ||
{ | ||
services.AddOptions(); | ||
|
||
services.AddTransient<IEventHandler<SNSEvent>, SnsEventHandler<TNotification>>(); | ||
|
||
services.TryAddSingleton<INotificationSerializer, DefaultJsonNotificationSerializer>(); | ||
|
||
services.AddTransient<INotificationHandler<TNotification>, THandler>(); | ||
|
||
return services; | ||
var configurator = new NotificationHandlerConfigurator<TNotification>(services); | ||
|
||
return configurator; | ||
} | ||
} | ||
|
||
public interface INotificationHandlerConfigurator<TNotification> | ||
where TNotification : class | ||
{ | ||
IServiceCollection Services { get; } | ||
} | ||
|
||
internal sealed class NotificationHandlerConfigurator<TNotification> : INotificationHandlerConfigurator<TNotification> | ||
where TNotification : class | ||
{ | ||
public NotificationHandlerConfigurator(IServiceCollection services) | ||
{ | ||
Services = services ?? throw new ArgumentNullException(nameof(services)); | ||
} | ||
|
||
public IServiceCollection Services { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters