Skip to content
Open
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
8 changes: 5 additions & 3 deletions src/DispatchR/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ public static IServiceCollection AddDispatchR(this IServiceCollection services,
var streamPipelineBehaviorType = typeof(IStreamPipelineBehavior<,>);
var syncNotificationHandlerType = typeof(INotificationHandler<>);

var allTypes = configurationOptions.Assemblies.SelectMany(x => x.GetTypes()).Distinct()
.Where(p =>
var allTypes = configurationOptions.Assemblies.SelectMany(x => x.GetTypes())
.Distinct()
.Where(type => type is { IsClass: true, IsAbstract: false })
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, but I feel like we’ve drifted away from the original goal. We wanted to avoid picking up interfaces, but now we’re explicitly saying it has to be a class and not abstract.

I think if we just exclude interfaces and allow everything else, we could also support other types like structs.

What do you think?

.Where(type =>
{
var interfaces = p.GetInterfaces();
var interfaces = type.GetInterfaces();
return interfaces.Length >= 1 &&
interfaces
.Where(i => i.IsGenericType)
Expand Down
4 changes: 1 addition & 3 deletions src/DispatchR/Requests/IMediator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ public IAsyncEnumerable<TResponse> CreateStream<TRequest, TResponse>(IStreamRequ

public async ValueTask Publish<TNotification>(TNotification request, CancellationToken cancellationToken) where TNotification : INotification
{
var notificationsInDi = serviceProvider.GetRequiredService<IEnumerable<INotificationHandler<TNotification>>>();

var notifications = Unsafe.As<INotificationHandler<TNotification>[]>(notificationsInDi);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bro, this piece of code is necessary because when you use foreach on an IEnumerable, it causes boxing, which leads to memory allocation on the heap. With this approach, we avoid using foreach directly on IEnumerable.

More information: https://youtu.be/7w4ho3WzKss?list=PLGiSgN3ODieILgFQN1puu-ey9guWwnxGX&t=3280

var notifications = serviceProvider.GetRequiredService<IEnumerable<INotificationHandler<TNotification>>>();
foreach (var notification in notifications)
{
var valueTask = notification.Handle(request, cancellationToken);
Expand Down