コード例 #1
0
        public void Test_Should_Add_Bean_To_Ignore_List()
        {
            var beanOptions = new BeanOptions();

            beanOptions.IgnoreBean(typeof(Spanish2LangBean));
            beanOptions.IgnoreBean <SpanishLangBean>();
            Assert.Contains(beanOptions.IgnoredBeanList, type => type == typeof(Spanish2LangBean));
            Assert.Contains(beanOptions.IgnoredBeanList, type => type == typeof(SpanishLangBean));
        }
コード例 #2
0
        public void Test_Should_Find_GlobalBeanConfig()
        {
            var beanOptions = new BeanOptions();

            beanOptions.UseGlobalBeanName("Global");
            var beanConfig = beanOptions.FindBeanConfig(typeof(ILangBean));

            Assert.Equal("Global", beanConfig.BeanName);
            Assert.False(beanConfig.ThrowExceptionIfNotFound);
        }
コード例 #3
0
        public void Test_Should_Find_InterfaceBeanConfig()
        {
            var beanOptions = new BeanOptions();

            beanOptions.UseGlobalBeanName("Global");
            beanOptions.UseBeanNameWithError(typeof(ILangBean), "Spanish");
            var beanConfig = beanOptions.FindBeanConfig(typeof(ILangBean));

            Assert.Equal("Spanish", beanConfig.BeanName);
            Assert.True(beanConfig.ThrowExceptionIfNotFound);
        }
コード例 #4
0
        /// <summary>
        /// Function called when you are registering services in Startup,
        /// this should be called as follow:
        ///
        /// <code>
        /// services.UseBeanDiscovery();
        /// </code>
        ///
        /// Also, you can define options to this function, for example:
        ///
        /// <code>
        /// services.UseBeanDiscovery(options =>
        /// {
        ///     options.UseGlobalBeanNameWithError("Secondary") // Secondary global name will be used. Default: "Primary"
        ///     options.UseBeanNameWithError&lt;ILangRepository&gt;("English"); // Use "English" Repository for ILangRepository interface
        ///     options.$lt;CustomBean$gt;(); // CustomBean will be ignored (even it's marked with Bean Attribute)
        /// });
        /// </code>
        ///
        /// </summary>
        /// <param name="services">Service Collection</param>
        /// <param name="options">Configuration of BeanOptions</param>
        public static void UseBeanDiscovery(this IServiceCollection services, Action <BeanOptions> options = null)
        {
            var beanOptions = new BeanOptions();

            services.AddSingleton(beanOptions);
            options?.Invoke(beanOptions);
            // This call needs to be here, if this call is inside "GetBeanTypes"
            // or another method, then the actual assembly is used, and not the 'Real calling assembly'
            var assembly      = Assembly.GetCallingAssembly();
            var assemblyNames = assembly.GetReferencedAssemblies().ToList();

            assemblyNames.Add(assembly.GetName());
            DiscoverBeans(services, assemblyNames, beanOptions);
        }
コード例 #5
0
        public void Test_Should_Use_Global_BeanName(string name, bool withErrors)
        {
            var beanOptions = new BeanOptions();

            if (withErrors)
            {
                beanOptions.UseGlobalBeanNameWithError(name);
            }
            else
            {
                beanOptions.UseGlobalBeanName(name);
            }

            Assert.Equal(name, beanOptions.GlobalBeanName.BeanName);
            Assert.Equal(withErrors, beanOptions.GlobalBeanName.ThrowExceptionIfNotFound);
        }
コード例 #6
0
        public void Test_Should_Use_BeanName()
        {
            var beanOptions = new BeanOptions();

            beanOptions.UseBeanName(typeof(ILangBean), "Spanish");
            Assert.Equal("Spanish", beanOptions.InterfaceNameBag[typeof(ILangBean)].BeanName);
            Assert.False(beanOptions.InterfaceNameBag[typeof(ILangBean)].ThrowExceptionIfNotFound);
            beanOptions.UseBeanNameWithError(typeof(ILangBean), "Spanish 2");
            Assert.Equal("Spanish 2", beanOptions.InterfaceNameBag[typeof(ILangBean)].BeanName);
            Assert.True(beanOptions.InterfaceNameBag[typeof(ILangBean)].ThrowExceptionIfNotFound);
            beanOptions.UseBeanName <ILangBean>("Spanish 3");
            Assert.Equal("Spanish 3", beanOptions.InterfaceNameBag[typeof(ILangBean)].BeanName);
            Assert.False(beanOptions.InterfaceNameBag[typeof(ILangBean)].ThrowExceptionIfNotFound);
            beanOptions.UseBeanNameWithError <ILangBean>("Spanish 4");
            Assert.Equal("Spanish 4", beanOptions.InterfaceNameBag[typeof(ILangBean)].BeanName);
            Assert.True(beanOptions.InterfaceNameBag[typeof(ILangBean)].ThrowExceptionIfNotFound);
        }
コード例 #7
0
        /// <summary>
        /// This function uses BeanFinder to discover beans and then register in service collection.
        /// </summary>
        /// <param name="services">Service Collection</param>
        /// <param name="assemblyNames">Name of the assemblies where Beans will be looked for</param>
        /// <param name="beanOptions">Bean Options to be applied when discover beans</param>
        private static void DiscoverBeans(IServiceCollection services, List <AssemblyName> assemblyNames, BeanOptions beanOptions)
        {
            var beanFinder = new BeanFinder();
            var beanGroup  = beanFinder.GetBeanTypes(assemblyNames, beanOptions.IgnoredBeanList);

            beanGroup.InterfaceBeans.ForEach(interfaceBean =>
            {
                var beanConfig = beanOptions.FindBeanConfig(interfaceBean.TInterface);
                var beanData   = interfaceBean.FindBean(beanConfig);
                RegisterTypeInServiceCollection(services, interfaceBean.TInterface, beanData);
            });
            beanGroup.SingleBeans.ForEach(beanData => RegisterTypeInServiceCollection(services, beanData));
        }