private ColumnMap CreateStructuralColumnMap(TypeUsage type, string name) { TypeInfo typeInfo = this.m_typeInfo.GetTypeInfo(type); if (TypeSemantics.IsRowType(type)) { return((ColumnMap)this.CreateRecordColumnMap(typeInfo, name)); } if (TypeSemantics.IsReferenceType(type)) { return((ColumnMap)this.CreateRefColumnMap(typeInfo, name)); } if (typeInfo.HasTypeIdProperty) { return((ColumnMap)this.CreatePolymorphicColumnMap(typeInfo, name)); } if (TypeSemantics.IsComplexType(type)) { return((ColumnMap)this.CreateComplexTypeColumnMap(typeInfo, name, (ComplexTypeColumnMap)null, (Dictionary <object, TypedColumnMap>)null, (List <TypedColumnMap>)null)); } if (TypeSemantics.IsEntityType(type)) { return((ColumnMap)this.CreateEntityColumnMap(typeInfo, name, (EntityColumnMap)null, (Dictionary <object, TypedColumnMap>)null, (List <TypedColumnMap>)null, true)); } throw new NotSupportedException(type.Identity); }
private SimplePolymorphicColumnMap CreatePolymorphicColumnMap( TypeInfo typeInfo, string name) { Dictionary <object, TypedColumnMap> dictionary = new Dictionary <object, TypedColumnMap>(typeInfo.RootType.DiscriminatorMap == null ? (IEqualityComparer <object>)null : (IEqualityComparer <object>)TrailingSpaceComparer.Instance); List <TypedColumnMap> allMaps = new List <TypedColumnMap>(); TypeInfo rootType = (TypeInfo)typeInfo.RootType; SimpleColumnMap typeIdColumnMap = this.CreateTypeIdColumnMap(rootType.TypeIdProperty); if (TypeSemantics.IsComplexType(typeInfo.Type)) { this.CreateComplexTypeColumnMap(rootType, name, (ComplexTypeColumnMap)null, dictionary, allMaps); } else { this.CreateEntityColumnMap(rootType, name, (EntityColumnMap)null, dictionary, allMaps, true); } TypedColumnMap typedColumnMap1 = (TypedColumnMap)null; foreach (TypedColumnMap typedColumnMap2 in allMaps) { if (TypeSemantics.IsStructurallyEqual(typedColumnMap2.Type, typeInfo.Type)) { typedColumnMap1 = typedColumnMap2; break; } } System.Data.Entity.Core.Query.PlanCompiler.PlanCompiler.Assert(null != typedColumnMap1, "Didn't find requested type in polymorphic type hierarchy?"); return(new SimplePolymorphicColumnMap(typeInfo.Type, name, typedColumnMap1.Properties, typeIdColumnMap, dictionary)); }
internal static bool IsStructuredType(TypeUsage type) { if (!TypeSemantics.IsReferenceType(type) && !TypeSemantics.IsRowType(type) && (!TypeSemantics.IsEntityType(type) && !TypeSemantics.IsRelationshipType(type))) { return(TypeSemantics.IsComplexType(type)); } return(true); }
/// <summary>Construct a new Complex Property mapping object</summary> /// <param name="property"> The MemberMetadata object that represents this Complex member </param> public ComplexPropertyMapping(EdmProperty property) : base(property) { Check.NotNull <EdmProperty>(property, nameof(property)); if (!TypeSemantics.IsComplexType(property.TypeUsage)) { throw new ArgumentException(Strings.StorageComplexPropertyMapping_OnlyComplexPropertyAllowed, nameof(property)); } this._typeMappings = new List <ComplexTypeMapping>(); }
/// <summary> /// Recursively add any Row types to the list of types needing a sentinel. /// </summary> /// <param name="typesNeedingNullableSentinel"> </param> /// <param name="typeUsage"> </param> private static void AddTypeNeedingNullSentinel(HashSet <string> typesNeedingNullSentinel, TypeUsage typeUsage) { if (TypeSemantics.IsCollectionType(typeUsage)) { AddTypeNeedingNullSentinel(typesNeedingNullSentinel, TypeHelpers.GetElementTypeUsage(typeUsage)); } else { if (TypeSemantics.IsRowType(typeUsage) || TypeSemantics.IsComplexType(typeUsage)) { MarkAsNeedingNullSentinel(typesNeedingNullSentinel, typeUsage); } foreach (EdmMember m in TypeHelpers.GetAllStructuralMembers(typeUsage)) { AddTypeNeedingNullSentinel(typesNeedingNullSentinel, m.TypeUsage); } } }
private static void AddTypeNeedingNullSentinel( HashSet <string> typesNeedingNullSentinel, TypeUsage typeUsage) { if (TypeSemantics.IsCollectionType(typeUsage)) { StructuredTypeNullabilityAnalyzer.AddTypeNeedingNullSentinel(typesNeedingNullSentinel, TypeHelpers.GetElementTypeUsage(typeUsage)); } else { if (TypeSemantics.IsRowType(typeUsage) || TypeSemantics.IsComplexType(typeUsage)) { StructuredTypeNullabilityAnalyzer.MarkAsNeedingNullSentinel(typesNeedingNullSentinel, typeUsage); } foreach (EdmMember structuralMember in (IEnumerable)TypeHelpers.GetAllStructuralMembers(typeUsage)) { StructuredTypeNullabilityAnalyzer.AddTypeNeedingNullSentinel(typesNeedingNullSentinel, structuralMember.TypeUsage); } } }
private void EmitField(CodeTypeDeclaration typeDecl, CodeTypeReference fieldType) { CodeMemberField memberField = new CodeMemberField(fieldType, FieldName); memberField.Attributes = MemberAttributes.Private; if (HasDefault(Item)) { memberField.InitExpression = GetDefaultValueExpression(Item); } AttributeEmitter.AddGeneratedCodeAttribute(memberField); typeDecl.Members.Add(memberField); if (TypeSemantics.IsComplexType(Item.TypeUsage)) { CodeMemberField complexInitField = new CodeMemberField(TypeReference.ForType(typeof(bool)), ComplexPropertyInitializedFieldName); complexInitField.Attributes = MemberAttributes.Private; AttributeEmitter.AddGeneratedCodeAttribute(complexInitField); typeDecl.Members.Add(complexInitField); } }
/// <summary> /// Emit static factory method which creates an instance of the class and initializes /// non-nullable properties (taken as arguments) /// </summary> /// <param name="typeDecl"></param> protected virtual void EmitFactoryMethod(CodeTypeDeclaration typeDecl) { // build list of non-nullable properties ReadOnlyMetadataCollection <EdmProperty> properties = GetProperties(); List <EdmProperty> parameters = new List <EdmProperty>(properties.Count); foreach (EdmProperty property in properties) { bool include = IncludeFieldInFactoryMethod(property); if (include) { parameters.Add(property); } } // if there are no parameters, we don't emit anything (1 is for the null element) // nor do we emit everything if this is the Ref propertied ctor and the parameter list is the same as the many parametered ctor if (parameters.Count < 1) { return; } CodeMemberMethod method = new CodeMemberMethod(); CodeTypeReference typeRef = TypeReference.FromString(Item.Name); UniqueIdentifierService uniqueIdentifierService = new UniqueIdentifierService(Generator.IsLanguageCaseSensitive); string instanceName = uniqueIdentifierService.AdjustIdentifier(Utils.CamelCase(Item.Name)); // public static Class CreateClass(...) method.Attributes = MemberAttributes.Static | MemberAttributes.Public; method.Name = "Create" + Item.Name; if (NavigationPropertyEmitter.IsNameAlreadyAMemberName(Item, method.Name, Generator.LanguageAppropriateStringComparer)) { Generator.AddError(Strings.GeneratedFactoryMethodNameConflict(method.Name, Item.Name), ModelBuilderErrorCode.GeneratedFactoryMethodNameConflict, EdmSchemaErrorSeverity.Error); } method.ReturnType = typeRef; AttributeEmitter.AddGeneratedCodeAttribute(method); // output method summary comments CommentEmitter.EmitSummaryComments(Strings.FactoryMethodSummaryComment(Item.Name), method.Comments); // Class class = new Class(); CodeVariableDeclarationStatement createNewInstance = new CodeVariableDeclarationStatement( typeRef, instanceName, new CodeObjectCreateExpression(typeRef)); method.Statements.Add(createNewInstance); CodeVariableReferenceExpression instanceRef = new CodeVariableReferenceExpression(instanceName); // iterate over the properties figuring out which need included in the factory method foreach (EdmProperty property in parameters) { // CreateClass( ... , propType propName ...) PropertyEmitter propertyEmitter = new PropertyEmitter(Generator, property, UsingStandardBaseClass); CodeTypeReference propertyTypeReference = propertyEmitter.PropertyType; String parameterName = uniqueIdentifierService.AdjustIdentifier(Utils.FixParameterName(propertyEmitter.PropertyName, "argument")); parameterName = Utils.SetSpecialCaseForFxCopOnPropertyName(parameterName); CodeParameterDeclarationExpression paramDecl = new CodeParameterDeclarationExpression( propertyTypeReference, parameterName); CodeArgumentReferenceExpression paramRef = new CodeArgumentReferenceExpression(paramDecl.Name); method.Parameters.Add(paramDecl); // add comment describing the parameter CommentEmitter.EmitParamComments(paramDecl, Strings.FactoryParamCommentGeneral(propertyEmitter.PropertyName), method.Comments); CodeExpression newPropertyValue; if (TypeSemantics.IsComplexType(propertyEmitter.Item.TypeUsage)) { List <CodeExpression> complexVerifyParameters = new List <CodeExpression>(); complexVerifyParameters.Add(paramRef); complexVerifyParameters.Add(new CodePrimitiveExpression(propertyEmitter.PropertyName)); // if (null == param) { throw new ArgumentNullException("PropertyName"); } method.Statements.Add( new CodeConditionStatement( EmitExpressionEqualsNull(paramRef), new CodeThrowExceptionStatement( new CodeObjectCreateExpression( TypeReference.ForType(typeof(ArgumentNullException)), new CodePrimitiveExpression(parameterName) ) ) ) ); newPropertyValue = paramRef; } else { newPropertyValue = paramRef; } // Scalar property: // Property = param; // Complex property: // Property = StructuralObject.VerifyComplexObjectIsNotNull(param, propertyName); method.Statements.Add(new CodeAssignStatement(new CodePropertyReferenceExpression(instanceRef, propertyEmitter.PropertyName), newPropertyValue)); } // return class; method.Statements.Add(new CodeMethodReturnStatement(instanceRef)); // actually add the method to the class typeDecl.Members.Add(method); }
private void AssignTypeIds() { int num = 0; foreach (KeyValuePair <TypeUsage, TypeInfo> typeInfo in this.m_typeInfoMap) { if (typeInfo.Value.RootType.DiscriminatorMap != null) { EntityType edmType = (EntityType)typeInfo.Key.EdmType; typeInfo.Value.TypeId = typeInfo.Value.RootType.DiscriminatorMap.GetTypeId(edmType); } else if (typeInfo.Value.IsRootType && (TypeSemantics.IsEntityType(typeInfo.Key) || TypeSemantics.IsComplexType(typeInfo.Key))) { this.AssignRootTypeId(typeInfo.Value, string.Format((IFormatProvider)CultureInfo.InvariantCulture, "{0}X", (object)num)); ++num; } } }