private IEdmTypeReference GetEdmTypeReference(Type clrType, Dictionary <Type, EntityTypeInfo> entityTypeInfos) { bool nullable = PrimitiveTypeHelper.IsNullable(clrType); if (nullable) { Type underlyingType = Nullable.GetUnderlyingType(clrType); if (underlyingType != null) { clrType = underlyingType; } } if (entityTypeInfos.TryGetValue(clrType, out EntityTypeInfo entityTypeInfo)) { return(new EdmEntityTypeReference(entityTypeInfo.EdmType, nullable)); } if (_enumTypes.TryGetValue(clrType, out EdmEnumType edmEnumType)) { return(new EdmEnumTypeReference(edmEnumType, nullable)); } if (_complexTypes.TryGetValue(clrType, out EdmComplexType edmComplexType)) { return(new EdmComplexTypeReference(edmComplexType, nullable)); } return(PrimitiveTypeHelper.GetPrimitiveTypeRef(clrType, nullable)); }
public static IEdmTypeReference GetEdmTypeReference(this IEdmModel edmModel, Type clrType) { IEdmTypeReference edmTypeRef; bool nullable; Type underlyingType = Nullable.GetUnderlyingType(clrType); if (underlyingType == null) { nullable = clrType.IsClass; } else { clrType = underlyingType; nullable = true; } IEdmPrimitiveType primitiveEdmType = PrimitiveTypeHelper.GetPrimitiveType(clrType); if (primitiveEdmType == null) { var edmEnumType = (IEdmEnumType)edmModel.FindType(clrType.FullName); edmTypeRef = new EdmEnumTypeReference(edmEnumType, nullable); } else { edmTypeRef = EdmCoreModel.Instance.GetPrimitive(primitiveEdmType.PrimitiveKind, nullable); } return(edmTypeRef); }
private EdmStructuralProperty?BuildStructuralProperty(Dictionary <Type, EntityTypeInfo> entityTypes, Dictionary <Type, EdmEnumType> enumTypes, Dictionary <Type, EdmComplexType> complexTypes, PropertyInfo clrProperty) { bool isNullable = !_metadataProvider.IsRequired(clrProperty); IEdmTypeReference?typeRef = PrimitiveTypeHelper.GetPrimitiveTypeRef(clrProperty, isNullable); if (typeRef == null) { Type?underlyingType = null; if (clrProperty.PropertyType.IsEnum || (underlyingType = Nullable.GetUnderlyingType(clrProperty.PropertyType)) != null && underlyingType.IsEnum) { Type clrPropertyType = underlyingType ?? clrProperty.PropertyType; if (!enumTypes.TryGetValue(clrPropertyType, out EdmEnumType? edmEnumType)) { edmEnumType = OeEdmModelBuilder.CreateEdmEnumType(clrPropertyType); enumTypes.Add(clrPropertyType, edmEnumType); } typeRef = new EdmEnumTypeReference(edmEnumType, underlyingType != null); } else { if (complexTypes.TryGetValue(clrProperty.PropertyType, out EdmComplexType? edmComplexType)) { typeRef = new EdmComplexTypeReference(edmComplexType, clrProperty.PropertyType.IsClass); } else { FKeyInfo?fkeyInfo = FKeyInfo.Create(_metadataProvider, entityTypes, this, clrProperty); if (fkeyInfo != null) { _navigationClrProperties.Add(fkeyInfo); } return(null); } } } else { if (clrProperty.PropertyType == typeof(DateTime?) && enumTypes.ContainsKey(typeof(DateTime?))) { var edmType = enumTypes[typeof(DateTime?)]; typeRef = new EdmEnumTypeReference(edmType, true); } } EdmStructuralProperty edmProperty = clrProperty is Infrastructure.OeShadowPropertyInfo ? new OeEdmStructuralShadowProperty(EdmType, clrProperty.Name, typeRef, clrProperty) : new EdmStructuralProperty(EdmType, clrProperty.Name, typeRef); EdmType.AddProperty(edmProperty); if (_metadataProvider.IsKey(clrProperty)) { _keyProperties.Add(new KeyValuePair <PropertyInfo, EdmStructuralProperty>(clrProperty, edmProperty)); } return(edmProperty); }
private void BuildProperty(Dictionary <Type, EntityTypeInfo> entityTypes, Dictionary <Type, EdmEnumType> enumTypes, Dictionary <Type, EdmComplexType> complexTypes, PropertyDescriptor clrProperty) { IEdmTypeReference typeRef = PrimitiveTypeHelper.GetPrimitiveTypeRef(clrProperty); if (typeRef == null) { EdmEnumType edmEnumType; Type underlyingType = null; if (clrProperty.PropertyType.GetTypeInfo().IsEnum || (underlyingType = Nullable.GetUnderlyingType(clrProperty.PropertyType)) != null && underlyingType.GetTypeInfo().IsEnum) { Type clrPropertyType = underlyingType ?? clrProperty.PropertyType; if (!enumTypes.TryGetValue(clrPropertyType, out edmEnumType)) { edmEnumType = CreateEdmEnumType(clrPropertyType); enumTypes.Add(clrPropertyType, edmEnumType); } typeRef = new EdmEnumTypeReference(edmEnumType, underlyingType != null); } else { EdmComplexType edmComplexType; if (complexTypes.TryGetValue(clrProperty.PropertyType, out edmComplexType)) { typeRef = new EdmComplexTypeReference(edmComplexType, clrProperty.PropertyType.GetTypeInfo().IsClass); } else { FKeyInfo fkeyInfo = FKeyInfo.Create(_metadataProvider, entityTypes, this, clrProperty); if (fkeyInfo != null) { _navigationClrProperties.Add(fkeyInfo); } return; } } } else { if (clrProperty.PropertyType == typeof(DateTime?) && enumTypes.ContainsKey(typeof(DateTime?))) //zzz { var edmType = enumTypes[typeof(DateTime?)]; typeRef = new EdmEnumTypeReference(edmType, true); } } var edmProperty = new EdmStructuralProperty(_edmType, clrProperty.Name, typeRef); _edmType.AddProperty(edmProperty); if (_metadataProvider.IsKey(clrProperty)) { _keyProperties.Add(new KeyValuePair <PropertyDescriptor, EdmStructuralProperty>(clrProperty, edmProperty)); } }
private IEdmTypeReference GetEdmTypeReference(Type clrType, Dictionary <Type, EntityTypeInfo> entityTypeInfos) { bool nullable = PrimitiveTypeHelper.IsNullable(clrType); if (nullable) { Type underlyingType = Nullable.GetUnderlyingType(clrType); if (underlyingType != null) { clrType = underlyingType; } } if (entityTypeInfos.TryGetValue(clrType, out EntityTypeInfo entityTypeInfo)) { return(new EdmEntityTypeReference(entityTypeInfo.EdmType, nullable)); } if (_enumTypes.TryGetValue(clrType, out EdmEnumType edmEnumType)) { return(new EdmEnumTypeReference(edmEnumType, nullable)); } if (_complexTypes.TryGetValue(clrType, out EdmComplexType edmComplexType)) { return(new EdmComplexTypeReference(edmComplexType, nullable)); } IEdmTypeReference typeRef = PrimitiveTypeHelper.GetPrimitiveTypeRef(clrType, nullable); if (typeRef != null) { return(typeRef); } Type itemType = Parsers.OeExpressionHelper.GetCollectionItemType(clrType); if (itemType != null) { typeRef = GetEdmTypeReference(itemType, entityTypeInfos); return(new EdmCollectionTypeReference(new EdmCollectionType(typeRef))); } throw new InvalidOperationException("Not suppoertyed parameter type " + clrType.FullName); }
public static Type GetClrType(this IEdmModel edmModel, IEdmType edmType) { if (edmType.TypeKind == EdmTypeKind.Primitive) { return(PrimitiveTypeHelper.GetClrType((edmType as IEdmPrimitiveType).PrimitiveKind)); } OeValueAnnotation <Type> clrTypeAnnotation = edmModel.GetAnnotationValue <OeValueAnnotation <Type> >(edmType); if (clrTypeAnnotation != null) { return(clrTypeAnnotation.Value); } if (edmType.TypeKind == EdmTypeKind.Collection) { return(edmModel.GetClrType((edmType as IEdmCollectionType).ElementType.Definition)); } throw new InvalidOperationException("Add OeClrTypeAnnotation for " + edmType.FullTypeName()); }
private static EdmMultiplicity GetEdmMultiplicity(Type propertyType, PropertyInfo[] dependentStructuralProperties) { if (propertyType != null && Parsers.OeExpressionHelper.GetCollectionItemTypeOrNull(propertyType) != null) { return(EdmMultiplicity.Many); } if (dependentStructuralProperties.Length == 0) { return(EdmMultiplicity.Unknown); } foreach (PropertyInfo clrProperty in dependentStructuralProperties) { if (PrimitiveTypeHelper.IsNullable(clrProperty.PropertyType)) { return(EdmMultiplicity.ZeroOrOne); } } return(EdmMultiplicity.One); }
public virtual bool IsRequired(PropertyInfo propertyInfo) { return(!PrimitiveTypeHelper.IsNullable(propertyInfo.PropertyType) || propertyInfo.GetCustomAttribute(typeof(RequiredAttribute)) != null || IsKey(propertyInfo)); }
public static IEdmPrimitiveTypeReference GetPrimitiveTypeRef(Type clrType, bool nullable) { IEdmPrimitiveType primitiveEdmType = PrimitiveTypeHelper.GetPrimitiveType(clrType); return(primitiveEdmType == null ? null : EdmCoreModel.Instance.GetPrimitive(primitiveEdmType.PrimitiveKind, nullable)); }