/// <summary> /// Creates an Edm property. /// </summary> /// <param name="declaringType">Type declaring this property.</param> /// <param name="propertyInfo">PropertyInfo instance for this property.</param> /// <returns>Returns a new instance of Edm property.</returns> private IEdmProperty CreateEdmProperty(IEdmStructuredType declaringType, PropertyInfo propertyInfo) { IEdmType propertyEdmType = this.GetOrCreateEdmType(propertyInfo.PropertyType); Debug.Assert( propertyEdmType.TypeKind == EdmTypeKind.Entity || propertyEdmType.TypeKind == EdmTypeKind.Complex || propertyEdmType.TypeKind == EdmTypeKind.Enum || propertyEdmType.TypeKind == EdmTypeKind.Primitive || propertyEdmType.TypeKind == EdmTypeKind.Collection, "Property kind should be Entity, Complex, Enum, Primitive or Collection."); IEdmProperty edmProperty = null; bool isPropertyNullable = ClientTypeUtil.CanAssignNull(propertyInfo.PropertyType); if (propertyEdmType.TypeKind == EdmTypeKind.Entity || (propertyEdmType.TypeKind == EdmTypeKind.Collection && ((IEdmCollectionType)propertyEdmType).ElementType.TypeKind() == EdmTypeKind.Entity)) { if (declaringType.TypeKind == EdmTypeKind.Entity || declaringType.TypeKind == EdmTypeKind.Complex) { if (declaringType as IEdmEntityType == null && declaringType as IEdmComplexType == null) { throw c.Error.InvalidOperation(c.Strings.ClientTypeCache_NonEntityTypeOrNonComplexTypeCannotContainEntityProperties(propertyInfo.Name, propertyInfo.DeclaringType.ToString())); } // Create a navigation property representing one side of an association. // The partner representing the other side exists only inside this property and is not added to the target entity type, // so it should not cause any name collisions. edmProperty = EdmNavigationProperty.CreateNavigationPropertyWithPartner( ClientTypeUtil.GetServerDefinedName(propertyInfo), propertyEdmType.ToEdmTypeReference(isPropertyNullable), /*dependentProperties*/ null, /*principalProperties*/ null, /*containsTarget*/ false, EdmOnDeleteAction.None, "Partner", declaringType.ToEdmTypeReference(true), /*partnerDependentProperties*/ null, /*partnerPrincipalProperties*/ null, /*partnerContainsTarget*/ false, EdmOnDeleteAction.None); } } else { edmProperty = new EdmStructuralProperty(declaringType, ClientTypeUtil.GetServerDefinedName(propertyInfo), propertyEdmType.ToEdmTypeReference(isPropertyNullable)); } edmProperty.SetClientPropertyAnnotation(new ClientPropertyAnnotation(edmProperty, propertyInfo, this)); return(edmProperty); }
/// <inheritdoc /> public IEdmTypeReference GetEdmType() { if (InstanceType != null) { IEdmStructuredType structuredType = Model.FindType(InstanceType) as IEdmStructuredType; IEdmEntityType entityType = structuredType as IEdmEntityType; if (entityType != null) { return(entityType.ToEdmTypeReference(true)); } return(structuredType.ToEdmTypeReference(true)); } Type elementType = GetElementType(); return(Model.GetTypeMappingCache().GetEdmType(elementType, Model)); }
/// <inheritdoc /> public IEdmTypeReference GetEdmType() { IEdmModel model = GetModel(); if (InstanceType != null) { IEdmStructuredType structuredType = model.FindType(InstanceType) as IEdmStructuredType; IEdmEntityType entityType = structuredType as IEdmEntityType; if (entityType != null) { return(entityType.ToEdmTypeReference(true)); } return(structuredType.ToEdmTypeReference(true)); } Type elementType = GetElementType(); return(model.GetEdmTypeReference(elementType)); }
/// <summary> /// Deserializes the given <paramref name="deltaResourceSet"/> under the given <paramref name="readContext"/>. /// </summary> /// <param name="deltaResourceSet">The delta resource set to deserialize.</param> /// <param name="readContext">The deserializer context.</param> /// <returns>The deserialized resource set object.</returns> public virtual IEnumerable ReadDeltaResourceSet(ODataDeltaResourceSetWrapper deltaResourceSet, ODataDeserializerContext readContext) { if (deltaResourceSet == null) { throw Error.ArgumentNull(nameof(deltaResourceSet)); } if (readContext == null) { throw Error.ArgumentNull(nameof(readContext)); } // resource foreach (ODataResourceBaseWrapper resourceBaseWrapper in deltaResourceSet.ResourceBases) { ODataResourceWrapper resourceWrapper = resourceBaseWrapper as ODataResourceWrapper; ODataDeletedResourceWrapper deletedResourceWrapper = resourceBaseWrapper as ODataDeletedResourceWrapper; if (resourceWrapper != null) { IEdmModel model = readContext.Model; if (model == null) { throw Error.Argument("readContext", SRResources.ModelMissingFromReadContext); } IEdmStructuredType actualType = model.FindType(resourceWrapper.Resource.TypeName) as IEdmStructuredType; if (actualType == null) { throw new ODataException(Error.Format(SRResources.ResourceTypeNotInModel, resourceWrapper.Resource.TypeName)); } IEdmTypeReference edmTypeReference = actualType.ToEdmTypeReference(true); ODataEdmTypeDeserializer deserializer = DeserializerProvider.GetEdmTypeDeserializer(edmTypeReference); if (deserializer == null) { throw new SerializationException( Error.Format(SRResources.TypeCannotBeDeserialized, edmTypeReference.FullName())); } // TODO: normal resource yield return(deserializer.ReadInline(resourceWrapper, edmTypeReference, readContext)); } else { // TODO: deleted resource } } // Delta links foreach (ODataDeltaLinkBaseWrapper deltaLinkBaseWrapper in deltaResourceSet.DeltaLinks) { ODataDeltaDeletedLinkWrapper deletedLinkWrapper = deltaLinkBaseWrapper as ODataDeltaDeletedLinkWrapper; if (deletedLinkWrapper != null) { yield return(ReadDeltaDeletedLink(deletedLinkWrapper, readContext)); } else { yield return(ReadDeltaLink((ODataDeltaLinkWrapper)deltaLinkBaseWrapper, readContext)); } } }
/// <inheritdoc/> public IEdmTypeReference GetEdmType() { return(_actualEdmType.ToEdmTypeReference(IsNullable)); }
/// <summary> /// Deserializes the given <paramref name="deltaResourceSet"/> under the given <paramref name="readContext"/>. /// </summary> /// <param name="deltaResourceSet">The delta resource set to deserialize.</param> /// <param name="elementType">The element type.</param> /// <param name="readContext">The deserializer context.</param> /// <returns>The deserialized resource set object.</returns> public virtual IEnumerable ReadDeltaResourceSet(ODataDeltaResourceSetWrapper deltaResourceSet, IEdmStructuredTypeReference elementType, ODataDeserializerContext readContext) { if (deltaResourceSet == null) { throw Error.ArgumentNull(nameof(deltaResourceSet)); } if (readContext == null) { throw Error.ArgumentNull(nameof(readContext)); } // Delta Items foreach (ODataItemWrapper itemWrapper in deltaResourceSet.DeltaItems) { // Deleted Link ODataDeltaDeletedLinkWrapper deletedLinkWrapper = itemWrapper as ODataDeltaDeletedLinkWrapper; if (deletedLinkWrapper != null) { yield return(ReadDeltaDeletedLink(deletedLinkWrapper, elementType, readContext)); } // Added Link ODataDeltaLinkWrapper deltaLinkWrapper = itemWrapper as ODataDeltaLinkWrapper; if (deltaLinkWrapper != null) { yield return(ReadDeltaLink(deltaLinkWrapper, elementType, readContext)); } // DeletedResource ODataDeletedResourceWrapper deletedResourceWrapper = itemWrapper as ODataDeletedResourceWrapper; if (deletedResourceWrapper != null) { // TODO: deleted resource yield return(null); } // Added resource or updated resource ODataResourceWrapper resourceWrapper = itemWrapper as ODataResourceWrapper; if (resourceWrapper != null) { IEdmModel model = readContext.Model; if (model == null) { throw Error.Argument("readContext", SRResources.ModelMissingFromReadContext); } IEdmStructuredType actualType = model.FindType(resourceWrapper.Resource.TypeName) as IEdmStructuredType; if (actualType == null) { throw new ODataException(Error.Format(SRResources.ResourceTypeNotInModel, resourceWrapper.Resource.TypeName)); } IEdmTypeReference edmTypeReference = actualType.ToEdmTypeReference(true); ODataEdmTypeDeserializer deserializer = DeserializerProvider.GetEdmTypeDeserializer(edmTypeReference); if (deserializer == null) { throw new SerializationException( Error.Format(SRResources.TypeCannotBeDeserialized, edmTypeReference.FullName())); } // TODO: normal resource // yield return deserializer.ReadInline(resourceWrapper, edmTypeReference, readContext); if (readContext.IsDeltaOfT) { Type elementClrType = readContext.Model.GetClrType(elementType); Type deltaType = typeof(Deltas.Delta <>).MakeGenericType(elementClrType); Deltas.IDelta delta = Activator.CreateInstance(deltaType) as Deltas.IDelta; yield return(delta); } else { yield return(new EdmDeltaResourceObject(actualType as IEdmEntityType)); } continue; } } }