MediatR.Extensions.Microsoft.DependencyInjection icon indicating copy to clipboard operation
MediatR.Extensions.Microsoft.DependencyInjection copied to clipboard

Design Question: Required assemblies to scan

Open winromulus opened this issue 3 years ago • 0 comments

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;
        }

winromulus avatar Oct 10 '21 11:10 winromulus