/// <summary> /// Initializes a new instance of the <see cref="EdmEntitySetBase"/> class. /// </summary> /// <param name="name">Name of the entity set base.</param> /// <param name="elementType">The entity type of the elements in this entity set base.</param> protected EdmEntitySetBase(string name, IEdmEntityType elementType) : base(name) { EdmUtil.CheckArgumentNull(elementType, "elementType"); this.type = new EdmCollectionType(new EdmEntityTypeReference(elementType, false)); }
/// <summary> /// Initializes a new instance of the <see cref="EdmEntitySet"/> class. /// </summary> /// <param name="container">An <see cref="IEdmEntityContainer"/> containing this entity set.</param> /// <param name="name">Name of the entity set.</param> /// <param name="elementType">The entity type of the elements in this entity set.</param> public EdmEntitySet(IEdmEntityContainer container, string name, IEdmEntityType elementType) : base(name, elementType) { EdmUtil.CheckArgumentNull(container, "container"); this.container = container; this.type = new EdmCollectionType(new EdmEntityTypeReference(elementType, false)); this.path = new EdmPathExpression(this.container.FullName() + "." + this.Name); }
private static bool GetIsNullable(IEdmCollectionType collectionType) { // check if the member type is entity, if yes, pass in default value (true), // as per spec: A navigation property whose Type attribute specifies a collection MUST NOT // specify a value for the Nullable attribute as the collection always exists, it may just be empty. // else replace with the nullable in member type reference IEdmTypeReference elementType = collectionType.ElementType; if (elementType == null) { return true; } IEdmEntityTypeReference entityReference = elementType as IEdmEntityTypeReference; return entityReference != null || collectionType.ElementType.IsNullable; }
/// <summary> /// Writes collection value in body operation parameter. /// </summary> /// <param name="parameterWriter">The odata parameter writer.</param> /// <param name="operationParameter">The operation parameter.</param> /// <param name="edmCollectionType">The edm collection type.</param> private void WriteCollectionValueInBodyOperationParameter(ODataParameterWriter parameterWriter, BodyOperationParameter operationParameter, IEdmCollectionType edmCollectionType) { ClientEdmModel model = this.requestInfo.Model; if (edmCollectionType.ElementType.TypeKind() == EdmTypeKind.Entity) { ODataWriter feedWriter = parameterWriter.CreateFeedWriter(operationParameter.Name); feedWriter.WriteStart(new ODataFeed()); IEnumerator enumerator = ((ICollection)operationParameter.Value).GetEnumerator(); while (enumerator.MoveNext()) { Object collectionItem = enumerator.Current; if (collectionItem == null) { throw new NotSupportedException(Strings.Serializer_NullCollectionParamterItemValue(operationParameter.Name)); } IEdmType edmItemType = model.GetOrCreateEdmType(collectionItem.GetType()); Debug.Assert(edmItemType != null, "edmItemType != null"); if (edmItemType.TypeKind != EdmTypeKind.Entity) { throw new NotSupportedException(Strings.Serializer_InvalidCollectionParamterItemType(operationParameter.Name, edmItemType.TypeKind)); } Debug.Assert(model.GetClientTypeAnnotation(edmItemType).ElementType != null, "edmItemType.GetClientTypeAnnotation().ElementType != null"); ODataEntry entry = this.CreateODataEntryFromEntityOperationParameter(model.GetClientTypeAnnotation(edmItemType), collectionItem); Debug.Assert(entry != null, "entry != null"); feedWriter.WriteStart(entry); feedWriter.WriteEnd(); } feedWriter.WriteEnd(); feedWriter.Flush(); } else { ODataCollectionWriter collectionWriter = parameterWriter.CreateCollectionWriter(operationParameter.Name); ODataCollectionStart odataCollectionStart = new ODataCollectionStart(); collectionWriter.WriteStart(odataCollectionStart); IEnumerator enumerator = ((ICollection)operationParameter.Value).GetEnumerator(); while (enumerator.MoveNext()) { Object collectionItem = enumerator.Current; if (collectionItem == null) { collectionWriter.WriteItem(null); continue; } IEdmType edmItemType = model.GetOrCreateEdmType(collectionItem.GetType()); Debug.Assert(edmItemType != null, "edmItemType != null"); switch (edmItemType.TypeKind) { case EdmTypeKind.Complex: { Debug.Assert(model.GetClientTypeAnnotation(edmItemType).ElementType != null, "edmItemType.GetClientTypeAnnotation().ElementType != null"); ODataComplexValue complexValue = this.propertyConverter.CreateODataComplexValue(model.GetClientTypeAnnotation(edmItemType).ElementType, collectionItem, null /*propertyName*/, false /*isCollectionItem*/, null /*visitedComplexTypeObjects*/); Debug.Assert(complexValue != null, "complexValue != null"); collectionWriter.WriteItem(complexValue); break; } case EdmTypeKind.Primitive: { object primitiveItemValue = ODataPropertyConverter.ConvertPrimitiveValueToRecognizedODataType(collectionItem, collectionItem.GetType()); collectionWriter.WriteItem(primitiveItemValue); break; } case EdmTypeKind.Enum: { ODataEnumValue enumTmp = this.propertyConverter.CreateODataEnumValue(model.GetClientTypeAnnotation(edmItemType).ElementType, collectionItem, false); collectionWriter.WriteItem(enumTmp); break; } default: // EdmTypeKind.Entity // EdmTypeKind.Row // EdmTypeKind.EntityReference throw new NotSupportedException(Strings.Serializer_InvalidCollectionParamterItemType(operationParameter.Name, edmItemType.TypeKind)); } } collectionWriter.WriteEnd(); collectionWriter.Flush(); } }
/// <summary> /// Initializes a new instance of the <see cref="EdmCollectionTypeReference"/> class. /// </summary> /// <param name="collectionType">The type definition this reference refers to.</param> /// <param name="isNullable">Denotes whether the type can be nullable.</param> public EdmCollectionTypeReference(IEdmCollectionType collectionType, bool isNullable) : base(collectionType, isNullable) { }
EdmEntityObjectCollection Get(IEdmCollectionType edmType, string sqlCmd, List<ExpandedNavigationSelectItem> expands = null) { var entityType = edmType.ElementType.AsEntity(); EdmEntityObjectCollection collection = new EdmEntityObjectCollection(new EdmCollectionTypeReference(edmType)); using (DbAccess db = new DbAccess(this.ConnectionString)) { db.ExecuteReader(sqlCmd, (reader) => { EdmEntityObject entity = new EdmEntityObject(entityType); for (int i = 0; i < reader.FieldCount; i++) { reader.SetEntityPropertyValue(i, entity); } if (expands != null) { foreach (var expanded in expands) { List<string> condition = new List<string>(); foreach (NavigationPropertySegment item in expanded.PathToNavigationProperty) { foreach (var p in item.NavigationProperty.ReferentialConstraint.PropertyPairs) { condition.Add(packCondition(p, reader[p.DependentProperty.Name])); } } var ss = Get(expanded.NavigationSource.Type as IEdmCollectionType, BuildSqlQueryCmd(expanded, string.Join(" and ", condition))); bool t = entity.TrySetPropertyValue(expanded.NavigationSource.Name, ss); } } collection.Add(entity); }, null, CommandType.Text); } return collection; }
public static IEdmFunctionImport FindBindableAction(this IEnumerable<IEdmFunctionImport> functions, IEdmCollectionType collectionType, string actionIdentifier) { if (functions == null) { throw Error.ArgumentNull("functions"); } if (collectionType == null) { throw Error.ArgumentNull("collectionType"); } if (actionIdentifier == null) { throw Error.ArgumentNull("actionIdentifier"); } IEdmFunctionImport[] matches = functions.GetMatchingActions(actionIdentifier).Where(fi => fi.CanBindTo(collectionType)).ToArray(); if (matches.Length > 1) { IEdmEntityType elementType = collectionType.ElementType as IEdmEntityType; Contract.Assert(elementType != null); throw Error.Argument( "actionIdentifier", SRResources.ActionResolutionFailed, actionIdentifier, String.Join(", ", matches.Select(match => match.Container.FullName() + "." + match.Name))); } else if (matches.Length == 1) { return matches[0]; } else { return null; } }
public static bool CanBindTo(this IEdmFunctionImport function, IEdmCollectionType collection) { if (function == null) { throw Error.ArgumentNull("function"); } if (collection == null) { throw Error.ArgumentNull("collection"); } if (!function.IsBindable) { return false; } // The binding parameter is the first parameter by convention IEdmFunctionParameter bindingParameter = function.Parameters.FirstOrDefault(); if (bindingParameter == null) { return false; } IEdmCollectionType bindingParameterType = bindingParameter.Type.Definition as IEdmCollectionType; if (bindingParameterType == null) { return false; } IEdmEntityType bindingParameterElementType = bindingParameterType.ElementType.Definition as IEdmEntityType; IEdmEntityType entity = collection.ElementType.Definition as IEdmEntityType; if (bindingParameterElementType == null || entity == null) { return false; } return entity.IsOrInheritsFrom(bindingParameterElementType); }
/// <summary> /// Initializes a new instance of the <see cref="EdmCollectionTypeReference"/> class. /// </summary> /// <param name="collectionType">The type definition this reference refers to.</param> public EdmCollectionTypeReference(IEdmCollectionType collectionType) : base(collectionType, GetIsNullable(collectionType)) { }
public static IEdmFunctionImport FindBindableAction(this IEnumerable<IEdmFunctionImport> functions, IEdmCollectionType collectionType, string actionIdentifier) { if (functions == null) { throw Error.ArgumentNull("functions"); } if (collectionType == null) { throw Error.ArgumentNull("collectionType"); } if (actionIdentifier == null) { throw Error.ArgumentNull("actionIdentifier"); } IEnumerable<IEdmFunctionImport> matches = functions.GetMatchingActions(actionIdentifier).Where(fi => fi.CanBindTo(collectionType)); IEdmEntityType elementType = (IEdmEntityType)collectionType.ElementType.Definition; return FindBest(actionIdentifier, matches, elementType, isCollection: true); }
private static bool IsEquivalentTo(this IEdmCollectionType thisType, IEdmCollectionType otherType) { return thisType.ElementType.IsEquivalentTo(otherType.ElementType); }
/// <summary> /// Constructs a Collection type reference from definition /// </summary> /// <param name="definition">The Collection type definition</param> /// <returns>The Collection type reference</returns> public static IEdmCollectionTypeReference CollectionTypeReference(IEdmCollectionType definition) { return new EdmCollectionTypeReference(definition); }