private Boolean ProcessGenericTypeBindingFromPublicComposite( CompositeModel cModel, CollectionsFactory cf, Tuple <Type, Type> pcType, Type type, out ListProxy <AbstractGenericTypeBinding> result ) { var retyrn = type.IsGenericParameter; if (retyrn) { result = cf.NewListProxy(new List <AbstractGenericTypeBinding>()); } else { var array = type.GetGenericArguments().Select(t => { AbstractGenericTypeBinding gtb = null; var gIdx = Array.IndexOf(pcType.Item1.GetGenericArguments(), t); var retyrn2 = !t.IsGenericParameter || gIdx != -1; if (retyrn2) { if (t.IsGenericParameter) { var state = new IndirectGenericTypeBindingState(); state.GenericDefinition = cModel.PublicTypes.First(pt => pt.GetAllParentTypes().Select(ptt => ptt.GetGenericDefinitionIfGenericType()).Any(ptt => ptt.Equals(pcType.Item2.GetGenericDefinitionIfGenericType()))); state.GenericIndex = gIdx; gtb = new IndirectGenericTypeBindingImpl(state); } else { var state = new DirectGenericTypeBindingState(); state.Type = t; retyrn2 = this.ProcessGenericTypeBindingFromPublicComposite(cModel, cf, pcType, t, out state._innerBindings); gtb = new DirectGenericTypeBindingImpl(state); } } else { } return(Tuple.Create(retyrn2, gtb)); }).ToArray(); retyrn = array.Count(tuple => tuple.Item1) == type.GetGenericArguments().Length; if (retyrn) { result = cf.NewListProxy(array.Select(a => a.Item2).ToList()); } else { result = null; } } return(retyrn); }
public IndirectGenericTypeBindingImpl(IndirectGenericTypeBindingState state) : base(state) { this._state = state; }
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; }