Example #1
0
 /// <summary>
 /// Gets enumerable of all composite types, including private composite types, of given <see cref="CompositeModel"/>.
 /// </summary>
 /// <param name="model">The <see cref="CompositeModel"/>.</param>
 /// <returns>Enumerable of all composite types, including private composites types, of given <paramref name="model"/>.</returns>
 /// <exception cref="NullReferenceException">If <paramref name="model"/> is <c>null</c>.</exception>
 public static IEnumerable <Type> GetAllCompositeTypes(this CompositeModel model)
 {
     return(model.Methods
            .Select(cMethod => cMethod.NativeInfo.DeclaringType)
            .Concat(model.PublicTypes)
            .Concat(model.GetAllInjectableModelsWithInjectionScope <Qi4CS.Core.API.Model.ThisAttribute>().Select(i => i.TargetType))
            .GetBottomTypes());
 }
        private IDictionary <Type, TypeBindingInformationState> CreateInvocationInfos <AttributeType>(
            CompositeValidationResultMutable vResult,
            CollectionsFactory collectionsFactory,
            CompositeModel compositeModel,
            IDictionary <Type, TypeBindingInformationState> privateComposites
            )
            where AttributeType : Attribute
        {
            var injModels       = compositeModel.GetAllInjectableModelsWithInjectionScope <AttributeType>();
            var invocationTypes = injModels.GetBottomTypes(model => model.TargetType);
            var invocationInfos = new Dictionary <Type, TypeBindingInformationState>();

            foreach (var model in invocationTypes)
            {
                var actualType = model.TargetType;
                this.ModifyTypeBindingInfoDictionary(invocationInfos, this.NewTypeBindingInformation(vResult, collectionsFactory, compositeModel, model, actualType, actualType, GetBottommostDeclaringType(model), privateComposites));
            }

            return(invocationInfos);
        }
        public CompositeTypeModelImpl(CompositeModel compositeModel, CompositeValidationResultMutable vResult)
        {
            var collectionsFactory = compositeModel.ApplicationModel.CollectionsFactory;

            this._publicCompositeGenericArguments = collectionsFactory.NewListProxy(GetCompositeModelPublicTypeGenericArguments(compositeModel).ToList()).CQ;

            var injThisTypes = compositeModel.GetAllInjectableModelsWithInjectionScope <ThisAttribute>()
                               .Select(inj => new ThisTypeInfo(inj))
                               .ToArray();
            var thisTypesInModel = compositeModel.Methods
                                   .Select(m => m.NativeInfo.DeclaringType)
                                   .Concat(compositeModel.PublicTypes)
                                   .Concat(injThisTypes.Select(inj => inj.resolvedTargetType))
                                   .Concat(compositeModel.GetAllFragmentTypes().SelectMany(ft => ft.GetAllParentTypes(false)))
                                   .GetBottomTypes();

            foreach (var injThisType in injThisTypes)
            {
                injThisType.resolvedTargetTypeFromModel = thisTypesInModel.First(tType => injThisType.resolvedTargetType.GetGenericDefinitionIfContainsGenericParameters().IsAssignableFrom_IgnoreGenericArgumentsForGenericTypes(tType));
            }

            var thisTypesToProcess = new HashSet <ThisTypeInfo>(injThisTypes.GetBottomTypes(model => model.resolvedTargetType).Distinct(INJECTABLE_MODEL_EQ_COMPARER));

            thisTypesToProcess.ExceptWith(thisTypesToProcess.Where(type => compositeModel.PublicTypes.Any(role => type.resolvedTargetType.GetGenericDefinitionIfGenericType().IsAssignableFrom_IgnoreGenericArgumentsForGenericTypes(role))).ToArray());

            // TODO check that if type binding has more than one list with indirect bindings -> throw or add error.

            var privateComposites = new Dictionary <Type, TypeBindingInformationState>();

            while (thisTypesToProcess.Any())
            {
                var processedTypes = new HashSet <ThisTypeInfo>(GDEF_EQ_COMPARER);
                foreach (var thisType in thisTypesToProcess)
                {
                    if (this.ModifyTypeBindingInfoDictionary(privateComposites, this.NewTypeBindingInformation(null, collectionsFactory, compositeModel, null, thisType.resolvedTargetTypeFromModel, thisType.resolvedTargetType, GetBottommostDeclaringType(thisType.model), privateComposites)))
                    {
                        processedTypes.Add(thisType);
                    }
                }
                thisTypesToProcess.ExceptWith(thisTypesToProcess.Where(t => processedTypes.Any(p => t.resolvedTargetType.GetGenericDefinitionIfContainsGenericParameters().Equals(p.resolvedTargetType.GetGenericDefinitionIfContainsGenericParameters()))).ToArray());
                if (processedTypes.Count == 0)
                {
                    // We are stuck
                    vResult.StructureValidationErrors.Add(ValidationErrorFactory.NewStructureError("Could not deduce generic bindings for types " + String.Join(", ", thisTypesToProcess.Select(ttp => ttp.resolvedDeclaringType)), compositeModel));
                    break;
                }
                else
                {
                    processedTypes.Clear();
                }
            }

            var fragmentTypeInfos = new Dictionary <Type, TypeBindingInformationState>();

            foreach (var fragmentType in compositeModel.GetAllFragmentTypes())
            {
                // TODO check - if any parent is public or private composite type (genericdefifgenerictype), error
                if (fragmentType.IsGenericType())
                {
                    var it = FindImplementingTypeFrom(fragmentType, compositeModel.PublicTypes);
                    if (it != null || !fragmentType.ContainsGenericParameters())
                    {
                        this.ModifyTypeBindingInfoDictionary(fragmentTypeInfos, this.NewTypeBindingInformation(vResult, collectionsFactory, compositeModel, null, fragmentType, fragmentType, fragmentType, privateComposites));
                    }
                    else
                    {
                        // Private composite fragment
                        // Process all base types + injectable models of this fragment, and all their generic arguments as DFS
                        var dependentStates = Enumerable.Repeat <Tuple <Int32, TypeBindingInformationState> >(null, fragmentType.GetGenericArguments().Length).ToList();

                        foreach (var typeToProcess in fragmentType.GetAllParentTypes(false)
                                 .Concat(
                                     compositeModel.GetAllInjectableModels()
                                     .Where(inj => fragmentType.GetClassHierarchy().Contains(inj.DeclaringType))
                                     .Select(inj => inj.TargetType)
                                     ))
                        {
                            if (dependentStates.Any(item => item == null))
                            {
                                var gArgs = typeToProcess.GetGenericArguments();
                                for (var i = 0; i < gArgs.Length; ++i)
                                {
                                    foreach (var typeOrGArg in gArgs[i].AsDepthFirstEnumerable(t => t.GetGenericArgumentsSafe()))
                                    {
                                        if (typeOrGArg.IsGenericParameter && dependentStates[typeOrGArg.GenericParameterPosition] == null)
                                        {
                                            TypeBindingInformationState other;
                                            if (!privateComposites.TryGetValue(typeToProcess.GetGenericDefinitionIfContainsGenericParameters(), out other) &&
                                                compositeModel.PublicTypes.Contains(typeToProcess)
                                                )
                                            {
                                                other            = new TypeBindingInformationState(collectionsFactory);
                                                other.NativeInfo = typeToProcess;
                                            }
                                            if (other != null)
                                            {
                                                dependentStates[typeOrGArg.GenericParameterPosition] = Tuple.Create(i, other);
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                        if (!dependentStates.Any(item => item == null))
                        {
                            var state = new TypeBindingInformationState(collectionsFactory);
                            state.NativeInfo = fragmentType;
                            var max = Math.Max(1, dependentStates.Max(dState => dState.Item2.Bindings.CQ.Count));
                            if (dependentStates.Select(dState => dState.Item2.Bindings.CQ.Count).Where(c => c > 1).Distinct().Count() > 1)
                            {
                                throw new NotImplementedException("Very complex private type binding is not yet supported.");
                            }
                            else
                            {
                                for (var i = 0; i < max; ++i)
                                {
                                    state.Bindings.Add(collectionsFactory.NewListProxy(dependentStates.Select(dState =>
                                    {
                                        AbstractGenericTypeBinding gtb;
                                        if (dState.Item2.Bindings.CQ.Any())
                                        {
                                            // Private composite binding
                                            gtb = dState.Item2.Bindings.CQ[i].CQ[dState.Item1];
                                        }
                                        else
                                        {
                                            var iState = new IndirectGenericTypeBindingState();
                                            iState.GenericDefinition = dState.Item2.NativeInfo.GetGenericDefinitionIfGenericType();
                                            iState.GenericIndex      = dState.Item1;
                                            gtb = new IndirectGenericTypeBindingImpl(iState);
                                        }
                                        return(gtb);
                                    }).ToList()));
                                }
                                this.ModifyTypeBindingInfoDictionary(fragmentTypeInfos, state);
                            }
                        }
                    }
                }
                else
                {
                    var state = new TypeBindingInformationState(collectionsFactory);
                    state.NativeInfo = fragmentType;
                    this.ModifyTypeBindingInfoDictionary(fragmentTypeInfos, state);
                }
            }

            this._fragmentTypeInfos             = collectionsFactory.NewDictionaryProxy(fragmentTypeInfos.ToDictionary(kvp => kvp.Key, kvp => (TypeBindingInformation) new TypeBindingInformationImpl(kvp.Value))).CQ;
            this._concernInvocationTypeInfos    = collectionsFactory.NewDictionaryProxy(this.CreateInvocationInfos <ConcernForAttribute>(vResult, collectionsFactory, compositeModel, privateComposites).ToDictionary(kvp => kvp.Key, kvp => (TypeBindingInformation) new TypeBindingInformationImpl(kvp.Value))).CQ;
            this._sideEffectInvocationTypeInfos = collectionsFactory.NewDictionaryProxy(this.CreateInvocationInfos <SideEffectForAttribute>(vResult, collectionsFactory, compositeModel, privateComposites).ToDictionary(kvp => kvp.Key, kvp => (TypeBindingInformation) new TypeBindingInformationImpl(kvp.Value))).CQ;
            this._privateCompositeTypeInfos     = collectionsFactory.NewDictionaryProxy(privateComposites.ToDictionary(kvp => kvp.Key, kvp => (TypeBindingInformation) new TypeBindingInformationImpl(kvp.Value))).CQ;
        }