Beispiel #1
0
        /// <summary>
        /// Try to bind the idenfier as bound operation segment,
        /// Append it into path.
        /// </summary>
        internal static bool TryBindOperations(string identifier, string parenthesisExpressions,
                                               IEdmModel model, IList <PathSegment> path, PathParserSettings settings)
        {
            PathSegment preSegment = path.LastOrDefault();

            if (preSegment == null)
            {
                // bound operation cannot be the first segment.
                return(false);
            }

            IEdmType bindingType = preSegment.EdmType;

            // operation
            parenthesisExpressions.ExtractKeyValuePairs(out IDictionary <string, string> parameters, out string remaining);
            IList <string> parameterNames = parameters == null ? null : parameters.Keys.ToList();

            IEdmOperation operation = OperationHelper.ResolveOperations(identifier, parameterNames, bindingType, model, settings.EnableCaseInsensitive);

            if (operation != null)
            {
                IEdmEntitySetBase targetset = null;
                if (operation.ReturnType != null)
                {
                    IEdmNavigationSource source = preSegment == null ? null : preSegment.NavigationSource;
                    targetset = operation.GetTargetEntitySet(source, model);
                }

                path.Add(new OperationSegment(operation, targetset));

                if (remaining != null && operation.IsFunction())
                {
                    IEdmFunction function = (IEdmFunction)operation;
                    if (function.IsComposable)
                    {
                        // to process the ~/ .../ NS.Function(p1 ={ abc})({ id})
                        if (TryBindKeySegment(parenthesisExpressions, path))
                        {
                            return(true);
                        }
                    }
                }

                return(true);
            }

            return(false);
        }
Beispiel #2
0
        /// <summary>
        /// Add the template to the action
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="edmOperation">The Edm operation.</param>
        /// <param name="hasKeyParameter">Has key parameter or not.</param>
        /// <param name="entityType">The entity type.</param>
        /// <param name="navigationSource">The navigation source.</param>
        /// <param name="castType">The type cast.</param>
        protected static void AddSelector(ODataControllerActionContext context,
                                          IEdmOperation edmOperation,
                                          bool hasKeyParameter,
                                          IEdmEntityType entityType,
                                          IEdmNavigationSource navigationSource,
                                          IEdmEntityType castType)
        {
            Contract.Assert(context != null);
            Contract.Assert(entityType != null);
            Contract.Assert(navigationSource != null);
            Contract.Assert(edmOperation != null);

            // Now, let's add the selector model.
            IList <ODataSegmentTemplate> segments = new List <ODataSegmentTemplate>();

            if (context.EntitySet != null)
            {
                segments.Add(new EntitySetSegmentTemplate(context.EntitySet));
                if (hasKeyParameter)
                {
                    segments.Add(new KeySegmentTemplate(entityType, navigationSource));
                }
            }
            else
            {
                segments.Add(new SingletonSegmentTemplate(context.Singleton));
            }

            if (castType != null)
            {
                if (context.Singleton != null || !hasKeyParameter)
                {
                    segments.Add(new CastSegmentTemplate(castType, entityType, navigationSource));
                }
                else
                {
                    segments.Add(new CastSegmentTemplate(new EdmCollectionType(castType.ToEdmTypeReference(false)),
                                                         new EdmCollectionType(entityType.ToEdmTypeReference(false)), navigationSource));
                }
            }

            IEdmNavigationSource targetset = null;

            if (edmOperation.ReturnType != null)
            {
                targetset = edmOperation.GetTargetEntitySet(navigationSource, context.Model);
            }

            string httpMethod;

            if (edmOperation.IsAction())
            {
                segments.Add(new ActionSegmentTemplate((IEdmAction)edmOperation, targetset));
                httpMethod = "post";
            }
            else
            {
                ISet <String> required = GetRequiredFunctionParamters(edmOperation, context.Action);
                segments.Add(new FunctionSegmentTemplate((IEdmFunction)edmOperation, targetset, required));
                httpMethod = "get";
            }

            ODataPathTemplate template = new ODataPathTemplate(segments);

            context.Action.AddSelector(httpMethod, context.Prefix, context.Model, template);
        }