Пример #1
0
        /// <summary>
        /// Get the ODaya query context.
        /// </summary>
        /// <param name="responseValue">The response value.</param>
        /// <param name="singleResultCollection">The content as SingleResult.Queryable.</param>
        /// <param name="actionDescriptor">The action context, i.e. action and controller name.</param>
        /// <param name="modelFunction">A function to get the model.</param>
        /// <param name="path">The OData path.</param>
        /// <returns></returns>
        private static ODataQueryContext GetODataQueryContext(
            object responseValue,
            IQueryable singleResultCollection,
            IWebApiActionDescriptor actionDescriptor,
            Func <Type, IEdmModel> modelFunction,
            ODataPath path)
        {
            Type elementClrType = GetElementType(responseValue, singleResultCollection, actionDescriptor);

            IEdmModel model = modelFunction(elementClrType);

            if (model == null)
            {
                throw Error.InvalidOperation(SRResources.QueryGetModelMustNotReturnNull);
            }

            IEdmType elementType = null;
            IEdmModelClrTypeMappingHandler typeMappingHandler = model.GetAnnotationValue <IEdmModelClrTypeMappingHandler>(model);

            if (typeMappingHandler != null)
            {
                elementType = typeMappingHandler.MapClrInstanceToEdmType(model, responseValue);
                elementType = EdmLibHelpers.UnwrapCollectionType(elementType);
            }

            if (elementType == null)
            {
                elementType = model.GetEdmType(elementClrType);
            }

            return(new ODataQueryContext(model, elementType, elementClrType, path));
        }
Пример #2
0
        internal IEdmTypeReference GetEdmType(object instance, Type type)
        {
            IEdmTypeReference edmType = null;

            IEdmObject edmObject = instance as IEdmObject;

            if (edmObject != null)
            {
                edmType = edmObject.GetEdmType();
                if (edmType == null)
                {
                    throw Error.InvalidOperation(SRResources.EdmTypeCannotBeNull, edmObject.GetType().FullName,
                                                 typeof(IEdmObject).Name);
                }
            }
            else
            {
                if (Model == null)
                {
                    throw Error.InvalidOperation(SRResources.RequestMustHaveModel);
                }

                IEdmModelClrTypeMappingHandler typeMappingHandler = Model.GetAnnotationValue <IEdmModelClrTypeMappingHandler>(Model);
                if (typeMappingHandler != null)
                {
                    edmType = typeMappingHandler.MapClrInstanceToEdmTypeReference(Model, instance);
                }

                if (edmType == null)
                {
                    _typeMappingCache = _typeMappingCache ?? Model.GetTypeMappingCache();
                    edmType           = _typeMappingCache.GetEdmType(type, Model);

                    if (edmType == null)
                    {
                        if (instance != null)
                        {
                            edmType = _typeMappingCache.GetEdmType(instance.GetType(), Model);
                        }

                        if (edmType == null)
                        {
                            throw Error.InvalidOperation(SRResources.ClrTypeNotInModel, type);
                        }
                    }
                    else if (instance != null)
                    {
                        IEdmTypeReference actualType = _typeMappingCache.GetEdmType(instance.GetType(), Model);
                        if (actualType != null && actualType != edmType)
                        {
                            edmType = actualType;
                        }
                    }
                }
            }

            return(edmType);
        }
Пример #3
0
        /// <summary>
        /// Determine if the
        /// </summary>
        /// <param name="responseValue">The response value.</param>
        /// <param name="singleResultCollection">The content as SingleResult.Queryable.</param>
        /// <param name="actionDescriptor">The action context, i.e. action and controller name.</param>
        /// <param name="modelFunction">A function to get the model.</param>
        /// <param name="path">The OData path.</param>
        /// <returns></returns>
        private static bool ContainsAutoSelectExpandProperty(
            object responseValue,
            IQueryable singleResultCollection,
            IWebApiActionDescriptor actionDescriptor,
            Func <Type, IEdmModel> modelFunction,
            ODataPath path)
        {
            Type elementClrType = GetElementType(responseValue, singleResultCollection, actionDescriptor);

            IEdmModel model = modelFunction(elementClrType);

            if (model == null)
            {
                throw Error.InvalidOperation(SRResources.QueryGetModelMustNotReturnNull);
            }

            IEdmType type = null;
            IEdmModelClrTypeMappingHandler typeMappingHandler = model.GetAnnotationValue <IEdmModelClrTypeMappingHandler>(model);

            if (typeMappingHandler != null)
            {
                type = typeMappingHandler.MapClrInstanceToEdmType(model, responseValue);
                type = EdmLibHelpers.UnwrapCollectionType(type);
            }

            if (type == null)
            {
                type = model.GetEdmType(elementClrType);
            }

            IEdmEntityType     baseEntityType = type as IEdmEntityType;
            IEdmStructuredType structuredType = type as IEdmStructuredType;
            IEdmProperty       property       = null;

            if (path != null)
            {
                string name;
                EdmLibHelpers.GetPropertyAndStructuredTypeFromPath(path.Segments, out property,
                                                                   out structuredType,
                                                                   out name);
            }

            if (baseEntityType != null)
            {
                List <IEdmEntityType> entityTypes = new List <IEdmEntityType>();
                entityTypes.Add(baseEntityType);
                entityTypes.AddRange(EdmLibHelpers.GetAllDerivedEntityTypes(baseEntityType, model));
                foreach (var entityType in entityTypes)
                {
                    IEnumerable <IEdmNavigationProperty> navigationProperties = entityType == baseEntityType
                        ? entityType.NavigationProperties()
                        : entityType.DeclaredNavigationProperties();

                    if (navigationProperties != null)
                    {
                        if (navigationProperties.Any(
                                navigationProperty =>
                                EdmLibHelpers.IsAutoExpand(navigationProperty, property, entityType, model)))
                        {
                            return(true);
                        }
                    }

                    IEnumerable <IEdmStructuralProperty> properties = entityType == baseEntityType
                        ? entityType.StructuralProperties()
                        : entityType.DeclaredStructuralProperties();

                    if (properties != null)
                    {
                        foreach (var edmProperty in properties)
                        {
                            if (EdmLibHelpers.IsAutoSelect(edmProperty, property, entityType, model))
                            {
                                return(true);
                            }
                        }
                    }
                }
            }
            else if (structuredType != null)
            {
                IEnumerable <IEdmStructuralProperty> properties = structuredType.StructuralProperties();
                if (properties != null)
                {
                    foreach (var edmProperty in properties)
                    {
                        if (EdmLibHelpers.IsAutoSelect(edmProperty, property, structuredType, model))
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }