Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 22 additions & 10 deletions src/DispatchR/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,32 @@ public static IServiceCollection AddDispatchR(this IServiceCollection services,
var otherHandlerTypes = new HashSet<Type>()
{
pipelineBehaviorType,
streamRequestHandlerType,
streamPipelineBehaviorType,
syncNotificationHandlerType
};

var allTypes = configurationOptions.Assemblies.SelectMany(x => x.GetTypes()).Distinct()
.Where(p =>
p.GetInterfaces() is { Length: >= 1 } interfaces &&
interfaces
.Where(i => i.IsGenericType)
.Select(i => i.GetGenericTypeDefinition())
.Any(i => i == requestHandlerType
? configurationOptions.IsHandlerIncluded(p)
: otherHandlerTypes.Contains(i)))
.Where(type =>
{
var genericInterfaces = type.GetInterfaces()
.Where(i => i.IsGenericType)
.Select(i => i.GetGenericTypeDefinition())
.ToList();

var isRequestHandler = genericInterfaces.Contains(requestHandlerType);
var isPipelineBehavior = genericInterfaces.Contains(pipelineBehaviorType);
var isStreamRequestHandler = genericInterfaces.Contains(streamRequestHandlerType);
var isStreamPipelineBehavior = genericInterfaces.Contains(streamPipelineBehaviorType);

var isConcreteRequestHandler = isRequestHandler && !isPipelineBehavior;
var isConcreteStreamRequestHandler = isStreamRequestHandler && !isStreamPipelineBehavior;
if (isConcreteRequestHandler || isConcreteStreamRequestHandler)
{
return configurationOptions.IsHandlerIncluded(type);
}

return genericInterfaces.Intersect(otherHandlerTypes).Any();
})
.ToList();

if (configurationOptions.RegisterNotifications)
Expand All @@ -79,4 +91,4 @@ public static IServiceCollection AddDispatchR(this IServiceCollection services,

return services;
}
}
}
63 changes: 63 additions & 0 deletions tests/DispatchR.UnitTest/AddDispatchRConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,48 @@ public void AddDispatchR_ReturnsExpectedResponse_IncludeSingleHandler()
Assert.Equal(1, countOfAllSimpleHandlers);
}

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

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

// Assert
Assert.DoesNotContain(services, p =>
p.IsKeyedService &&
p.KeyedImplementationType == Fixture.AnyStreamHandler.GetType());
}

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

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

// Assert
Assert.Contains(services, p =>
p.IsKeyedService &&
p.KeyedImplementationType == Fixture.AnyStreamHandler.GetType());
}

[Fact]
public void AddDispatchR_ReturnsExpectedResponse_ExcludeSingleHandler()
{
Expand All @@ -99,6 +141,27 @@ public void AddDispatchR_ReturnsExpectedResponse_ExcludeSingleHandler()
Assert.Equal(0, countOfAllSimpleHandlers);
}

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

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

// Assert
Assert.DoesNotContain(services, p =>
p.IsKeyedService &&
p.KeyedImplementationType == Fixture.AnyStreamHandler.GetType());
}

[Fact]
public void AddDispatchR_ReturnsExpectedResponse_IncludeAndExcludeOneHandlers()
{
Expand Down
Loading