/// <summary>
        /// Uses MediatR.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="serviceConfig">The MediatR service configuration.</param>
        /// <returns>IServiceConventionContext.</returns>
        public static IServiceConventionContext UseMediatR(
            this IServiceConventionContext builder,
            MediatRServiceConfiguration serviceConfig
            )
        {
            if (builder is null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (serviceConfig is null)
            {
                throw new ArgumentNullException(nameof(serviceConfig));
            }

            if (builder.Services.Any(z => z.ServiceType == typeof(IMediator)))
            {
                return(builder);
            }

            builder.Set(serviceConfig);
            var assemblies = builder.AssemblyCandidateFinder
                             .GetCandidateAssemblies(nameof(MediatR)).ToArray();

            ServiceRegistrar.AddRequiredServices(builder.Services, serviceConfig);
            ServiceRegistrar.AddMediatRClasses(builder.Services, assemblies);
            return(builder);
        }
示例#2
0
        public static IServiceCollection AddDomainService(this IServiceCollection services)
        {
            var assembly = typeof(DomainServiceCollectionExtensions).Assembly;

            ServiceRegistrar.AddMediatRClasses(services, new[] { assembly });
            services.AddTransient(typeof(IPipelineBehavior <,>), typeof(CommandValidationPreProcessor <,>));
            foreach (var validatorImplType in assembly.DefinedTypes.Where(t => !t.IsAbstract && !t.IsInterface))
            {
                foreach (var validatorServiceType in validatorImplType.FindInterfacesThatClose(typeof(IValidator <>)))
                {
                    services.AddTransient(validatorServiceType, validatorImplType);
                }
            }

            return(services);
        }