Ejemplo n.º 1
0
        public static IQuanticBuilder AddValidationDecorator(this IQuanticBuilder builder, Action <QuanticWebOptions> genesisOptions = null)
        {
            var options = new QuanticWebOptions();

            genesisOptions?.Invoke(options);

            return(builder);
        }
Ejemplo n.º 2
0
        public static IQuanticBuilder AddElasticApmDecorator(this IQuanticBuilder builder)
        {
            builder.Services.AddSingleton <ITracer>(n => Agent.Tracer);
            builder.Services.TryDecorate(typeof(ICommandHandler <>), typeof(TraceCommandHandlerDecorator <>));
            builder.Services.TryDecorate(typeof(IQueryHandler <,>), typeof(TraceQueryHandlerDecorator <,>));

            return(builder);
        }
Ejemplo n.º 3
0
        public static IQuanticBuilder AddValidationDecorator(this IQuanticBuilder builder, Action<QuanticValidationOptions> genesisOptions = null)
        {
            var options = new QuanticValidationOptions();
            genesisOptions?.Invoke(options);

            builder.Services.AddValidators(options.Assemblies ?? builder.Assemblies);  

            builder.Services.AddDecorators();          

            return builder;
        }
        public static IQuanticBuilder AddLogDecorator(this IQuanticBuilder builder, Action <QuanticLogOptions> logOptions = null)
        {
            var options = new QuanticLogOptions();

            logOptions?.Invoke(options);

            builder.Services.AddSingleton(typeof(IRequestLogger), options.RequestLoggerType ?? typeof(RequestLogger));

            builder.Services.TryDecorate(typeof(ICommandHandler <>), typeof(LogCommandHandlerDecorator <>));
            builder.Services.TryDecorate(typeof(IQueryHandler <,>), typeof(LogQueryHandlerDecorator <,>));

            return(builder);
        }
        public static IQuanticBuilder AddQuanticMassTransit(this IQuanticBuilder builder,
                                                            Action <IServiceProvider, IRabbitMqBusFactoryConfigurator> rabbitMqBusFactoryConfigurator,
                                                            Action <QuanticMassTransitRabbitMqOptions> masstransitOptions = null)
        {
            var option = new QuanticMassTransitRabbitMqOptions();

            masstransitOptions?.Invoke(option);

            var rabbitConfig = option.RabbitConfig ?? GetRabbitMqConfig(builder.Services);

            builder.Services.AddSingleton(rabbitConfig);

            var factory = new ConnectionFactory()
            {
                HostName    = rabbitConfig.Host,
                Password    = rabbitConfig.Password,
                UserName    = rabbitConfig.UserName,
                VirtualHost = "/",
                Port        = rabbitConfig.Port,
                AutomaticRecoveryEnabled = true
            };

            var connection = factory.CreateConnection();

            builder.Services.AddSingleton <IConnection>(connection);

            builder.Services.AddMassTransit(x => {
                x.AddConsumers(option.ConsumerAssemblies ?? builder.Assemblies);

                x.AddBus(provider => Bus.Factory.CreateUsingRabbitMq(cfg => {
                    Action <IRabbitMqHostConfigurator> configure = h =>
                    {
                        h.Username(rabbitConfig.UserName);
                        h.Password(rabbitConfig.Password);
                    };

                    cfg.Host(new Uri($"rabbitmq://{rabbitConfig.Host}"), "/", configure);

                    rabbitMqBusFactoryConfigurator?.Invoke(provider, cfg);
                }));
            });

            builder.Services.AddSingleton <IQuanticBus, QuanticBus>();
            builder.Services.AddSingleton <IHostedService, BusService>();

            return(builder);
        }
