Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions src/DispatchR/Configuration/ServiceRegistrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,28 +170,29 @@ public static void RegisterHandlers(IServiceCollection services, List<Type> allT
}
}

public static void RegisterNotification(IServiceCollection services, List<Type> allTypes,
Type syncNotificationHandlerType)
{
var allNotifications = allTypes
.SelectMany(handlerType => handlerType.GetInterfaces()
.Where(i => i.IsGenericType && syncNotificationHandlerType == i.GetGenericTypeDefinition())
.Select(i => new { HandlerType = handlerType, Interface = i }))
.ToList();

foreach (var notification in allNotifications)
{
var serviceType = notification.Interface;
var implementationType = notification.HandlerType;

public static void RegisterNotification(IServiceCollection services, List<Type> allTypes,
Type syncNotificationHandlerType)
{
var allNotifications = allTypes
.Where(handlerType => handlerType.IsClass && !handlerType.IsAbstract)
.SelectMany(handlerType => handlerType.GetInterfaces()
.Where(i => i.IsGenericType && syncNotificationHandlerType == i.GetGenericTypeDefinition())
.Select(i => new { HandlerType = handlerType, Interface = i }))
.ToList();

foreach (var notification in allNotifications)
{
var serviceType = notification.Interface;
var implementationType = notification.HandlerType;

if (serviceType.ContainsGenericParameters)
{
serviceType = serviceType.GetGenericTypeDefinition();
}

services.AddScoped(serviceType, implementationType);
}
}
}
services.AddScoped(serviceType, implementationType);
}
}

private static bool IsAwaitable(Type type)
{
Expand All @@ -208,4 +209,4 @@ private static bool IsAwaitable(Type type)
return false;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using DispatchR.Abstractions.Notification;

namespace DispatchR.TestCommon.Fixtures.Notification;

public abstract class AbstractNotificationHandler : INotificationHandler<OpenGenericTargetNotification>
{
public abstract ValueTask Handle(OpenGenericTargetNotification request, CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace DispatchR.TestCommon.Fixtures.Notification;

public sealed record ConcreteWorkflowEvent : IWorkflowEvent;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using DispatchR.Abstractions.Notification;

namespace DispatchR.TestCommon.Fixtures.Notification;

public sealed class ConcreteWorkflowNotificationHandler : INotificationHandler<WorkflowNotification<ConcreteWorkflowEvent>>
{
public ValueTask Handle(WorkflowNotification<ConcreteWorkflowEvent> request, CancellationToken cancellationToken)
{
return ValueTask.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace DispatchR.TestCommon.Fixtures.Notification;

public interface IWorkflowEvent;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using DispatchR.Abstractions.Notification;

namespace DispatchR.TestCommon.Fixtures.Notification;

public interface IWorkflowNotificationHandler<TNotification> : INotificationHandler<WorkflowNotification<TNotification>>
where TNotification : IWorkflowEvent;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using DispatchR.Abstractions.Notification;

namespace DispatchR.TestCommon.Fixtures.Notification;

public sealed record WorkflowNotification<TNotification>(TNotification Event) : INotification
where TNotification : IWorkflowEvent;
28 changes: 28 additions & 0 deletions tests/DispatchR.UnitTest/AddDispatchRConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,32 @@ p.ImplementationType is not null &&

Assert.NotNull(openGenericHandler);
}

[Fact]
public void AddDispatchR_RegisterNotifications_SkipsInterfaceAndAbstractHandlers()
{
// Arrange
var services = new ServiceCollection();

// Act
services.AddDispatchR(cfg =>
{
cfg.Assemblies.Add(typeof(Fixture).Assembly);
cfg.RegisterPipelines = false;
cfg.RegisterNotifications = true;
});

// Assert
Assert.DoesNotContain(services, p =>
p.IsKeyedService is false &&
p.ImplementationType == typeof(AbstractNotificationHandler));

Assert.DoesNotContain(services, p =>
p.IsKeyedService is false &&
p.ImplementationType == typeof(IWorkflowNotificationHandler<>));

Assert.Contains(services, p =>
p.IsKeyedService is false &&
p.ImplementationType == typeof(ConcreteWorkflowNotificationHandler));
}
}
Loading