/// <summary>
        /// Registers all the <see cref="IValidator{T}"/> found in the given assembly.
        /// </summary>
        /// <param name="options">The mediator options</param>
        /// <param name="assembly">The assembly to scan</param>
        /// <param name="lifetime">The validators lifetime</param>
        /// <returns>The options instance after changes</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static MediatorOptions AddValidatorsFromAssembly(this MediatorOptions options,
                                                                Assembly assembly, ServiceLifetime lifetime = ServiceLifetime.Singleton)
        {
#if NETSTANDARD1_1
            var exportedTypes = assembly.ExportedTypes.Select(t => t.GetTypeInfo());
#else
            var exportedTypes = assembly.ExportedTypes;
#endif
            foreach (var t in exportedTypes.Where(t => t.IsClass && !t.IsAbstract))
            {
#if NETSTANDARD1_1
                var implementedInterfaces = t.ImplementedInterfaces.Select(i => i.GetTypeInfo());
#else
                var implementedInterfaces = t.GetInterfaces();
#endif
                foreach (var i in implementedInterfaces.Where(e => e.IsGenericType && e.GetGenericTypeDefinition() == typeof(IValidator <>)))
                {
#if NETSTANDARD1_1
                    var serviceType        = i.AsType();
                    var implementationType = t.AsType();
#else
                    var serviceType        = i;
                    var implementationType = t;
#endif
                    options.Services.Add(new ServiceDescriptor(serviceType, implementationType, lifetime));
                }
            }

            return(options);
        }
        /// <summary>
        /// Registers all the <see cref="IValidator{T}"/> found in the assembly of the given type.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="options"></param>
        /// <param name="lifetime"></param>
        /// <returns></returns>
        public static MediatorOptions AddValidatorsFromAssemblyOf <T>(this MediatorOptions options, ServiceLifetime lifetime = ServiceLifetime.Singleton)
        {
#if NETSTANDARD1_1
            return(options.AddValidatorsFromAssembly(typeof(T).GetTypeInfo().Assembly, lifetime));
#else
            return(options.AddValidatorsFromAssembly(typeof(T).Assembly, lifetime));
#endif
        }
        /// <summary>
        /// Registers all the command, query and event handlers found in the given type assembly.
        /// </summary>
        /// <typeparam name="T">The type to scan the assembly</typeparam>
        /// <param name="options">The mediator options</param>
        /// <param name="lifetime">The handlers lifetime</param>
        /// <returns>The options instance after changes</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static MediatorOptions AddHandlersFromAssemblyOf <T>(this MediatorOptions options,
                                                                    ServiceLifetime lifetime = ServiceLifetime.Transient)
        {
#if NETSTANDARD1_1
            return(options.AddHandlersFromAssembly(typeof(T).GetTypeInfo().Assembly, lifetime));
#else
            return(options.AddHandlersFromAssembly(typeof(T).Assembly, lifetime));
#endif
        }
        /// <summary>
        /// Configures the given factory for mediator <see cref="IPipeline"/> instances.
        /// Registration order will be kept when executing the pipeline.
        /// </summary>
        /// <param name="options">The mediator options</param>
        /// <param name="factory">The pipeline factory</param>
        /// <param name="lifetime">The pipeline lifetime</param>
        /// <returns>The options instance after changes</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static MediatorOptions AddPipeline(
            this MediatorOptions options, Func <IServiceProvider, IPipeline> factory, ServiceLifetime lifetime = ServiceLifetime.Transient)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            options.Services.Add(new ServiceDescriptor(typeof(IPipeline), factory, lifetime));
            return(options);
        }
        /// <summary>
        /// Configures the given type as a mediator <see cref="IPipeline"/>.
        /// Registration order will be kept when executing the pipeline.
        /// </summary>
        /// <typeparam name="T">The pipeline type</typeparam>
        /// <param name="options">The mediator options</param>
        /// <param name="lifetime">The pipeline lifetime</param>
        /// <returns>The options instance after changes</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static MediatorOptions AddPipeline <T>(this MediatorOptions options, ServiceLifetime lifetime = ServiceLifetime.Transient)
            where T : class, IPipeline
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            options.Services.Add(new ServiceDescriptor(typeof(IPipeline), typeof(T), lifetime));
            return(options);
        }
        /// <summary>
        /// Registers a <see cref="ValidationPipeline"/>.
        /// </summary>
        /// <param name="options">The mediator registration options</param>
        /// <param name="config">Configures the pipeline options</param>
        /// <param name="lifetime">The pipeline lifetime</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static MediatorOptions AddPipelineForValidation(
            this MediatorOptions options, Action <ValidationPipelineOptions> config = null, ServiceLifetime lifetime = ServiceLifetime.Transient)
        {
            if (config != null)
            {
                options.Services.Configure(config);
            }

            options.AddPipeline <ValidationPipeline>(lifetime);

            return(options);
        }
