Skip to content

Commit

Permalink
being able to register handler type without specifying events
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacques Kang committed Nov 21, 2018
1 parent e410c20 commit d816465
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/JKang.EventBus.Core/DependencyInjection/EventBusBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using JKang.EventBus.MultiChannel;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;

namespace JKang.EventBus.DependencyInjection
{
Expand Down Expand Up @@ -38,5 +41,22 @@ public IEventBusBuilder AddEventHandler<TEvent, TEventHandler>()
Services.AddScoped<IEventHandler<TEvent>, TEventHandler>();
return this;
}

public IEventBusBuilder AddEventHandler<TEventHandler>()
where TEventHandler : class
{
Type implementationType = typeof(TEventHandler);
IEnumerable<Type> serviceTypes = implementationType
.GetInterfaces()
.Where(i => i.IsGenericType)
.Where(i => i.GetGenericTypeDefinition() == typeof(IEventHandler<>));

foreach (Type serviceType in serviceTypes)
{
Services.AddScoped(serviceType, implementationType);
}

return this;
}
}
}
2 changes: 1 addition & 1 deletion src/Samples.EventBus.AspNetCore/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void ConfigureServices(IServiceCollection services)
builder
.AddInMemoryEventBus()
.AddAmazonSnsEventPublisher(x => x.Region = "eu-west-3")
.AddEventHandler<MessageSent, MessageSentEventHandler>()
.AddEventHandler<MessageSentEventHandler>()
;
});
}
Expand Down

0 comments on commit d816465

Please sign in to comment.