Ejemplo n.º 6
0
        public static IQuanticBuilder AddRedisCacheDecorator(this IQuanticBuilder builder, Action <RedisCacheOption> redisCacheOption = null)
        {
            var options = new RedisCacheOption();

            redisCacheOption?.Invoke(options);

            var cacheOptions = options?.RedisCacheOptions ?? new RedisCacheOptions();

            builder.Services.AddSingleton <IDistributedCache>(new RedisCache(cacheOptions));

            builder.Services.AddSingleton <IQueryInfoProvider, QueryInfoProvider>();

            foreach (var service in builder.Services.ToList().Where(x => ShouldDecorate(x)))
            {
                builder.Services.Decorate(service.ServiceType, typeof(RedisCacheQueryHandlerDecorator <,>)
                                          .MakeGenericType(service.ServiceType.GenericTypeArguments));
            }
        public static IQuanticBuilder AddDbRequestContextPropagationDecorator(this IQuanticBuilder builder, Action <QuanticEfConfig> efOptions = null)
        {
            var options = new QuanticEfConfig();

            efOptions?.Invoke(options);

            if (options.DbContextType == null)
            {
                throw new DbContextTypeMissingException();
            }

            builder.Services.AddSingleton <QuanticEfConfig>(options);

            foreach (var service in builder.Services.ToList().Where(x => IsCommandHandler(x.ServiceType)))
            {
                builder.Services.Decorate(service.ServiceType, typeof(DbRequestContextPropagationDecorator <>)
                                          .MakeGenericType(service.ServiceType.GenericTypeArguments));
            }
Ejemplo n.º 8
0
        public static IQuanticBuilder AddFeatureManagementDecorator(this IQuanticBuilder builder)
        {
            builder.Services.AddSingleton <IHandlerFeatureInfoProvider, HandlerFeatureInfoProvider>();

            foreach (var service in builder.Services.ToList())
            {
                if (ShouldDecorateCommandHandler(service))
                {
                    builder.Services.Decorate(service.ServiceType, typeof(FeatureManagementCommandHandlerDecorator <>)
                                              .MakeGenericType(service.ServiceType.GenericTypeArguments));
                }

                if (ShouldDecorateQueryHandler(service))
                {
                    builder.Services.Decorate(service.ServiceType, typeof(FeatureManagementQueryHandlerDecorator <,>)
                                              .MakeGenericType(service.ServiceType.GenericTypeArguments));
                }
            }
            bool ShouldDecorateCommandHandler(ServiceDescriptor serviceDescriptor)
            {
                return(serviceDescriptor.ServiceType.IsGenericType &&
                       serviceDescriptor.ImplementationType?.GetCustomAttributes <DecorateFeatureManagementAttribute>() != null &&
                       serviceDescriptor.ImplementationType.GetCustomAttributes <DecorateFeatureManagementAttribute>().Any() &&
                       serviceDescriptor.ServiceType.GetGenericTypeDefinition() == typeof(ICommandHandler <>));
            }

            bool ShouldDecorateQueryHandler(ServiceDescriptor serviceDescriptor)
            {
                return(serviceDescriptor.ServiceType.IsGenericType &&
                       serviceDescriptor.ImplementationType?.GetCustomAttributes <DecorateFeatureManagementAttribute>() != null &&
                       serviceDescriptor.ImplementationType.GetCustomAttributes <DecorateFeatureManagementAttribute>().Any() &&
                       serviceDescriptor.ServiceType.GetGenericTypeDefinition() == typeof(IQueryHandler <,>));
            }

            return(builder);
        }
        public static IQuanticBuilder AddMemoryCacheDecorator(this IQuanticBuilder builder, Action <InMemoryCacheOption> memoryCacheOption = null)
        {
            var options = new InMemoryCacheOption();

            memoryCacheOption?.Invoke(options);

            var cacheOptions = options?.MemoryCacheOptions ?? new MemoryCacheOptions();

            builder.Services.AddSingleton <IMemoryCache>(new MemoryCache(cacheOptions));

            List <Type> types = new List <Type>();

            foreach (var asm in builder.Assemblies)
            {
                types.AddRange(asm.GetTypes());
            }

            builder.Services.AddSingleton <IQueryInfoProvider>(new QueryInfoProvider(types));

            foreach (var service in builder.Services.ToList().Where(x => ShouldDecorate(x)))
            {
                builder.Services.Decorate(service.ServiceType, typeof(InMemoryCacheQueryHandlerDecorator <,>)
                                          .MakeGenericType(service.ServiceType.GenericTypeArguments));
            }