/// <summary>
        /// Register all <see cref="Intermedium"/> services.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
        /// <param name="setupAction">
        /// An <see cref="Action{T}"/> to configure the provided <see cref="IntermediumOptions"/>.
        /// </param>
        /// <returns>A <paramref name="services"/> parameter after the operation has completed.</returns>
        public static IServiceCollection AddIntermedium(
            this IServiceCollection services,
            Action <IntermediumOptions> setupAction)
        {
            if (services is null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            var options = new IntermediumOptions();

            setupAction?.Invoke(options);

            if (!options.ScanTargets.Any())
            {
                throw new InvalidOperationException("Please, specify at least one assembly to scan.");
            }

            if (services.Any(x => x.ServiceType == typeof(IMediator)))
            {
                throw new InvalidOperationException(
                          nameof(IMediator) + " is already registered. "
                          + "Define your mediator implementation using setup action of this method."
                          );
            }

            ServiceRegistrar.FindAndRegister(services, options);

            return(services);
        }
Exemple #2
0
        public void FindAndRegister_ExecutingAssembly_RegistersAllComponents()
        {
            var services = new ServiceCollection();
            var options  = new IntermediumOptions();

            options.Scan(typeof(ServiceRegistrarTests));
            ServiceRegistrar.FindAndRegister(services, options);

            var provider = services.BuildServiceProvider();

            provider.GetService <Core.ServiceProvider>().Should().NotBeNull();
            provider.GetService <IMediator>().Should().BeOfType <Mediator>();

            new[]
            {
                typeof(ExceptionHandlingMiddleware <,>),
                typeof(PostProcessingMiddleware <,>),
                typeof(PreProcessingMiddleware <,>)
            }
            .All(middleware => services.Any(x => x.ImplementationType == middleware))
            .Should()
            .BeTrue();

            provider.GetServices <IQueryHandler <QueryTest, int> >().Should().HaveCount(1);
            provider.GetServices <IQueryHandler <CommandTest, VoidUnit> >().Should().HaveCount(1);
            provider.GetServices <INotificationHandler <NotificationTest> >().Should().HaveCount(2);
            provider.GetServices <IQueryExceptionHandler <CommandTest, VoidUnit> >().Should().HaveCount(3);
        }