-
-
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.
Allow customization of SNS and SQS serializer (#23)
Co-authored-by: Thomas Zwarts <[email protected]>
- Loading branch information
Showing
21 changed files
with
621 additions
and
139 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
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
14 changes: 14 additions & 0 deletions
14
src/Kralizek.Lambda.Template.Sns/INotificationSerializer.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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.Text.Json; | ||
|
||
namespace Kralizek.Lambda | ||
{ | ||
public interface INotificationSerializer | ||
{ | ||
public TMessage Deserialize<TMessage>(string input); | ||
} | ||
|
||
public class DefaultJsonNotificationSerializer : INotificationSerializer | ||
{ | ||
public TMessage Deserialize<TMessage>(string input) => JsonSerializer.Deserialize<TMessage>(input); | ||
} | ||
} |
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: 66 additions & 8 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,36 +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); | ||
|
||
if (enableParallelExecution) | ||
if (maxDegreeOfParallelism <= 1) throw new ArgumentOutOfRangeException(nameof(maxDegreeOfParallelism), $"{nameof(maxDegreeOfParallelism)} must be greater than 1"); | ||
|
||
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
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.Text.Json; | ||
|
||
namespace Kralizek.Lambda | ||
{ | ||
public interface IMessageSerializer | ||
{ | ||
public TMessage Deserialize<TMessage>(string input); | ||
} | ||
|
||
public class DefaultJsonMessageSerializer : IMessageSerializer | ||
{ | ||
public TMessage Deserialize<TMessage>(string input) => JsonSerializer.Deserialize<TMessage>(input); | ||
} | ||
} |
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
73 changes: 66 additions & 7 deletions
73
src/Kralizek.Lambda.Template.Sqs/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,35 +1,94 @@ | ||
using System; | ||
using Amazon.Lambda.SQSEvents; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
|
||
namespace Kralizek.Lambda | ||
{ | ||
public static class ServiceCollectionExtensions | ||
{ | ||
[Obsolete("Use `services.UseQueueMessageHandler<TMessage,THandler>().UseParallelExecution(maxDegreeOfParallelism);` instead.")] | ||
public static IServiceCollection ConfigureSnsParallelExecution(this IServiceCollection services, int maxDegreeOfParallelism) | ||
{ | ||
services.Configure<ParallelSqsExecutionOptions>(option => option.MaxDegreeOfParallelism = maxDegreeOfParallelism); | ||
|
||
return services; | ||
} | ||
|
||
public static IMessageHandlerConfigurator<TMessage> WithParallelExecution<TMessage>(this IMessageHandlerConfigurator<TMessage> configurator, int? maxDegreeOfParallelism = null) | ||
where TMessage : class | ||
{ | ||
ArgumentNullException.ThrowIfNull(configurator); | ||
|
||
if (maxDegreeOfParallelism <= 1) throw new ArgumentOutOfRangeException(nameof(maxDegreeOfParallelism), $"{nameof(maxDegreeOfParallelism)} must be greater than 1"); | ||
|
||
configurator.Services.AddTransient<IEventHandler<SQSEvent>, ParallelSqsEventHandler<TMessage>>(); | ||
|
||
if (maxDegreeOfParallelism.HasValue) | ||
{ | ||
configurator.Services.Configure<ParallelSqsExecutionOptions>(options => options.MaxDegreeOfParallelism = maxDegreeOfParallelism.Value); | ||
} | ||
|
||
return configurator; | ||
} | ||
|
||
public static IServiceCollection UseCustomMessageSerializer<TSerializer>(this IServiceCollection services, ServiceLifetime lifetime = ServiceLifetime.Singleton) | ||
where TSerializer : IMessageSerializer | ||
{ | ||
ArgumentNullException.ThrowIfNull(services); | ||
|
||
services.Add(ServiceDescriptor.Describe(typeof(IMessageSerializer), typeof(TSerializer), lifetime)); | ||
|
||
return services; | ||
} | ||
|
||
[Obsolete("Use `services.UseQueueMessageHandler<TMessage, THandler>();` instead.")] | ||
public static IServiceCollection UseSqsHandler<TMessage, THandler>(this IServiceCollection services, bool enableParallelExecution = false) | ||
where TMessage : class | ||
where THandler : class, IMessageHandler<TMessage> | ||
{ | ||
services.AddOptions(); | ||
var configurator = UseQueueMessageHandler<TMessage, THandler>(services); | ||
|
||
if (enableParallelExecution) | ||
{ | ||
services.AddTransient<IEventHandler<SQSEvent>, ParallelSqsEventHandler<TMessage>>(); | ||
} | ||
else | ||
{ | ||
services.AddTransient<IEventHandler<SQSEvent>, SqsEventHandler<TMessage>>(); | ||
configurator.WithParallelExecution(); | ||
} | ||
|
||
return services; | ||
} | ||
|
||
public static IMessageHandlerConfigurator<TMessage> UseQueueMessageHandler<TMessage, THandler>(this IServiceCollection services) | ||
where TMessage : class | ||
where THandler : class, IMessageHandler<TMessage> | ||
{ | ||
services.AddOptions(); | ||
|
||
services.AddTransient<IEventHandler<SQSEvent>, SqsEventHandler<TMessage>>(); | ||
|
||
services.TryAddSingleton<IMessageSerializer, DefaultJsonMessageSerializer>(); | ||
|
||
services.AddTransient<IMessageHandler<TMessage>, THandler>(); | ||
|
||
return services; | ||
var configurator = new MessageHandlerConfigurator<TMessage>(services); | ||
|
||
return configurator; | ||
} | ||
} | ||
|
||
public interface IMessageHandlerConfigurator<TMessage> | ||
where TMessage : class | ||
{ | ||
IServiceCollection Services { get; } | ||
} | ||
|
||
internal sealed class MessageHandlerConfigurator<TMessage> : IMessageHandlerConfigurator<TMessage> | ||
where TMessage : class | ||
{ | ||
public MessageHandlerConfigurator(IServiceCollection services) | ||
{ | ||
Services = services ?? throw new ArgumentNullException(nameof(services)); | ||
} | ||
|
||
public IServiceCollection Services { get; } | ||
} | ||
} |
Oops, something went wrong.