private static void AddServices(IServiceCollection services)
        {
            var typesFromRepositories = DependencyInjectionAttribute.GetBindingForAssembly(typeof(CodeSnippetsDbContext).Assembly).ToList();
            var typesFromServices     = DependencyInjectionAttribute.GetBindingForAssembly(typeof(BaseService).Assembly).ToList();

            AddServicesFromDIAttribute(typesFromRepositories, services);
            AddServicesFromDIAttribute(typesFromServices, services);
        }
        public void ShouldCreateTest()
        {
            DependencyInjectionAttribute attribute;
            string expected;

            expected  = "selid";
            attribute = new DependencyInjectionAttribute();
            Assert.IsNotNull(attribute);

            attribute.SelectorKey = expected;
            Assert.AreEqual(expected, attribute.SelectorKey);
        }
        public IEnumerable <ServiceDescriptor> ResolveService(Type type, DependencyInjectionAttribute attribute)
        {
            var handler = Handlers.Find(h => h.MatchAttribute(attribute));

            if (handler == null)
            {
                Logger.LogError("Handler for attribute {Attribute} not found.", attribute.GetType());
                throw new ArgumentException($"Handler for attribute {attribute.GetType()} not found");
            }

            var descriptors = handler.ResolveServices(type, attribute);

            return(descriptors);
        }
        private static void AddServices(IServiceCollection services)
        {
            services.AddScoped <IUserService, UserService>();

            var typesFromRepositories = DependencyInjectionAttribute.GetBindingForAssembly(typeof(CodeSnippetsDbContext).Assembly).ToList();
            var typesFromServices     = DependencyInjectionAttribute.GetBindingForAssembly(typeof(BaseServiceObject).Assembly);

            AddServicesFromDIAttribute(typesFromServices, services);
            AddServicesFromDIAttribute(typesFromRepositories, services);


            //TEST
            int a = 0;
            //END TESTS
        }
 public bool MatchAttribute(DependencyInjectionAttribute attribute)
 {
     return(attribute.GetType() == typeof(T) && MatchAdditionalCriteria((T)attribute));
 }
 public IEnumerable <ServiceDescriptor> ResolveServices(Type implementationType, DependencyInjectionAttribute attribute)
 {
     return(ResolveServices(implementationType, (T)attribute));
 }
Beispiel #7
0
        internal static TResolution AutoWireResolve <TResolution>(IReflectionFascade reflectionFascade, Type activatorType, IDependencyManager dependencyManager, Type resolutionType, string selectorKey)
        {
            ConstructorInfo constructorInfo;

            ConstructorInfo[] constructorInfos;
            ParameterInfo[]   parameterInfos;

            DependencyInjectionAttribute dependencyInjectionAttribute;
            Lazy <TResolution>           lazyConstructorInvokation = null;

            if ((object)reflectionFascade == null)
            {
                throw new ArgumentNullException(nameof(reflectionFascade));
            }

            if ((object)activatorType == null)
            {
                throw new ArgumentNullException(nameof(activatorType));
            }

            if ((object)dependencyManager == null)
            {
                throw new ArgumentNullException(nameof(dependencyManager));
            }

            if ((object)resolutionType == null)
            {
                throw new ArgumentNullException(nameof(resolutionType));
            }

            if ((object)selectorKey == null)
            {
                throw new ArgumentNullException(nameof(selectorKey));
            }

            TypeInfo _activatorTypeInfo = activatorType.GetTypeInfo();

            // TODO
            //if (!resolutionType.IsAssignableFrom(activatorType))
            //;

            // get public, instance .ctors for activation type
            constructorInfos = activatorType.GetConstructors(BindingFlags.Public | BindingFlags.Instance);

            if ((object)constructorInfos != null)
            {
                for (int constructorIndex = 0; constructorIndex < constructorInfos.Length; constructorIndex++)
                {
                    Lazy <object>[] lazyConstructorArguments;

                    constructorInfo = constructorInfos[constructorIndex];

                    // on constructor
                    dependencyInjectionAttribute = reflectionFascade.GetOneAttribute <DependencyInjectionAttribute>(constructorInfo);

                    if ((object)dependencyInjectionAttribute == null)
                    {
                        continue;
                    }

                    if (dependencyInjectionAttribute.SelectorKey != selectorKey)
                    {
                        continue;
                    }

                    if ((object)lazyConstructorInvokation != null)
                    {
                        throw new DependencyException(string.Format("More than one constructor for activator type '{0}' specified the '{1}' with selector key '{2}'.", activatorType.FullName, nameof(DependencyInjectionAttribute), selectorKey));
                    }

                    parameterInfos           = constructorInfo.GetParameters();
                    lazyConstructorArguments = new Lazy <object> [parameterInfos.Length];

                    for (int parameterIndex = 0; parameterIndex < parameterInfos.Length; parameterIndex++)
                    {
                        ParameterInfo parameterInfo;
                        Type          parameterType;
                        Lazy <object> lazyConstructorArgument;
                        DependencyInjectionAttribute parameterDependencyInjectionAttribute;

                        parameterInfo = parameterInfos[parameterIndex];
                        parameterType = parameterInfo.ParameterType;

                        // on parameter
                        parameterDependencyInjectionAttribute = reflectionFascade.GetOneAttribute <DependencyInjectionAttribute>(parameterInfo);

                        if ((object)parameterDependencyInjectionAttribute == null)
                        {
                            throw new DependencyException(string.Format("A constructor for activator type '{0}' specifying the '{1}' with selector key '{2}' had at least one parameter missing the '{1}': index='{3}';name='{4}';type='{5}'.", activatorType.FullName, nameof(DependencyInjectionAttribute), selectorKey, parameterIndex, parameterInfo.Name, parameterInfo.ParameterType.FullName));
                        }

                        lazyConstructorArgument = new Lazy <object>(() =>
                        {
                            // prevent modified closure bug
                            IDependencyManager _dependencyManager = dependencyManager;
                            Type _resolutionType = parameterType;
                            DependencyInjectionAttribute _parameterDependencyInjectionAttribute = parameterDependencyInjectionAttribute;
                            return(_dependencyManager.ResolveDependency(_resolutionType, _parameterDependencyInjectionAttribute.SelectorKey, true));
                        });

                        lazyConstructorArguments[parameterIndex] = lazyConstructorArgument;
                    }

                    lazyConstructorInvokation = new Lazy <TResolution>(() =>
                    {
                        // prevent modified closure bug
                        Type _activatorType = activatorType;
                        Lazy <object>[] _lazyConstructorArguments = lazyConstructorArguments;
                        return((TResolution)Activator.CreateInstance(_activatorType, _lazyConstructorArguments.Select(l => l.Value).ToArray()));
                    });
                }
            }

            if ((object)lazyConstructorInvokation == null)
            {
                throw new DependencyException(string.Format("Cannot find a dependency injection constructor for activator type '{0}' with selector key '{1}'.", activatorType.FullName, selectorKey));
            }

            return(lazyConstructorInvokation.Value);            // lazy loads a cascading chain of Lazy's...
        }