Skip to content

Commit

Permalink
Removed DI interface w/ issues on autoregistration.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmwebb-lv committed May 10, 2024
1 parent da8a29c commit 899de2a
Showing 1 changed file with 0 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,50 +25,5 @@ public static void AddSubscriber<TEvent, TEventHandler>(this IInMemoryEventBusBu
{
builder.Services.TryAddScoped(getSubscriber);
}

public static void AddSubscribers(this IInMemoryEventBusBuilder builder, params Type[] queryHandlerTypes)
{
AddSubscribers(builder, (IEnumerable<Type>)queryHandlerTypes);
}

public static void AddSubscribers(this IInMemoryEventBusBuilder builder, Assembly fromAssembly,
Predicate<Type> predicate = null)
{
predicate = predicate ?? (t => true);
var subscribeSynchronousToTypes = fromAssembly
.GetTypes()
.Where(t => t.GetTypeInfo().GetInterfaces().Any(IsSubscriberInterface))
.Where(t => !t.HasConstructorParameterOfType(IsSubscriberInterface))
.Where(t => predicate(t));
AddSubscribers(builder, subscribeSynchronousToTypes);
}

public static void AddSubscribers(this IInMemoryEventBusBuilder builder, IEnumerable<Type> queryHandlerTypes)
{
foreach (var queryHandlerType in queryHandlerTypes)
{
var t = queryHandlerType;
if (t.GetTypeInfo().IsAbstract) continue;
var queryHandlerInterfaces = t
.GetTypeInfo()
.GetInterfaces()
.Where(IsSubscriberInterface)
.ToList();
if (!queryHandlerInterfaces.Any())
{
throw new ArgumentException($"Type '{t.PrettyPrint()}' is not an '{typeof(ISubscriber<>).PrettyPrint()}'");
}

foreach (var queryHandlerInterface in queryHandlerInterfaces)
{
builder.Services.AddTransient(queryHandlerInterface, t);
}
}
}

private static bool IsSubscriberInterface(this Type type)
{
return type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == typeof(ISubscriber<>);
}
}
}

0 comments on commit 899de2a

Please sign in to comment.