internal static object GetDefaultValue(IEdmTypeReference propertyType, AssembliesResolver assembliesResolver) { Contract.Assert(propertyType != null); bool isCollection = propertyType.IsCollection(); if (!propertyType.IsNullable || isCollection) { Type clrType = GetClrTypeForUntypedDelta(propertyType, assembliesResolver); if (propertyType.IsPrimitive() || (isCollection && propertyType.AsCollection().ElementType().IsPrimitive())) { // primitive or primitive collection return(Activator.CreateInstance(clrType)); } else { // IEdmObject return(Activator.CreateInstance(clrType, propertyType)); } } return(null); }
/// <summary> /// Constructs an instance of <see cref="ODataQueryContext"/> with <see cref="IEdmModel" />, element CLR type, /// and <see cref="ODataPath" />. /// </summary> /// <param name="model">The EdmModel that includes the <see cref="IEdmType"/> corresponding to /// the given <paramref name="elementClrType"/>.</param> /// <param name="elementClrType">The CLR type of the element of the collection being queried.</param> /// <param name="path">The parsed <see cref="ODataPath"/>.</param> /// <param name="assembliesResolver"></param> public ODataQueryContext(IEdmModel model, Type elementClrType, AssembliesResolver assembliesResolver, ODataPath path) { if (model == null) { throw Error.ArgumentNull("model"); } if (elementClrType == null) { throw Error.ArgumentNull("elementClrType"); } ElementType = model.GetEdmType(elementClrType); if (ElementType == null) { throw Error.Argument("elementClrType", SRResources.ClrTypeNotInModel, elementClrType.FullName); } ElementClrType = elementClrType; Model = model; Path = path; AssembliesResolver = assembliesResolver; NavigationSource = GetNavigationSource(Model, ElementType, path); }
public virtual object ApplyQueryOptions(object value, HttpRequest request, ActionDescriptor actionDescriptor, AssembliesResolver assembliesResolver) { var elementClrType = value is IEnumerable ? TypeHelper.GetImplementedIEnumerableType(value.GetType()) : value.GetType(); var model = request.ODataProperties().Model; if (model == null) { throw Error.InvalidOperation(SRResources.QueryGetModelMustNotReturnNull); } var queryContext = new ODataQueryContext( model, elementClrType, assembliesResolver, request.ODataProperties().Path ); var queryOptions = new ODataQueryOptions(queryContext, request, assembliesResolver); var enumerable = value as IEnumerable; if (enumerable == null) { // response is single entity. return value; } // response is a collection. var query = (value as IQueryable) ?? enumerable.AsQueryable(); query = queryOptions.ApplyTo(query, new ODataQuerySettings { // TODO: If we are using SQL, set this to false // otherwise if it is entities in code then // set it to true HandleNullPropagation = //HandleNullPropagationOption.True HandleNullPropagationOptionHelper.GetDefaultHandleNullPropagationOption(query), PageSize = actionDescriptor.PageSize(), SearchDerivedTypeWhenAutoExpand = true }, AllowedQueryOptions.None); // Determine if this result should be a single entity if (ODataCountMediaTypeMapping.IsCountRequest(request)) { long? count = request.ODataProperties().TotalCount; if (count.HasValue) { // Return the count value if it is a $count request. return count.Value; } } return query; }
/// <summary> /// Initializes a new instance of the <see cref="EdmStructuredObject"/> class. /// </summary> /// <param name="edmType">The <see cref="IEdmStructuredTypeReference"/> of this object.</param> /// <param name="assembliesResolver">Assembly names to use for type resolution</param> /// <param name="isNullable">true if this object can be nullable; otherwise, false.</param> protected EdmStructuredObject(IEdmStructuredType edmType, AssembliesResolver assembliesResolver, bool isNullable) { if (edmType == null) { throw Error.ArgumentNull("edmType"); } _expectedEdmType = edmType; _assembliesResolver = assembliesResolver; _actualEdmType = edmType; IsNullable = isNullable; }
public static IEdmModel BuildEdmModel(Type apiContextType, AssembliesResolver assembliesResolver, Action<ODataConventionModelBuilder> after = null) { var builder = new ODataConventionModelBuilder(assembliesResolver) { Namespace = apiContextType.Namespace }; var publicProperties = apiContextType.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var property in publicProperties) { var entityClrType = TypeHelper.GetImplementedIEnumerableType(property.PropertyType); var entity = builder.AddEntityType(entityClrType); builder.AddEntitySet(property.Name, entity); } after?.Invoke(builder); var edmModel = builder.GetEdmModel(); return edmModel; }
internal static Type GetClrTypeForUntypedDelta(IEdmTypeReference edmType, AssembliesResolver assembliesResolver) { Contract.Assert(edmType != null); switch (edmType.TypeKind()) { case EdmTypeKind.Primitive: return(EdmLibHelpers.GetClrType(edmType.AsPrimitive(), EdmCoreModel.Instance, assembliesResolver)); case EdmTypeKind.Complex: return(typeof(EdmComplexObject)); case EdmTypeKind.Entity: return(typeof(EdmEntityObject)); case EdmTypeKind.Enum: return(typeof(EdmEnumObject)); case EdmTypeKind.Collection: IEdmTypeReference elementType = edmType.AsCollection().ElementType(); if (elementType.IsPrimitive()) { Type elementClrType = GetClrTypeForUntypedDelta(elementType, assembliesResolver); return(typeof(List <>).MakeGenericType(elementClrType)); } else if (elementType.IsComplex()) { return(typeof(EdmComplexObjectCollection)); } else if (elementType.IsEntity()) { return(typeof(EdmEntityObjectCollection)); } else if (elementType.IsEnum()) { return(typeof(EdmEnumObjectCollection)); } break; } throw Error.InvalidOperation(SRResources.UnsupportedEdmType, edmType.ToTraceString(), edmType.TypeKind()); }
public static IEdmModel BuildEdmModel(Type apiContextType, AssembliesResolver assembliesResolver, Action <ODataConventionModelBuilder> after = null) { var builder = new ODataConventionModelBuilder(assembliesResolver) { Namespace = apiContextType.Namespace }; var publicProperties = apiContextType.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var property in publicProperties) { var entityClrType = TypeHelper.GetImplementedIEnumerableType(property.PropertyType); var entity = builder.AddEntityType(entityClrType); builder.AddEntitySet(property.Name, entity); } after?.Invoke(builder); var edmModel = builder.GetEdmModel(); return(edmModel); }
/// <summary> /// Initializes a new instance of the <see cref="EdmDeltaDeletedEntityObject"/> class. /// </summary> /// <param name="entityType">The <see cref="IEdmEntityType"/> of this DeltaDeletedEntityObject.</param> /// <param name="isNullable">true if this object can be nullable; otherwise, false.</param> public EdmDeltaDeletedEntityObject(IEdmEntityType entityType, AssembliesResolver assembliesResolver, bool isNullable) : base(entityType, assembliesResolver, isNullable) { _edmType = new EdmDeltaType(entityType, EdmDeltaEntityKind.DeletedEntry); }
/// <summary> /// Initializes a new instance of the <see cref="EdmStructuredObject"/> class. /// </summary> /// <param name="edmType">The <see cref="IEdmStructuredType"/> of this object.</param> /// <param name="assembliesResolver">Assembly names to use for type resolution</param> protected EdmStructuredObject(IEdmStructuredType edmType, AssembliesResolver assembliesResolver) : this(edmType, assembliesResolver, isNullable: false) { }
/// <summary> /// Initializes a new instance of the <see cref="EdmDeltaLink"/> class. /// </summary> /// <param name="entityType">The <see cref="IEdmEntityType"/> of this DeltaLink.</param> /// <param name="isNullable">true if this object can be nullable; otherwise, false.</param> public EdmDeltaLink(IEdmEntityType entityType, AssembliesResolver assembliesResolver, bool isNullable) : base(entityType, assembliesResolver, isNullable) { _edmType = new EdmDeltaType(entityType, EdmDeltaEntityKind.LinkEntry); }
/// <summary> /// Initializes a new instance of the <see cref="EdmStructuredObject"/> class. /// </summary> /// <param name="edmType">The <see cref="IEdmStructuredType"/> of this object.</param> /// <param name="assembliesResolver">The assemblies resolve to use for type resolution</param> public EdmComplexObject(IEdmComplexType edmType, AssembliesResolver assembliesResolver) : this(edmType, assembliesResolver, isNullable: false) { }
/// <summary> /// Initializes a new instance of the <see cref="EdmStructuredObject"/> class. /// </summary> /// <param name="edmType">The <see cref="IEdmEntityType"/> of this object.</param> /// <param name="assembliesResolver">The assemblies resolve to use for type resolution</param> /// <param name="isNullable">true if this object can be nullable; otherwise, false.</param> public EdmEntityObject(IEdmEntityType edmType, AssembliesResolver assembliesResolver, bool isNullable) : base(edmType, assembliesResolver, isNullable) { }
/// <summary> /// Initializes a new instance of the <see cref="EdmStructuredObject"/> class. /// </summary> /// <param name="edmType">The <see cref="IEdmEntityType"/> of this object.</param> /// <param name="assembliesResolver">The assemblies resolve to use for type resolution</param> public EdmEntityObject(IEdmEntityType edmType, AssembliesResolver assembliesResolver) : this(edmType, assembliesResolver, isNullable : false) { }
/// <summary> /// Initializes a new instance of the <see cref="EdmDeltaDeletedEntityObject"/> class. /// </summary> /// <param name="entityType">The <see cref="IEdmEntityType"/> of this DeltaDeletedEntityObject.</param> public EdmDeltaDeletedEntityObject(IEdmEntityType entityType, AssembliesResolver assembliesResolver) : this(entityType, assembliesResolver, isNullable: false) { }
/// <summary> /// Initializes a new instance of the <see cref="EdmStructuredObject"/> class. /// </summary> /// <param name="edmType">The <see cref="IEdmComplexTypeReference"/> of this object.</param> /// <param name="assembliesResolver">The assemblies resolve to use for type resolution</param> public EdmComplexObject(IEdmComplexTypeReference edmType, AssembliesResolver assembliesResolver) : this(edmType.ComplexDefinition(), assembliesResolver, edmType.IsNullable) { }
/// <summary> /// Initializes a new instance of the <see cref="EdmStructuredObject"/> class. /// </summary> /// <param name="edmType">The <see cref="IEdmStructuredType"/> of this object.</param> /// <param name="assembliesResolver">Assembly names to use for type resolution</param> protected EdmStructuredObject(IEdmStructuredType edmType, AssembliesResolver assembliesResolver) : this(edmType, assembliesResolver, isNullable : false) { }
/// <summary> /// Initializes a new instance of the <see cref="EdmDeltaDeletedEntityObject"/> class. /// </summary> /// <param name="entityTypeReference">The <see cref="IEdmEntityTypeReference"/> of this DeltaDeletedEntityObject.</param> public EdmDeltaDeletedEntityObject(IEdmEntityTypeReference entityTypeReference, AssembliesResolver assembliesResolver) : this(entityTypeReference.EntityDefinition(), assembliesResolver, entityTypeReference.IsNullable) { }
/// <summary> /// Initializes a new instance of the <see cref="EdmDeltaDeletedEntityObject"/> class. /// </summary> /// <param name="entityType">The <see cref="IEdmEntityType"/> of this DeltaDeletedEntityObject.</param> public EdmDeltaDeletedEntityObject(IEdmEntityType entityType, AssembliesResolver assembliesResolver) : this(entityType, assembliesResolver, isNullable : false) { }
/// <summary> /// Initializes a new instance of the <see cref="EdmDeltaLink"/> class. /// </summary> /// <param name="entityType">The <see cref="IEdmEntityType"/> of this DeltaLink.</param> public EdmDeltaLink(IEdmEntityType entityType, AssembliesResolver assembliesResolver) : this(entityType, assembliesResolver, isNullable : false) { }
/// <summary> /// Initializes a new instance of the <see cref="EdmStructuredObject"/> class. /// </summary> /// <param name="edmType">The <see cref="IEdmComplexType"/> of this object.</param> /// <param name="assembliesResolver">The assemblies resolve to use for type resolution</param> /// <param name="isNullable">true if this object can be nullable; otherwise, false.</param> public EdmComplexObject(IEdmComplexType edmType, AssembliesResolver assembliesResolver, bool isNullable) : base(edmType, assembliesResolver, isNullable) { }
/// <summary> /// Initializes a new instance of the <see cref="EdmStructuredObject"/> class. /// </summary> /// <param name="edmType">The <see cref="IEdmStructuredTypeReference"/> of this object.</param> /// <param name="assembliesResolver">Assembly names to use for type resolution</param> protected EdmStructuredObject(IEdmStructuredTypeReference edmType, AssembliesResolver assembliesResolver) : this(edmType.StructuredDefinition(), assembliesResolver, edmType.IsNullable) { }
/// <summary> /// Initializes a new instance of the <see cref="EdmStructuredObject"/> class. /// </summary> /// <param name="edmType">The <see cref="IEdmStructuredType"/> of this object.</param> /// <param name="assembliesResolver">The assemblies resolve to use for type resolution</param> public EdmComplexObject(IEdmComplexType edmType, AssembliesResolver assembliesResolver) : this(edmType, assembliesResolver, isNullable : false) { }
/// <summary> /// Initializes a new instance of the <see cref="EdmStructuredObject"/> class. /// </summary> /// <param name="edmType">The <see cref="IEdmEntityTypeReference"/> of this object.</param> /// <param name="assembliesResolver">The assemblies resolve to use for type resolution</param> public EdmEntityObject(IEdmEntityTypeReference edmType, AssembliesResolver assembliesResolver) : this(edmType.EntityDefinition(), assembliesResolver, edmType.IsNullable) { }
public virtual object ApplyQueryOptions(object value, HttpRequest request, ActionDescriptor actionDescriptor, AssembliesResolver assembliesResolver) { var elementClrType = value is IEnumerable ? TypeHelper.GetImplementedIEnumerableType(value.GetType()) : value.GetType(); var model = request.ODataProperties().Model; if (model == null) { throw Error.InvalidOperation(SRResources.QueryGetModelMustNotReturnNull); } var queryContext = new ODataQueryContext( model, elementClrType, assembliesResolver, request.ODataProperties().Path ); var queryOptions = new ODataQueryOptions(queryContext, request, assembliesResolver); var enumerable = value as IEnumerable; if (enumerable == null) { // response is single entity. return(value); } // response is a collection. var query = (value as IQueryable) ?? enumerable.AsQueryable(); query = queryOptions.ApplyTo(query, new ODataQuerySettings { // TODO: If we are using SQL, set this to false // otherwise if it is entities in code then // set it to true HandleNullPropagation = //HandleNullPropagationOption.True HandleNullPropagationOptionHelper.GetDefaultHandleNullPropagationOption(query), PageSize = actionDescriptor.PageSize(), SearchDerivedTypeWhenAutoExpand = true }, AllowedQueryOptions.None); // Determine if this result should be a single entity if (ODataCountMediaTypeMapping.IsCountRequest(request)) { long?count = request.ODataProperties().TotalCount; if (count.HasValue) { // Return the count value if it is a $count request. return(count.Value); } } return(query); }
internal static Type GetClrTypeForUntypedDelta(IEdmTypeReference edmType, AssembliesResolver assembliesResolver) { Contract.Assert(edmType != null); switch (edmType.TypeKind()) { case EdmTypeKind.Primitive: return EdmLibHelpers.GetClrType(edmType.AsPrimitive(), EdmCoreModel.Instance, assembliesResolver); case EdmTypeKind.Complex: return typeof(EdmComplexObject); case EdmTypeKind.Entity: return typeof(EdmEntityObject); case EdmTypeKind.Enum: return typeof(EdmEnumObject); case EdmTypeKind.Collection: IEdmTypeReference elementType = edmType.AsCollection().ElementType(); if (elementType.IsPrimitive()) { Type elementClrType = GetClrTypeForUntypedDelta(elementType, assembliesResolver); return typeof(List<>).MakeGenericType(elementClrType); } else if (elementType.IsComplex()) { return typeof(EdmComplexObjectCollection); } else if (elementType.IsEntity()) { return typeof(EdmEntityObjectCollection); } else if (elementType.IsEnum()) { return typeof(EdmEnumObjectCollection); } break; } throw Error.InvalidOperation(SRResources.UnsupportedEdmType, edmType.ToTraceString(), edmType.TypeKind()); }
/// <summary> /// Initializes a new instance of the <see cref="EdmDeltaLink"/> class. /// </summary> /// <param name="entityType">The <see cref="IEdmEntityType"/> of this DeltaLink.</param> public EdmDeltaLink(IEdmEntityType entityType, AssembliesResolver assembliesResolver) : this(entityType, assembliesResolver, isNullable: false) { }
/// <summary> /// Initializes a new instance of the <see cref="EdmStructuredObject"/> class. /// </summary> /// <param name="edmType">The <see cref="IEdmEntityType"/> of this object.</param> /// <param name="assembliesResolver">The assemblies resolve to use for type resolution</param> public EdmEntityObject(IEdmEntityType edmType, AssembliesResolver assembliesResolver) : this(edmType, assembliesResolver, isNullable: false) { }
internal static object GetDefaultValue(IEdmTypeReference propertyType, AssembliesResolver assembliesResolver) { Contract.Assert(propertyType != null); bool isCollection = propertyType.IsCollection(); if (!propertyType.IsNullable || isCollection) { Type clrType = GetClrTypeForUntypedDelta(propertyType, assembliesResolver); if (propertyType.IsPrimitive() || (isCollection && propertyType.AsCollection().ElementType().IsPrimitive())) { // primitive or primitive collection return Activator.CreateInstance(clrType); } else { // IEdmObject return Activator.CreateInstance(clrType, propertyType); } } return null; }