/// <summary>
        ///     Registers all types that implement the `IParticipant<T>` interface against the open generic type `IParticipant<>`.
        /// </summary>
        /// <param name="registry">The `IComponentRegistry` instance to register the mapping against.</param>
        /// <param name="assembly">The assembly that contains the types to evaluate.</param>
        public static void RegisterMediatorParticipants(this IComponentRegistry registry, Assembly assembly)
        {
            Guard.AgainstNull(registry, nameof(registry));
            Guard.AgainstNull(assembly, nameof(assembly));

            var reflectionService = new ReflectionService();
            var participantType   = typeof(IParticipant <>);

            registry.RegisterCollection(participantType,
                                        reflectionService.GetTypesAssignableTo(participantType, assembly), Lifestyle.Singleton);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Attempts to register all components specified in the given `IComponentRegistryConfiguration` instance.
        /// </summary>
        /// <param name="registry">The `IComponentRegistry` instance to register the mapping against.</param>
        /// <param name="registryConfiguration">
        ///     The `IComponentRegistryConfiguration` instance that contains the registry
        ///     configuration.
        /// </param>
        public static void AttemptRegisterConfiguration(this IComponentRegistry registry, IComponentRegistryConfiguration registryConfiguration)
        {
            Guard.AgainstNull(registry, nameof(registry));

            foreach (var component in registryConfiguration.Components)
            {
                registry.Register(component.DependencyType, component.ImplementationType, component.Lifestyle);
            }

            foreach (var collection in registryConfiguration.Collections)
            {
                registry.RegisterCollection(collection.DependencyType, collection.ImplementationTypes,
                                            collection.Lifestyle);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        ///     Registers all the types in the given assembly that satisfies the `shouldRegister` function against the type
        ///     returned
        ///     from the `getDependencyType` function.
        /// </summary>
        /// <param name="registry">The `IComponentRegistry` instance to register the mapping against.</param>
        /// <param name="assembly">The assembly that contains the types to evaluate.</param>
        /// <param name="shouldRegister">A function that returns `true` to register the type; else `false` to ignore the type.</param>
        /// <param name="getDependencyType">
        ///     A function that returns the dependency `Type` that the implementation type should be
        ///     registered against; else `null` to ignore the register for no qualifying dependency type.
        /// </param>
        /// <param name="getLifestyle">A function that returns the `Lifestyle` with which to register the component.</param>
        public static void Register(this IComponentRegistry registry, Assembly assembly,
                                    Func <Type, bool> shouldRegister, Func <Type, Type> getDependencyType, Func <Type, Lifestyle> getLifestyle)
        {
            Guard.AgainstNull(registry, nameof(registry));
            Guard.AgainstNull(assembly, nameof(assembly));
            Guard.AgainstNull(shouldRegister, nameof(shouldRegister));
            Guard.AgainstNull(getDependencyType, nameof(getDependencyType));
            Guard.AgainstNull(getLifestyle, nameof(getLifestyle));

            var registrations = new Dictionary <Type, List <Type> >();

            foreach (var type in assembly.GetTypes())
            {
                if (type.IsInterface || type.IsAbstract || !shouldRegister.Invoke(type))
                {
                    continue;
                }

                var interfaceType = getDependencyType.Invoke(type);

                if (interfaceType == null)
                {
                    continue;
                }

                if (!registrations.ContainsKey(interfaceType))
                {
                    registrations.Add(interfaceType, new List <Type>());
                }

                registrations[interfaceType].Add(type);
            }

            foreach (var registration in registrations)
            {
                if (registration.Value.Count == 1)
                {
                    registry.Register(registration.Key, registration.Value.First(),
                                      getLifestyle.Invoke(registration.Key));
                }
                else
                {
                    registry.RegisterCollection(registration.Key, registration.Value,
                                                getLifestyle.Invoke(registration.Key));
                }
            }
        }
Exemplo n.º 4
0
        public void Register(IComponentRegistry registry)
        {
            Guard.AgainstNull(registry, nameof(registry));

            lock (Lock)
            {
                if (_initialized)
                {
                    return;
                }

                registry.RegisterCollection(typeof(IOAuthProvider), new ReflectionService().GetTypesAssignableTo <IOAuthProvider>(), Lifestyle.Singleton);
                registry.AttemptRegister <IOAuthProviderCollection, OAuthProviderCollection>();

                _initialized = true;
            }
        }
        /// <summary>
        ///     Creates an instance of all types implementing the `IComponentRegistryBootstrap` interface and calls the `Register`
        ///     method.
        /// </summary>
        /// <param name="registry">The `IComponentRegistry` instance to pass register the components in.</param>
        /// <param name="registryConfiguration">The `IComponentRegistryConfiguration` instance that contains the registry configuration.</param>
        /// <param name="bootstrapConfiguration">The `IBootstrapConfiguration` instance that contains the bootstrapping configuration.</param>
        public static void RegistryBoostrap(this IComponentRegistry registry,
                                            IComponentRegistryConfiguration registryConfiguration, IBootstrapConfiguration bootstrapConfiguration)
        {
            Guard.AgainstNull(registry, "registry");

            var completed = new List <Type>();

            var reflectionService = new ReflectionService();

            foreach (var assembly in bootstrapConfiguration.Assemblies)
            {
                foreach (var type in reflectionService.GetTypes <IComponentRegistryBootstrap>(assembly))
                {
                    if (completed.Contains(type))
                    {
                        continue;
                    }

                    type.AssertDefaultConstructor(string.Format(InfrastructureResources.DefaultConstructorRequired,
                                                                "IComponentRegistryBootstrap", type.FullName));

                    ((IComponentRegistryBootstrap)Activator.CreateInstance(type)).Register(registry);

                    completed.Add(type);
                }
            }

            foreach (var component in registryConfiguration.Components)
            {
                registry.Register(component.DependencyType, component.ImplementationType, component.Lifestyle);
            }

            foreach (var collection in registryConfiguration.Collections)
            {
                registry.RegisterCollection(collection.DependencyType, collection.ImplementationTypes,
                                            collection.Lifestyle);
            }
        }
Exemplo n.º 6
0
        public static void Register(IComponentRegistry registry, IServiceBusConfiguration configuration)
        {
            Guard.AgainstNull(registry, nameof(registry));
            Guard.AgainstNull(configuration, nameof(configuration));

            registry.RegistryBoostrap();

            registry.AttemptRegister(configuration);

            registry.AttemptRegister <IServiceBusEvents, ServiceBusEvents>();
            registry.AttemptRegister <ISerializer, DefaultSerializer>();
            registry.AttemptRegister <IServiceBusPolicy, DefaultServiceBusPolicy>();
            registry.AttemptRegister <IMessageRouteProvider, DefaultMessageRouteProvider>();
            registry.AttemptRegister <IIdentityProvider, DefaultIdentityProvider>();
            registry.AttemptRegister <IMessageHandlerInvoker, DefaultMessageHandlerInvoker>();
            registry.AttemptRegister <IMessageHandlingAssessor, DefaultMessageHandlingAssessor>();
            registry.AttemptRegister <IUriResolver, DefaultUriResolver>();
            registry.AttemptRegister <IQueueManager, QueueManager>();
            registry.AttemptRegister <IWorkerAvailabilityManager, WorkerAvailabilityManager>();
            registry.AttemptRegister <ISubscriptionManager, NullSubscriptionManager>();
            registry.AttemptRegister <IIdempotenceService, NullIdempotenceService>();

            registry.AttemptRegister <ITransactionScopeObserver, TransactionScopeObserver>();

            if (!registry.IsRegistered <ITransactionScopeFactory>())
            {
                var transactionScopeConfiguration = configuration.TransactionScope ??
                                                    new TransactionScopeConfiguration();

                registry.AttemptRegister <ITransactionScopeFactory>(
                    new DefaultTransactionScopeFactory(transactionScopeConfiguration.Enabled,
                                                       transactionScopeConfiguration.IsolationLevel,
                                                       TimeSpan.FromSeconds(transactionScopeConfiguration.TimeoutSeconds)));
            }

            registry.AttemptRegister <IPipelineFactory, DefaultPipelineFactory>();
            registry.AttemptRegister <ITransportMessageFactory, DefaultTransportMessageFactory>();

            var reflectionService = new ReflectionService();

            foreach (var type in reflectionService.GetTypes <IPipeline>(typeof(ServiceBus).Assembly))
            {
                if (type.IsInterface || type.IsAbstract || registry.IsRegistered(type))
                {
                    continue;
                }

                registry.Register(type, type, Lifestyle.Transient);
            }

            var observers = new List <Type>();

            foreach (var type in reflectionService.GetTypes <IPipelineObserver>(typeof(ServiceBus).Assembly))
            {
                if (type.IsInterface || type.IsAbstract)
                {
                    continue;
                }

                var interfaceType = type.InterfaceMatching($"I{type.Name}");

                if (interfaceType != null)
                {
                    if (registry.IsRegistered(type))
                    {
                        continue;
                    }

                    registry.Register(interfaceType, type, Lifestyle.Singleton);
                }
                else
                {
                    throw new EsbConfigurationException(string.Format(Resources.ObserverInterfaceMissingException, type.Name));
                }

                observers.Add(type);
            }

            registry.RegisterCollection(typeof(IPipelineObserver), observers, Lifestyle.Singleton);

            if (configuration.RegisterHandlers)
            {
                foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
                {
                    foreach (var type in reflectionService.GetTypes(MessageHandlerType, assembly))
                    {
                        foreach (var @interface in type.GetInterfaces())
                        {
                            if ([email protected](MessageHandlerType))
                            {
                                continue;
                            }

                            var genericType = MessageHandlerType.MakeGenericType(@interface.GetGenericArguments()[0]);

                            if (!registry.IsRegistered(genericType))
                            {
                                registry.Register(genericType, type, Lifestyle.Transient);
                            }
                        }
                    }
                }
            }

            var queueFactoryType = typeof(IQueueFactory);
            var queueFactoryImplementationTypes = new List <Type>();

            void AddQueueFactoryImplementationType(Type type)
            {
                if (queueFactoryImplementationTypes.Contains(type))
                {
                    return;
                }

                queueFactoryImplementationTypes.Add(type);
            }

            if (configuration.ScanForQueueFactories)
            {
                foreach (var type in new ReflectionService().GetTypes <IQueueFactory>())
                {
                    AddQueueFactoryImplementationType(type);
                }
            }

            foreach (var type in configuration.QueueFactoryTypes)
            {
                AddQueueFactoryImplementationType(type);
            }

            registry.RegisterCollection(queueFactoryType, queueFactoryImplementationTypes, Lifestyle.Singleton);

            registry.AttemptRegister <IServiceBus, ServiceBus>();
        }