Skip to content

Commit

Permalink
feat: add action when no handler was found
Browse files Browse the repository at this point in the history
  • Loading branch information
Douglas Lima authored and dougolima committed Oct 22, 2021
1 parent f5a15d5 commit 3241f79
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
4 changes: 4 additions & 0 deletions src/KafkaFlow.TypedHandler/TypedHandlerConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
namespace KafkaFlow.TypedHandler
{
using System;

internal class TypedHandlerConfiguration
{
public HandlerTypeMapping HandlerMapping { get; } = new();

public Action<IMessageContext> OnNoHandlerFound { get; set; } = (_) => { };
}
}
20 changes: 18 additions & 2 deletions src/KafkaFlow.TypedHandler/TypedHandlerConfigurationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class TypedHandlerConfigurationBuilder
private readonly IDependencyConfigurator dependencyConfigurator;
private readonly List<Type> handlers = new();

private Action<IMessageContext> onNoHandlerFound = (_) => { };
private InstanceLifetime serviceLifetime = InstanceLifetime.Singleton;

/// <summary>
Expand All @@ -29,7 +30,8 @@ public TypedHandlerConfigurationBuilder(IDependencyConfigurator dependencyConfig
/// </summary>
/// <typeparam name="T">A type that implements the <see cref="IMessageHandler{TMessage}"/> interface</typeparam>
/// <returns></returns>
public TypedHandlerConfigurationBuilder AddHandlersFromAssemblyOf<T>() => this.AddHandlersFromAssemblyOf(typeof(T));
public TypedHandlerConfigurationBuilder AddHandlersFromAssemblyOf<T>() =>
this.AddHandlersFromAssemblyOf(typeof(T));

/// <summary>
/// Adds all classes that implements the <see cref="IMessageHandler{TMessage}"/> interface from the assemblies of the provided types
Expand Down Expand Up @@ -70,6 +72,17 @@ public TypedHandlerConfigurationBuilder AddHandler<T>()
return this;
}

/// <summary>
/// Register the action to be executed when no handler was found to process the message
/// </summary>
/// <param name="handler">The handler that will be executed</param>
/// <returns></returns>
public TypedHandlerConfigurationBuilder WhenNoHandlerFound(Action<IMessageContext> handler)
{
this.onNoHandlerFound = handler;
return this;
}

/// <summary>
/// Set the handler lifetime. The default value is <see cref="InstanceLifetime.Singleton"/>
/// </summary>
Expand All @@ -83,7 +96,10 @@ public TypedHandlerConfigurationBuilder WithHandlerLifetime(InstanceLifetime lif

internal TypedHandlerConfiguration Build()
{
var configuration = new TypedHandlerConfiguration();
var configuration = new TypedHandlerConfiguration
{
OnNoHandlerFound = this.onNoHandlerFound,
};

foreach (var handlerType in this.handlers)
{
Expand Down
35 changes: 22 additions & 13 deletions src/KafkaFlow.TypedHandler/TypedHandlerMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,28 @@ public TypedHandlerMiddleware(

public async Task Invoke(IMessageContext context, MiddlewareDelegate next)
{
await Task.WhenAll(
this.configuration
.HandlerMapping
.GetHandlersTypes(context.Message.Value.GetType())
.Select(
handler =>
HandlerExecutor
.GetExecutor(context.Message.Value.GetType())
.Execute(
this.dependencyResolver.Resolve(handler),
context,
context.Message.Value)))
.ConfigureAwait(false);
var handlers = this.configuration
.HandlerMapping
.GetHandlersTypes(context.Message.Value.GetType());

if (!handlers.Any())
{
this.configuration.OnNoHandlerFound(context);
}
else
{
await Task.WhenAll(
handlers
.Select(
handler =>
HandlerExecutor
.GetExecutor(context.Message.Value.GetType())
.Execute(
this.dependencyResolver.Resolve(handler),
context,
context.Message.Value)))
.ConfigureAwait(false);
}

await next(context).ConfigureAwait(false);
}
Expand Down

0 comments on commit 3241f79

Please sign in to comment.