// <summary> // Retrieves a mapping to CLR type for the given EDM type. Assumes the MetadataWorkspace has no // </summary> internal static ObjectTypeMapping GetObjectMapping(EdmType type, MetadataWorkspace workspace) { // Check if the workspace has cspace item collection registered with it. If not, then its a case // of public materializer trying to create objects from PODR or EntityDataReader with no context. ItemCollection collection; if (workspace.TryGetItemCollection(DataSpace.CSpace, out collection)) { return((ObjectTypeMapping)workspace.GetMap(type, DataSpace.OCSpace)); } else { EdmType ospaceType; EdmType cspaceType; // If its a case of EntityDataReader with no context, the typeUsage which is passed in must contain // a cspace type. We need to look up an OSpace type in the ospace item collection and then create // ocMapping if (type.DataSpace == DataSpace.CSpace) { // if its a primitive type, then the names will be different for CSpace type and OSpace type if (Helper.IsPrimitiveType(type)) { ospaceType = workspace.GetMappedPrimitiveType(((PrimitiveType)type).PrimitiveTypeKind, DataSpace.OSpace); } else { // Metadata will throw if there is no item with this identity present. // Is this exception fine or does object materializer code wants to wrap and throw a new exception ospaceType = workspace.GetItem <EdmType>(type.FullName, DataSpace.OSpace); } cspaceType = type; } else { // In case of PODR, there is no cspace at all. We must create a fake ocmapping, with ospace types // on both the ends ospaceType = type; cspaceType = type; } // This condition must be hit only when someone is trying to materialize a legacy data reader and we // don't have the CSpace metadata. if (!Helper.IsPrimitiveType(ospaceType) && !Helper.IsEntityType(ospaceType) && !Helper.IsComplexType(ospaceType)) { throw new NotSupportedException(Strings.Materializer_UnsupportedType); } ObjectTypeMapping typeMapping; if (Helper.IsPrimitiveType(ospaceType)) { typeMapping = new ObjectTypeMapping(ospaceType, cspaceType); } else { typeMapping = DefaultObjectMappingItemCollection.LoadObjectMapping(cspaceType, ospaceType, null); } return(typeMapping); } }
public TypeUsage GetModelTypeUsage() { if (_modelTypeUsage == null) { var edmType = EdmType; // If the edm type is already a cspace type, return the same type if (edmType.DataSpace == DataSpace.CSpace || edmType.DataSpace == DataSpace.OSpace) { return(this); } TypeUsage result; if (Helper.IsRowType(edmType)) { var sspaceRowType = (RowType)edmType; var properties = new EdmProperty[sspaceRowType.Properties.Count]; for (var i = 0; i < properties.Length; i++) { var sspaceProperty = sspaceRowType.Properties[i]; var newTypeUsage = sspaceProperty.TypeUsage.GetModelTypeUsage(); properties[i] = new EdmProperty(sspaceProperty.Name, newTypeUsage); } var edmRowType = new RowType(properties, sspaceRowType.InitializerMetadata); result = Create(edmRowType, Facets); } else if (Helper.IsCollectionType(edmType)) { var sspaceCollectionType = ((CollectionType)edmType); var newTypeUsage = sspaceCollectionType.TypeUsage.GetModelTypeUsage(); result = Create(new CollectionType(newTypeUsage), Facets); } else if (Helper.IsPrimitiveType(edmType)) { result = ((PrimitiveType)edmType).ProviderManifest.GetEdmType(this); if (result == null) { throw new ProviderIncompatibleException(Strings.Mapping_ProviderReturnsNullType(ToString())); } if (!TypeSemantics.IsNullable(this)) { result = Create( result.EdmType, OverrideFacetValues( result.Facets, new FacetValues { Nullable = false })); } } else if (Helper.IsEntityTypeBase(edmType) || Helper.IsComplexType(edmType)) { result = this; } else { Debug.Assert(false, "Unexpected type found in entity data reader"); return(null); } Interlocked.CompareExchange(ref _modelTypeUsage, result, null); } return(_modelTypeUsage); }
private static bool TryGetCommonType(EdmType edmType1, EdmType edmType2, out EdmType commonEdmType) { DebugCheck.NotNull(edmType1); DebugCheck.NotNull(edmType2); if (edmType2 == edmType1) { commonEdmType = edmType1; return(true); } if (Helper.IsPrimitiveType(edmType1) && Helper.IsPrimitiveType(edmType2)) { return(TryGetCommonType( (PrimitiveType)edmType1, (PrimitiveType)edmType2, out commonEdmType)); } else if (Helper.IsCollectionType(edmType1) && Helper.IsCollectionType(edmType2)) { return(TryGetCommonType( (CollectionType)edmType1, (CollectionType)edmType2, out commonEdmType)); } else if (Helper.IsEntityTypeBase(edmType1) && Helper.IsEntityTypeBase(edmType2)) { return(TryGetCommonBaseType( edmType1, edmType2, out commonEdmType)); } else if (Helper.IsRefType(edmType1) && Helper.IsRefType(edmType2)) { return(TryGetCommonType( (RefType)edmType1, (RefType)edmType2, out commonEdmType)); } else if (Helper.IsRowType(edmType1) && Helper.IsRowType(edmType2)) { return(TryGetCommonType( (RowType)edmType1, (RowType)edmType2, out commonEdmType)); } else { commonEdmType = null; return(false); } }
// <summary> // determines if type is a primitive/scalar type. // </summary> internal static bool IsPrimitiveType(TypeUsage type) { return(Helper.IsPrimitiveType(type.EdmType)); }
// <summary> // determines if <paramref name="type" /> is primitive or enumeration type // </summary> // <param name="type"> Type to verify. </param> // <returns> // <c>true</c> if <paramref name="type" /> is primitive or enumeration type. <c>false</c> otherwise. // </returns> internal static bool IsScalarType(EdmType type) { DebugCheck.NotNull(type); return(Helper.IsPrimitiveType(type) || Helper.IsEnumType(type)); }
private bool TryFindAndCreatePrimitiveProperties( Type type, StructuralType cspaceType, StructuralType ospaceType, IEnumerable <PropertyInfo> clrProperties) { foreach (EdmProperty edmProperty in cspaceType.GetDeclaredOnlyMembers <EdmProperty>().Where <EdmProperty>((Func <EdmProperty, bool>)(p => Helper.IsPrimitiveType(p.TypeUsage.EdmType)))) { EdmProperty cspaceProperty = edmProperty; PropertyInfo propertyInfo = clrProperties.FirstOrDefault <PropertyInfo>((Func <PropertyInfo, bool>)(p => OSpaceTypeFactory.MemberMatchesByConvention(p, (EdmMember)cspaceProperty))); if (propertyInfo != (PropertyInfo)null) { PrimitiveType primitiveType; if (OSpaceTypeFactory.TryGetPrimitiveType(propertyInfo.PropertyType, out primitiveType)) { if (propertyInfo.CanRead && propertyInfo.CanWriteExtended()) { OSpaceTypeFactory.AddScalarMember(type, propertyInfo, ospaceType, cspaceProperty, (EdmType)primitiveType); } else { this.LogLoadMessage(Strings.Validator_OSpace_Convention_ScalarPropertyMissginGetterOrSetter((object)propertyInfo.Name, (object)type.FullName, (object)type.Assembly().FullName), (EdmType)cspaceType); return(false); } } else { this.LogLoadMessage(Strings.Validator_OSpace_Convention_NonPrimitiveTypeProperty((object)propertyInfo.Name, (object)type.FullName, (object)propertyInfo.PropertyType.FullName), (EdmType)cspaceType); return(false); } } else { this.LogLoadMessage(Strings.Validator_OSpace_Convention_MissingRequiredProperty((object)cspaceProperty.Name, (object)type.FullName), (EdmType)cspaceType); return(false); } } return(true); }
internal static bool IsCastAllowed(TypeUsage fromType, TypeUsage toType) { if (Helper.IsPrimitiveType(fromType.EdmType) && Helper.IsPrimitiveType(toType.EdmType) || Helper.IsPrimitiveType(fromType.EdmType) && Helper.IsEnumType(toType.EdmType) || Helper.IsEnumType(fromType.EdmType) && Helper.IsPrimitiveType(toType.EdmType)) { return(true); } if (Helper.IsEnumType(fromType.EdmType) && Helper.IsEnumType(toType.EdmType)) { return(fromType.EdmType.Equals((object)toType.EdmType)); } return(false); }