Exemplo n.º 1
0
 internal static void ConfigureInMemoryCommandBus(Bootstrapper bootstrapper, InMemoryCommandBusConfiguration configuration, string[] excludedCommandsDLLs, BootstrappingContext ctx)
 {
     InMemoryCommandBus.InitHandlersCollection(excludedCommandsDLLs);
     if (ctx.IsServiceRegistered(BootstrapperServiceType.IoC))
     {
         bootstrapper.AddIoCRegistration(new TypeRegistration(typeof(InMemoryCommandBus), typeof(ICommandBus), typeof(InMemoryCommandBus)));
         if (configuration != null)
         {
             bootstrapper.AddIoCRegistration(new InstanceTypeRegistration(configuration, typeof(InMemoryCommandBusConfiguration)));
         }
     }
     bootstrapper.AddNotifications(PerformCommandChecksAccordingToBootstrapperParameters(ctx, configuration));
 }
Exemplo n.º 2
0
        private static IEnumerable <BootstrapperNotification> PerformCommandChecksAccordingToBootstrapperParameters(BootstrappingContext ctx,
                                                                                                                    InMemoryCommandBusConfiguration configuration)
        {
            var notifs = new List <BootstrapperNotification>();

            foreach (var cmdType in _allTypes.Where(t => typeof(ICommand).IsAssignableFrom(t) && t.IsClass && !t.IsAbstract).AsParallel())
            {
                var handlers = _allTypes.Where(t => typeof(ICommandHandler <>).MakeGenericType(cmdType).IsAssignableFrom(t)).ToList();
                if (handlers.Count == 0)
                {
                    if (ctx.CheckOptimal)
                    {
                        notifs.Add(
                            new BootstrapperNotification(
                                BootstrapperNotificationType.Warning,
                                $"Your project doesn't contain any handler for command type {cmdType.FullName}, which is generally not desired (you forget to add implementation of it). When implementing it, don't forget to allow InMemoryCommandBus to be able to retrieve it (by adding it to your IoC container or by creating a parameterless constructor to allow to create it by reflection). If this is desired, you can ignore this warning.",
                                typeof(InMemoryCommandBus))
                            );
                    }
                }
                else if (handlers.Count > 1)
                {
                    if (ctx.Strict)
                    {
                        if (ctx.CheckOptimal)
                        {
                            notifs.Add(
                                new BootstrapperNotification(
                                    BootstrapperNotificationType.Error,
                                    $"Your project contains more than one handler for command type {cmdType.FullName}. This is not allowed by best practices, even if you configured it in the configuration, because you've configured your bootstrapper to be 'strict' and 'optimal'. To get rid of this error, remove handlers until you get only one left, or change your bootstrapper configuration.",
                                    typeof(InMemoryCommandBus))
                                );
                        }
                        else
                        {
                            notifs.Add(
                                new BootstrapperNotification(
                                    BootstrapperNotificationType.Warning,
                                    $"Your project contains more than one handler for command type {cmdType.FullName}. This is generally a bad practice, even if you configured it in the configuration. It is recommended to have only one handler per command type to avoid multiple treatment of same command. This warning can be remove by passing false to bootstrapper for the 'strict' flag.",
                                    typeof(InMemoryCommandBus))
                                );
                        }
                    }
                }
                var isHandlerCritical = handlers.Any(h => h.IsDefined(typeof(CriticalHandlerAttribute)));
                if (isHandlerCritical && configuration?.CommandAllowMultipleHandlers.Any(t => t.CommandType == cmdType && !t.ShouldWait) == true)
                {
                    notifs.Add(
                        new BootstrapperNotification(
                            BootstrapperNotificationType.Warning,
                            $"There are multiples handlers for command type {cmdType.FullName}, and at least one of them is marked as 'critical', meaning next ones shouldn't be called if critical failed. However, your configuration for multiple handlers for this specific command type doesn't state that handlers should wait for completion, meaning they're running in parallel. This configuration *cannot* ensure that critical handlers will block next ones, you should review your configuration (by setting ShouldWait to true) or remove CriticalHandler attribute, which can't be ensured in this case.",
                            typeof(InMemoryCommandBus))
                        );
                }
            }

            return(notifs.AsEnumerable());
        }
Exemplo n.º 3
0
        /// <summary>
        /// Configure the bootstrapper to use InMemory buses for dispatching commands.
        /// </summary>
        /// <param name="bootstrapper">Instance of boostrapper.</param>
        /// <param name="configuration">Configuration to use for in memory command bus.</param>
        /// <param name="excludedCommandsDLLs">DLLs name to exclude from auto-configuration into IoC
        /// (IAutoRegisterType will be ineffective).</param>
        public static Bootstrapper UseInMemoryCommandBus(this Bootstrapper bootstrapper, InMemoryCommandBusConfiguration configuration = null,
                                                         params string[] excludedCommandsDLLs)
        {
            var service = InMemoryBusesBootstrappService.Instance;

            service.BootstrappAction += (ctx) =>
            {
                InMemoryCommandBus.InitHandlersCollection(excludedCommandsDLLs);
                if (ctx.IsServiceRegistered(BootstrapperServiceType.IoC))
                {
                    bootstrapper.AddIoCRegistration(new TypeRegistration(typeof(InMemoryCommandBus), typeof(ICommandBus), typeof(InMemoryCommandBus)));
                    if (configuration != null)
                    {
                        bootstrapper.AddIoCRegistration(new InstanceTypeRegistration(configuration, typeof(InMemoryCommandBusConfiguration)));
                    }
                }
                bootstrapper.AddNotifications(PerformCommandChecksAccordingToBootstrapperParameters(ctx, configuration));
            };
            if (!bootstrapper.RegisteredServices.Any(s => s == service))
            {
                bootstrapper.AddService(service);
            }
            return(bootstrapper);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Configure the bootstrapper to use InMemory buses for dispatching commands.
        /// </summary>
        /// <param name="bootstrapper">Instance of boostrapper.</param>
        /// <param name="configuration">Configuration to use for in memory command bus.</param>
        /// <param name="excludedCommandsDLLs">DLLs name to exclude from auto-configuration into IoC
        /// (IAutoRegisterType will be ineffective).</param>
        public static Bootstrapper UseInMemoryCommandBus(this Bootstrapper bootstrapper, InMemoryCommandBusConfiguration configuration = null,
                                                         params string[] excludedCommandsDLLs)
        {
            var service = InMemoryBusesBootstrappService.Instance;

            service.BootstrappAction += (ctx) =>
            {
                ConfigureInMemoryCommandBus(bootstrapper, configuration, excludedCommandsDLLs, ctx);
            };
            if (!bootstrapper.RegisteredServices.Any(s => s == service))
            {
                bootstrapper.AddService(service);
            }
            return(bootstrapper);
        }