public void Instantiation_WithValidSection_ShouldSetSection()
        {
            const string section = "TestSection";

            var attribute = new AutoBindAttribute(section);

            Assert.Equal(section, attribute.Section);
        }
コード例 #2
0
        private static void BindToSelf(IServiceCollection source, Type type, AutoBindAttribute attr)
        {
            switch (attr.LifeCycle)
            {
            case LifeCycle.Transient:
            {
                if (attr.UseTryAdd)
                {
                    source.TryAddTransient(type);
                }
                else
                {
                    source.AddTransient(type);
                }

                break;
            }

            case LifeCycle.Scoped:
            {
                if (attr.UseTryAdd)
                {
                    source.TryAddScoped(type);
                }
                else
                {
                    source.AddScoped(type);
                }

                break;
            }

            case LifeCycle.Singleton:
            {
                if (attr.UseTryAdd)
                {
                    source.TryAddSingleton(type);
                }
                else
                {
                    source.AddSingleton(type);
                }

                break;
            }

            default:
            {
                throw new Exception($"Unsupported LifcycleType {attr.LifeCycle} provided");
            }
            }
        }
 private static ServiceLifetime?GetLifeTimeOrNull(Type type, AutoBindAttribute autoBindAttribute)
 {
     return(autoBindAttribute?.LifeCycle);
 }
        public void Instantiation_WithOutSection_ShouldSetSectionNull()
        {
            var attribute = new AutoBindAttribute();

            Assert.Null(attribute.Section);
        }
コード例 #5
0
        /// <summary>
        /// Scan supplied assemblies and bind them automatically to service collection based on attribute and convention
        /// </summary>
        /// <param name="source"></param>
        /// <param name="assemblies"></param>
        public static IServiceCollection Scan(this IServiceCollection source, IEnumerable <Assembly> assemblies, IocScannerOptions options)
        {
            options = options ?? new IocScannerOptions();

            var skipAutoBindAttributeFullName = typeof(SkipAutoBindAttribute).FullName;
            var autoBindAttributeFullName     = typeof(AutoBindAttribute).FullName;

            if (options.TypesToExclude?.Any() ?? false)
            {
                options.Exclude(t => options.TypesToExclude.Any(e => e.FullName == t.FullName));
            }

            foreach (var assembly in assemblies)
            {
                var types = assembly.GetTypes();

                foreach (var type in types)
                {
                    var typeInfo = type.GetTypeInfo();

                    var isAbstract = typeInfo.IsAbstract || typeInfo.IsInterface;

                    if (isAbstract)
                    {
                        continue;
                    }

                    var attributes = typeInfo.GetCustomAttributes();

                    if (attributes.Any(a => a.GetType().FullName == skipAutoBindAttributeFullName))
                    {
                        continue;
                    }

                    if (ShouldExclude(type, options))
                    {
                        continue;
                    }

                    var autoBindAttribute = attributes.FirstOrDefault(x => x.GetType().FullName == autoBindAttributeFullName) as AutoBindAttribute;

                    if (autoBindAttribute == null && options.SkipWhenAutoBindMissing)
                    {
                        continue;
                    }

                    var interfaces = typeInfo.GetInterfaces();

                    if (!interfaces.Any())
                    {
                        if (autoBindAttribute != null)
                        {
                            BindToSelf(source, type, autoBindAttribute);
                        }

                        continue;
                    }

                    if (interfaces.Any(x => x == typeof(IServiceRegistry)))
                    {
                        var registry = Activator.CreateInstance(type) as IServiceRegistry;

                        registry.Register(source);

                        continue;
                    }

                    if (autoBindAttribute == null)
                    {
                        autoBindAttribute = new AutoBindAttribute(LifeCycle.Transient)
                        {
                            UseTryAdd = false
                        };
                    }

                    BindToIntefaces(source, type, interfaces, autoBindAttribute);
                }
            }

            return(source);
        }
コード例 #6
0
        private static void BindAsScoped(IServiceCollection source, Type type, Type[] interfaces, AutoBindAttribute attr)
        {
            if (interfaces.Length > 1)
            {
                var firstInterface = interfaces[0];

                source.Add(ServiceDescriptor.Scoped(firstInterface, type));

                for (var i = 1; i < interfaces.Length; i++)
                {
                    source.Add(ServiceDescriptor.Scoped(interfaces[i], sp => sp.GetService(firstInterface)));
                }
            }
            else
            {
                foreach (var interfaceImplemented in interfaces)
                {
                    source.Add(ServiceDescriptor.Scoped(interfaceImplemented, type));
                }
            }
        }
コード例 #7
0
        private static void BindToIntefaces(IServiceCollection source, Type type, Type[] interfaces, AutoBindAttribute attr)
        {
            switch (attr.LifeCycle)
            {
            case LifeCycle.Transient:
            {
                if (attr.UseTryAdd)
                {
                    foreach (var interfaceImplemented in interfaces)
                    {
                        source.TryAdd(ServiceDescriptor.Transient(interfaceImplemented, type));
                    }
                }
                else
                {
                    foreach (var interfaceImplemented in interfaces)
                    {
                        source.Add(ServiceDescriptor.Transient(interfaceImplemented, type));
                    }
                }

                break;
            }

            case LifeCycle.Scoped:
            {
                if (attr.UseTryAdd)
                {
                    TryBindAsScoped(source, type, interfaces, attr);
                }
                else
                {
                    BindAsScoped(source, type, interfaces, attr);
                }

                break;
            }

            case LifeCycle.Singleton:
            {
                if (attr.UseTryAdd)
                {
                    TryBindAsSingleton(source, type, interfaces, attr);
                }
                else
                {
                    BindAsSingleton(source, type, interfaces, attr);
                }

                break;
            }

            default:
            {
                throw new Exception($"Unsupported LifcycleType {attr.LifeCycle} provided");
            }
            }
        }