// Copyright (c) HelloShop Corporation. All rights reserved. // See the license file in the project root for more information. using Microsoft.Extensions.DependencyInjection; using System.Reflection; using System.Text.Json; namespace HelloShop.EventBus.Abstractions { public static class EventBusBuilderExtensions { public static IEventBusBuilder ConfigureJsonOptions(this IEventBusBuilder eventBusBuilder, Action configure) { eventBusBuilder.Services.Configure(o => { configure(o.JsonSerializerOptions); }); return eventBusBuilder; } public static IEventBusBuilder AddSubscription(this IEventBusBuilder eventBusBuilder) where TEvent : DistributedEvent where TEventHandler : class, IDistributedEventHandler { eventBusBuilder.Services.AddKeyedTransient(typeof(TEvent)); eventBusBuilder.Services.Configure(o => { o.EventTypes[typeof(TEvent).Name] = typeof(TEvent); }); return eventBusBuilder; } public static IEventBusBuilder AddSubscription(this IEventBusBuilder eventBusBuilder, Type eventType, Type eventHandlerType) { eventBusBuilder.Services.AddKeyedTransient(typeof(IDistributedEventHandler), eventType, eventHandlerType); eventBusBuilder.Services.Configure(o => { o.EventTypes[eventType.Name] = eventType; }); return eventBusBuilder; } public static IEventBusBuilder AddSubscriptionFromAssembly(this IEventBusBuilder eventBusBuilder, Assembly? assembly = null) { assembly ??= Assembly.GetCallingAssembly(); var handlers = assembly.GetTypes().Where(t => t.IsAssignableTo(typeof(IDistributedEventHandler))).ToList(); handlers.ForEach(handler => { var eventType = handler.GetInterfaces().Single(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDistributedEventHandler<>)).GetGenericArguments().Single(); eventBusBuilder.AddSubscription(eventType, handler); }); return eventBusBuilder; } } }