Ejemplo n.º 1
0
        public static ContainerBuilder RegisterCqrs(
            this ContainerBuilder builder,
            IEnumerable <Assembly> assemblies,
            Action <CqrsOptions>?optionBuilder = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (assemblies == null || !assemblies.Any())
            {
                throw new ArgumentNullException(nameof(CqrsOptions.AssembliesLoader));
            }
            var asses = assemblies.ToArray();

            var options = new CqrsOptions();

            optionBuilder?.Invoke(options);


            builder.RegisterType <DefaultHandlerExecutor>().As <IHandlerExecutor>();

            builder.RegisterType <AutofacExtendedServiceProvider>().As <IServiceProvider>().As <IExtendedServiceProvider>();

            builder.RegisterGeneric(typeof(GeneralStrategicQueryHandler <,>))
            .As(typeof(IQueryHandler <,>));

            builder.RegisterAssemblyTypes(asses)
            .Where(t => !t.IsClosedTypeOf(typeof(IStrategicQueryHandler <,>)))
            .AsClosedTypesOf(typeof(IQueryHandler <,>));

            builder.RegisterAssemblyTypes(asses)
            .Where(t => t.IsClosedTypeOf(typeof(IStrategicQueryHandler <,>)))
            .AsClosedTypesOf(typeof(IQueryHandler <,>), t => t.FullName);

            builder.RegisterAssemblyTypes(asses)
            .AsClosedTypesOf(typeof(IStrategyDetector <>));

            builder.RegisterAssemblyTypes(asses)
            .AsClosedTypesOf(typeof(IValidator <>))
            .AsClosedTypesOf(typeof(IValidator <>), t => t.FullName);

            builder.RegisterGenericDecorator(typeof(QueryValidatorDecorator <,>), typeof(IQueryHandler <,>), dc => {
                var implType = dc.ImplementationType;

                if (implType.GetCustomAttribute(typeof(SkipValidationAttribute)) != null)
                {
                    return(false);
                }

                if (!(implType.GetCustomAttribute(typeof(SkipDecoratorAttribute)) is SkipDecoratorAttribute attr))
                {
                    return(true);
                }

                return(attr.DecoratorTypes.All(t => t != typeof(QueryValidatorDecorator <,>)));
            });
Ejemplo n.º 2
0
 public CqrsInterceptor(EventBag eventBag, IBoltOnLogger <CqrsInterceptor> logger,
                        IEventDispatcher eventDispatcher, CqrsOptions cqrsOptions,
                        IEventPurger eventPurger)
 {
     _eventBag        = eventBag;
     _logger          = logger;
     _eventDispatcher = eventDispatcher;
     _cqrsOptions     = cqrsOptions;
     _eventPurger     = eventPurger;
 }
        public static IServiceCollection AddCqrs(this IServiceCollection services, CqrsOptions options)
        {
            var locator = new RegistrationLocator();

            foreach (var registration in locator.GetRegistrations(options.Assemblies))
            {
                services.AddTransient(registration.InterfaceType, registration.ImplementationType);
            }
            return(services.AddTransient <ICqrsBus>(p => new KlinkedCqrsBus(p, options)));
        }
Ejemplo n.º 4
0
        public static BoltOnOptions BoltOnCqrsModule(this BoltOnOptions boltOnOptions,
                                                     Action <CqrsOptions> action = null)
        {
            boltOnOptions.IsCqrsEnabled = true;
            var options = new CqrsOptions();

            action?.Invoke(options);
            boltOnOptions.AddInterceptor <CqrsInterceptor>().Before <UnitOfWorkInterceptor>();
            boltOnOptions.ServiceCollection.AddSingleton(options);
            boltOnOptions.ServiceCollection.AddTransient <IEventDispatcher, EventDispatcher>();
            return(boltOnOptions);
        }