internal void AddAction(Action action)
        {
            EdmAction          edmAction;
            EdmEntityContainer entityContainer  = (EdmEntityContainer)EntityContainer;
            IEdmTypeReference  edmTypeReference = GetNotEntityTypeReference(action.ReturnType);

            if (edmTypeReference == null)
            {
                edmTypeReference = GetCollectionNotEntityTypeReference(action.ReturnType);
            }

            if (edmTypeReference != null)
            {
                edmAction = new EdmAction(DefaultNamespace, action.Name, edmTypeReference);
                entityContainer.AddActionImport(edmAction);
            }
            else if (action.ReturnType == typeof(void))
            {
                edmAction = new EdmAction(DefaultNamespace, action.Name, null);
                entityContainer.AddActionImport(edmAction);
            }
            else
            {
                IEdmTypeReference returnEdmTypeReference = GetEdmTypeReference(action.ReturnType, out IEdmEntityType returnEntityType, out bool isCollection);
                if (returnEntityType == null)
                {
                    throw new ArgumentNullException("Тип возвращаемого результата action не найден в модели OData.");
                }

                edmAction = new EdmAction(DefaultNamespace, action.Name, returnEdmTypeReference);
                edmAction.AddParameter("bindingParameter", returnEdmTypeReference);
                entityContainer.AddActionImport(action.Name, edmAction, new EdmEntitySetReferenceExpression(GetEdmEntitySet(returnEntityType)));
            }

            AddElement(edmAction);
            foreach (var parameter in action.ParametersTypes.Keys)
            {
                Type paramType = action.ParametersTypes[parameter];
                edmTypeReference = GetEdmTypeReference(paramType, out IEdmEntityType entityType, out bool isCollection);
                if (edmTypeReference == null)
                {
                    edmTypeReference = GetNotEntityTypeReference(paramType);
                    if (edmTypeReference == null)
                    {
                        edmTypeReference = GetCollectionNotEntityTypeReference(paramType);
                    }
                }

                if (edmTypeReference != null)
                {
                    edmAction.AddParameter(parameter, edmTypeReference);
                }
            }
        }
示例#2
0
        private IHttpActionResult ExecuteAction(ODataActionParameters parameters)
        {
            ODataPath odataPath = Request.ODataProperties().Path;
            UnboundActionPathSegment segment = odataPath.Segments[odataPath.Segments.Count - 1] as UnboundActionPathSegment;

            if (segment == null || !_functions.IsRegistered(segment.ActionName))
            {
                return(SetResult("Action not found"));
            }

            Action action = _functions.GetFunction(segment.ActionName) as Action;

            if (action == null)
            {
                return(SetResult("Action not found"));
            }

            QueryParameters queryParameters = new QueryParameters(this);

            queryParameters.Count       = null;
            queryParameters.Request     = Request;
            queryParameters.RequestBody = (string)Request.Properties[PostPatchHandler.RequestContent];
            var result = action.Handler(queryParameters, parameters);

            if (action.ReturnType == typeof(void))
            {
                return(Ok());
            }

            if (result == null)
            {
                return(SetResult("Result is null."));
            }

            if (result is DataObject)
            {
                var entityType = _model.GetEdmEntityType(result.GetType());
                return(SetResult(GetEdmObject(entityType, result, 1, null)));
            }

            if (!(result is string) && result is IEnumerable)
            {
                Type type = null;
                if (result.GetType().IsGenericType)
                {
                    Type[] args = result.GetType().GetGenericArguments();
                    if (args.Length == 1)
                    {
                        type = args[0];
                    }
                }

                if (result.GetType().IsArray)
                {
                    type = result.GetType().GetElementType();
                }

                if (type != null && (type.IsSubclassOf(typeof(DataObject)) || type == typeof(DataObject)))
                {
                    var coll = GetEdmCollection((IEnumerable)result, type, 1, null);
                    return(SetResult(coll));
                }
            }

            return(SetResultPrimitive(result.GetType(), result));
        }