jbogard / MediatR.Extensions.Microsoft.DependencyInjection

MediatR extensions for Microsoft.Extensions.DependencyInjection
MIT License
326 stars 90 forks source link

Design Question: Required assemblies to scan #104

Open winromulus opened 3 years ago

winromulus commented 3 years ago

Hi,

We have an issue with the mandatory requirement of passing in at least one assembly to scan for handlers. Our need is to be able to register handlers in a custom manner but due to the assembly scanning either they get registered twice or, if we skip the current assembly, we have to provide an assembly that does not have any handlers in it. My question is, why are assemblies required? This would work perfectly with an empty list of assemblies to scan.

/// <summary>
        /// Registers handlers and mediator types from the specified assemblies
        /// </summary>
        /// <param name="services">Service collection</param>
        /// <param name="assemblies">Assemblies to scan</param>
        /// <param name="configuration">The action used to configure the options</param>
        /// <returns>Service collection</returns>
        public static IServiceCollection AddMediatR(this IServiceCollection services, IEnumerable<Assembly> assemblies, Action<MediatRServiceConfiguration> configuration)
        {
            if (!assemblies.Any())
            {
                throw new ArgumentException("No assemblies found to scan. Supply at least one assembly to scan for handlers.");
            }
            var serviceConfig = new MediatRServiceConfiguration();

            configuration?.Invoke(serviceConfig);

            ServiceRegistrar.AddRequiredServices(services, serviceConfig);

            ServiceRegistrar.AddMediatRClasses(services, assemblies);

            return services;
        }