Ejemplo n.º 7
0
        public static IServiceCollection AddMediator(this IServiceCollection services, Action <MediatorOptions> options)
        {
            var optionsInstance = new MediatorOptions();

            options(optionsInstance);
            RegisterAssembly(services, optionsInstance);

            var handlerTypes = services
                               .SelectMany(d => {
                var interfaces = d.ServiceType.GetInterfaces().Select(iface => (type: d.ServiceType, iface));
                return(d.ServiceType.IsInterface
                        ? interfaces.Concat(new[] { (type: d.ServiceType, iface: d.ServiceType) })
        /// <summary>
        /// Registers a <see cref="EFCoreTransactionPipeline{TDbContext}"/>.
        /// </summary>
        /// <param name="options">The mediator registration options</param>
        /// <param name="config">Configures the pipeline options</param>
        /// <param name="lifetime">The pipeline lifetime</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static MediatorOptions AddPipelineForEFCoreTransaction <TDbContext>(
            this MediatorOptions options, Action <EFCoreTransactionPipelineOptions> config = null, ServiceLifetime lifetime = ServiceLifetime.Transient)
            where TDbContext : DbContext
        {
            if (config != null)
            {
                options.Services.Configure(config);
            }

            options.AddPipeline <EFCoreTransactionPipeline <TDbContext> >(lifetime);

            return(options);
        }
        /// <summary>
        /// Configures the given instance as a mediator <see cref="IPipeline"/>.
        /// Registration order will be kept when executing the pipeline.
        /// </summary>
        /// <typeparam name="T">The pipeline type</typeparam>
        /// <param name="options">The mediator options</param>
        /// <param name="instance">The singleton instance</param>
        /// <returns>The options instance after changes</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static MediatorOptions AddPipeline <T>(this MediatorOptions options, T instance)
            where T : class, IPipeline
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }

            options.Services.Add(new ServiceDescriptor(typeof(IPipeline), instance));
            return(options);
        }
        /// <summary>
        /// Registers all the command, query and event handlers found in the given assembly.
        /// </summary>
        /// <param name="options">The mediator options</param>
        /// <param name="assembly">The assembly to scan</param>
        /// <param name="lifetime">The handlers lifetime</param>
        /// <returns>The options instance after changes</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static MediatorOptions AddHandlersFromAssembly(this MediatorOptions options,
                                                              Assembly assembly, ServiceLifetime lifetime = ServiceLifetime.Transient)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (assembly == null)
            {
                throw new ArgumentNullException(nameof(assembly));
            }

#if NETSTANDARD1_1
            var exportedTypes = assembly.ExportedTypes.Select(t => t.GetTypeInfo());
#else
            var exportedTypes = assembly.ExportedTypes;
#endif
            foreach (var t in exportedTypes.Where(t => t.IsClass && !t.IsAbstract))
            {
#if NETSTANDARD1_1
                var implementedInterfaces = t.ImplementedInterfaces.Select(i => i.GetTypeInfo());
#else
                var implementedInterfaces = t.GetInterfaces();
#endif
                foreach (var i in implementedInterfaces.Where(e => e.IsGenericType))
                {
                    var iGenericType = i.GetGenericTypeDefinition();
                    if (iGenericType == typeof(ICommandHandler <>) ||
                        iGenericType == typeof(ICommandHandler <,>) ||
                        iGenericType == typeof(IEventHandler <>) ||
                        iGenericType == typeof(IQueryHandler <,>))
                    {
#if NETSTANDARD1_1
                        var serviceType        = i.AsType();
                        var implementationType = t.AsType();
#else
                        var serviceType        = i;
                        var implementationType = t;
#endif
                        options.Services.Add(new ServiceDescriptor(serviceType, implementationType, lifetime));
                    }
                }
            }

            return(options);
        }
        /// <summary>
        /// Configures the mediator into the <see cref="IServiceCollection"/>.
        /// </summary>
        /// <param name="services">The service collection</param>
        /// <param name="config">An optional configurations action</param>
        /// <returns>The service collection after changes</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static IServiceCollection AddMediator(this IServiceCollection services, Action <MediatorOptions> config = null)
        {
            var options = new MediatorOptions(services);

            config?.Invoke(options);

            services.TryAdd(new ServiceDescriptor(
                                typeof(IMediatorServiceProvider), typeof(MicrosoftMediatorServiceProvider), options.ServiceProviderLifetime));
            services.TryAdd(new ServiceDescriptor(
                                typeof(IMediator), typeof(Mediator), options.Lifetime));

            services.TryAdd(new ServiceDescriptor(
                                typeof(IFetcher <,>), typeof(MicrosoftFetcher <,>), ServiceLifetime.Transient));
            services.TryAdd(new ServiceDescriptor(
                                typeof(ISender <>), typeof(MicrosoftSender <>), ServiceLifetime.Transient));
            services.TryAdd(new ServiceDescriptor(
                                typeof(ISender <,>), typeof(MicrosoftSender <,>), ServiceLifetime.Transient));
            services.TryAdd(new ServiceDescriptor(
                                typeof(IBroadcaster <>), typeof(MicrosoftBroadcaster <>), ServiceLifetime.Transient));

            return(services);
        }
Ejemplo n.º 12
0
        public static IServiceCollection AddMediator(this IServiceCollection services, Action <MediatorOptions> config)
        {
            var options = new MediatorOptions(services);

            config(options);

            if (services.Contains(typeof(IMediator)))
            {
                return(services);
            }

            var mediatorLifetime = options.Lifetime;

            services
            .AddScoped <ServiceFactory>(provider => provider.GetService)
            .AddSingleton(new PipelineCollection())
            .Add(new ServiceDescriptor(typeof(IMediator), typeof(Mediator), mediatorLifetime));

            services.Add(new ServiceDescriptor(typeof(ISender), ResolveMediator, mediatorLifetime));
            services.Add(new ServiceDescriptor(typeof(IPublisher), ResolveMediator, mediatorLifetime));

            return(services);
        }