Example #1
0
        /// <summary>
        /// Helper method to register a validator from an AssemblyScanner result
        /// </summary>
        /// <param name="services">The collection of services</param>
        /// <param name="scanResult">The scan result</param>
        /// <param name="lifetime">The lifetime of the validators. The default is transient</param>
        /// <returns></returns>
        private static IServiceCollection AddScanResult(this IServiceCollection services, AssemblyScanner.AssemblyScanResult scanResult, ServiceLifetime lifetime)
        {
            //Register as interface
            services.Add(
                new ServiceDescriptor(
                    serviceType: scanResult.InterfaceType,
                    implementationType: scanResult.ValidatorType,
                    lifetime: lifetime));

            //Register as self
            services.Add(
                new ServiceDescriptor(
                    serviceType: scanResult.ValidatorType,
                    implementationType: scanResult.ValidatorType,
                    lifetime: lifetime));

            return(services);
        }
Example #2
0
        /// <summary>
        /// Helper method to register a validator from an AssemblyScanner result
        /// </summary>
        /// <param name="services">The collection of services</param>
        /// <param name="scanResult">The scan result</param>
        /// <param name="lifetime">The lifetime of the validators. The default is scoped (per-request in web applications)</param>
        /// <param name="filter">Optional filter that allows certain types to be skipped from registration.</param>
        /// <returns></returns>
        private static IServiceCollection AddScanResult(this IServiceCollection services, AssemblyScanner.AssemblyScanResult scanResult, ServiceLifetime lifetime, Func <AssemblyScanner.AssemblyScanResult, bool> filter)
        {
            bool shouldRegister = filter?.Invoke(scanResult) ?? true;

            if (shouldRegister)
            {
                //Register as interface
                services.Add(
                    new ServiceDescriptor(
                        serviceType: scanResult.InterfaceType,
                        implementationType: scanResult.ValidatorType,
                        lifetime: lifetime));

                //Register as self
                services.Add(
                    new ServiceDescriptor(
                        serviceType: scanResult.ValidatorType,
                        implementationType: scanResult.ValidatorType,
                        lifetime: lifetime));
            }

            return(services);
        }