Beispiel #1
0
        private static void ConnectImplementationsToTypes(ConcreteRegistration concreteRegistration,
                                                          IServiceCollection services, IEnumerable <Assembly> assembliesToScan)
        {
            var interfaceType = concreteRegistration.InterfaceType;
            var concretions   = assembliesToScan.SelectMany(assembly => assembly.GetInstancesOfInterface(interfaceType))
                                .Where(type => type.IsConcrete()).ToList();

            var exactMatches = concretions
                               .Where(concretion => concretion.CanBeCastTo(interfaceType)).ToList();

            if (concreteRegistration.AddIfAlreadyExists)
            {
                foreach (var type in exactMatches)
                {
                    services.AddTransient(interfaceType, type);
                }
            }
            else
            {
                if (exactMatches.Count > 1)
                {
                    exactMatches.RemoveAll(match => !IsMatchingWithInterface(match, interfaceType));
                }

                foreach (var type in exactMatches)
                {
                    services.TryAddTransient(interfaceType, type);
                }
            }
        }
Beispiel #2
0
        private static void ConnectImplementationsToTypesClosing(ConcreteRegistration concreteRegistration,
                                                                 IServiceCollection services, IEnumerable <Assembly> assembliesToScan)
        {
            var concretions = new List <Type>();
            var interfaces  = new List <Type>();

            foreach (var type in assembliesToScan.SelectMany(assembly => assembly.DefinedTypes)
                     .Where(type => !type.IsOpenGeneric()))
            {
                var interfaceTypes = type.FindInterfacesThatClose(concreteRegistration.InterfaceType).ToArray();
                if (!interfaceTypes.Any())
                {
                    continue;
                }

                if (type.IsConcrete())
                {
                    concretions.Add(type);
                }

                foreach (var interfaceType in interfaceTypes)
                {
                    interfaces.Fill(interfaceType);
                }
            }

            foreach (var @interface in interfaces)
            {
                var exactMatches = concretions.Where(concretion => concretion.CanBeCastTo(@interface)).ToList();
                if (concreteRegistration.AddIfAlreadyExists)
                {
                    foreach (var type in exactMatches)
                    {
                        services.AddTransient(@interface, type);
                    }
                }
                else
                {
                    if (exactMatches.Count > 1)
                    {
                        exactMatches.RemoveAll(match => !IsMatchingWithInterface(match, @interface));
                    }

                    foreach (var type in exactMatches)
                    {
                        services.TryAddTransient(@interface, type);
                    }
                }

                if ([email protected]())
                {
                    AddConcretionsThatCouldBeClosed(@interface, concretions, services);
                }
            }
        }