示例#1
0
        public static IBootstrapConfiguration Configuration()
        {
            var result  = new BootstrapConfiguration();
            var section = ConfigurationSectionProvider.Open <BootstrapSection>("shuttle", "bootstrap");

            var reflectionService = new ReflectionService();

            if (section != null)
            {
                result.Scan = section.Scan;

                foreach (BootstrapAssemblyElement assemblyElement in section.Assemblies)
                {
                    var assembly = reflectionService.FindAssemblyNamed(assemblyElement.Name);

                    if (assembly == null)
                    {
                        throw new ConfigurationErrorsException(string.Format(InfrastructureResources.AssemblyNameNotFound, assemblyElement.Name));
                    }

                    result.AddAssembly(assembly);
                }
            }

            if (result.Scan != BootstrapScan.None)
            {
                foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
                {
                    result.AddAssembly(assembly);
                }
            }

            switch (result.Scan)
            {
            case BootstrapScan.All:
            {
                foreach (var assembly in reflectionService.GetAssemblies())
                {
                    result.AddAssembly(assembly);
                }
                break;
            }

            case BootstrapScan.Shuttle:
            {
                foreach (var assembly in reflectionService.GetMatchingAssemblies("^Shuttle\\."))
                {
                    result.AddAssembly(assembly);
                }
                break;
            }
            }

            return(result);
        }
        /// <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);
            }
        }
示例#3
0
        /// <summary>
        ///     Creates an instance of all types implementing the `IComponentResolverBootstrap` interface and calls the `Resolve`
        ///     method.
        /// </summary>
        /// <param name="resolver">The `IComponentResolver` instance to resolve dependencies from.</param>
        /// <param name="resolverConfiguration">The `IComponentResolverConfiguration` instance that contains the registry configuration.</param>
        /// <param name="bootstrapConfiguration">The `IBootstrapConfiguration` instance that contains the bootstrapping configuration.</param>
        public static void ResolverBoostrap(IComponentResolver resolver, IComponentResolverConfiguration resolverConfiguration, IBootstrapConfiguration bootstrapConfiguration)
        {
            Guard.AgainstNull(resolver, "resolver");
            Guard.AgainstNull(resolverConfiguration, nameof(resolverConfiguration));

            var reflectionService = new ReflectionService();

            foreach (var assembly in bootstrapConfiguration.Assemblies)
            {
                foreach (var type in reflectionService.GetTypes <IComponentResolverBootstrap>(assembly))
                {
                    type.AssertDefaultConstructor(string.Format(InfrastructureResources.DefaultConstructorRequired,
                                                                "IComponentResolverBootstrap", type.FullName));

                    ((IComponentResolverBootstrap)Activator.CreateInstance(type)).Resolve(resolver);
                }
            }

            foreach (var component in resolverConfiguration.Components)
            {
                resolver.Resolve(component.DependencyType);
            }
        }