Пример #1
0
        /// <summary>
        /// Sets the binding resource type for the current path expression.
        /// </summary>
        /// <param name="parameter">The resource type this path expression will bind to.</param>
        internal void SetBindingParameter(OperationParameter parameter)
        {
            Debug.Assert(parameter != null, "parameter != null");
            Debug.Assert(
                parameter.ParameterType.ResourceTypeKind == ResourceTypeKind.EntityType || parameter.ParameterType.ResourceTypeKind == ResourceTypeKind.EntityCollection,
                "parameter.ParameterType.ResourceTypeKind == ResourceTypeKind.EntityType || parameter.ParameterType.ResourceTypeKind == ResourceTypeKind.EntityCollection");

            if (this.PathExpression != parameter.Name && !this.PathExpression.StartsWith(parameter.Name + ResourceSetPathExpression.PathSeparator, StringComparison.Ordinal))
            {
                throw new InvalidOperationException(Strings.ResourceSetPathExpression_PathExpressionMustStartWithBindingParameterName(this.PathExpression, parameter.Name));
            }

            this.bindingParameter = parameter;
        }
        /// <summary>
        /// Sets the binding resource type for the current path expression.
        /// </summary>
        /// <param name="parameter">The resource type this path expression will bind to.</param>
        internal void SetBindingParameter(OperationParameter parameter)
        {
            Debug.Assert(parameter != null, "parameter != null");
            Debug.Assert(
                parameter.ParameterType.ResourceTypeKind == ResourceTypeKind.EntityType || parameter.ParameterType.ResourceTypeKind == ResourceTypeKind.EntityCollection,
                "parameter.ParameterType.ResourceTypeKind == ResourceTypeKind.EntityType || parameter.ParameterType.ResourceTypeKind == ResourceTypeKind.EntityCollection");

            if (this.PathExpression != parameter.Name && !this.PathExpression.StartsWith(parameter.Name + ResourceSetPathExpression.PathSeparator, StringComparison.Ordinal))
            {
                throw new InvalidOperationException(Strings.ResourceSetPathExpression_PathExpressionMustStartWithBindingParameterName(this.PathExpression, parameter.Name));
            }

            this.bindingParameter = parameter;
        }
Пример #3
0
        /// <summary>
        /// Validates the input parameters and convert it to a read only collection of parameters.
        /// </summary>
        /// <param name="operationParameterBindingKind">the kind of the operation parameter binding (Never, Sometimes, Always).</param>
        /// <param name="parameters">In-order parameters for this operation.</param>
        /// <returns>A read only collection of parameters.</returns>
        private static ReadOnlyCollection <OperationParameter> ValidateParameters(OperationParameterBindingKind operationParameterBindingKind, IEnumerable <OperationParameter> parameters)
        {
            Debug.Assert(
                operationParameterBindingKind == OperationParameterBindingKind.Never ||
                operationParameterBindingKind == OperationParameterBindingKind.Always ||
                operationParameterBindingKind == OperationParameterBindingKind.Sometimes,
                "Unexpected value of OperationParameterBindingKind.");

            ReadOnlyCollection <OperationParameter> resultParameters;

            if (parameters == null)
            {
                resultParameters = OperationParameter.EmptyOperationParameterCollection;
            }
            else
            {
                resultParameters = new ReadOnlyCollection <OperationParameter>(new List <OperationParameter>(parameters));
                HashSet <string> paramNames = new HashSet <string>(StringComparer.Ordinal);

                int bindingParameterIndex = operationParameterBindingKind != OperationParameterBindingKind.Never ? 0 : -1;
                for (int idx = 0; idx < resultParameters.Count; idx++)
                {
                    OperationParameter parameter = resultParameters[idx];
                    if (!paramNames.Add(parameter.Name))
                    {
                        throw new ArgumentException(Strings.ServiceOperation_DuplicateParameterName(parameter.Name), "parameters");
                    }

                    if (idx > bindingParameterIndex)
                    {
                        ResourceTypeKind parameterTypeKind = parameter.ParameterType.ResourceTypeKind;
                        if (parameterTypeKind == ResourceTypeKind.EntityType || parameterTypeKind == ResourceTypeKind.EntityCollection)
                        {
                            throw new ArgumentException(Strings.ServiceOperation_NonBindingParametersCannotBeEntityorEntityCollection(parameter.Name, parameterTypeKind));
                        }
                    }
                }
            }

            return(resultParameters);
        }
Пример #4
0
        /// <summary>
        /// Initializes a new <see cref="Operation"/> instance.
        /// </summary>
        /// <param name="name">name of the operation.</param>
        /// <param name="resultKind">Kind of result expected from this operation.</param>
        /// <param name="returnType">Return type of the operation.</param>
        /// <param name="resultSet">EntitySet of the result expected from this operation, must be null if <paramref name="resultSetPathExpression"/> is not null.</param>
        /// <param name="resultSetPathExpression">Path expression to calculate the result set of the operation, must be null if <paramref name="resultSet"/> is not null.</param>
        /// <param name="method">Protocol (for example HTTP) method the service operation responds to.</param>
        /// <param name="parameters">In-order parameters for this operation.</param>
        /// <param name="operationParameterBindingKind">the kind of the operation parameter binding (Never, Sometimes, Always).</param>
        /// <param name="kind">The kind of the current service operation.</param>
        internal Operation(
            string name,
            ServiceOperationResultKind resultKind,
            ResourceType returnType,
            ResourceSet resultSet,
            ResourceSetPathExpression resultSetPathExpression,
            string method,
            IEnumerable <OperationParameter> parameters,
            OperationParameterBindingKind operationParameterBindingKind,
            OperationKind kind)
        {
            WebUtil.CheckStringArgumentNullOrEmpty(name, "name");
            WebUtil.CheckServiceOperationResultKind(resultKind, "resultKind");
            WebUtil.CheckStringArgumentNullOrEmpty(method, "method");

            Debug.Assert(
                this.GetType() == typeof(ServiceOperation) && kind == OperationKind.ServiceOperation ||
                this.GetType() == typeof(ServiceAction) && kind == OperationKind.Action,
                "OperationKind and the current type doesn't match.");

            ValidateConstructorArguments(name, returnType, resultSet, resultSetPathExpression, method, operationParameterBindingKind, kind);
            this.name                          = name;
            this.resultKind                    = resultKind;
            this.returnType                    = returnType;
            this.resourceSet                   = resultSet;
            this.resultSetPathExpression       = resultSetPathExpression;
            this.method                        = method;
            this.kind                          = kind;
            this.operationParameterBindingKind = operationParameterBindingKind;
            this.operationParameters           = Operation.ValidateParameters(this.operationParameterBindingKind, parameters);

            if (this.operationParameterBindingKind != OperationParameterBindingKind.Never)
            {
                Debug.Assert(
                    this.operationParameterBindingKind == OperationParameterBindingKind.Always || this.operationParameterBindingKind == OperationParameterBindingKind.Sometimes,
                    "Value of operationParameterBindingKind was expected to be 'always' or 'sometimes'.");

                Debug.Assert(this.kind != OperationKind.ServiceOperation, "ServiceOperations should never be bindable.");
                this.bindingParameter = this.operationParameters.FirstOrDefault();
                if (this.bindingParameter == null)
                {
                    throw new ArgumentException(Strings.ServiceOperation_BindableOperationMustHaveAtLeastOneParameter, "operationParameterBindingKind");
                }

                if (resourceSet != null)
                {
                    throw new ArgumentException(Strings.Opereration_BoundOperationsMustNotSpecifyEntitySetOnlyEntitySetPath(this.name), "resourceSet");
                }

                if (resultSetPathExpression != null &&
                    this.bindingParameter.ParameterType.ResourceTypeKind != ResourceTypeKind.EntityType &&
                    this.bindingParameter.ParameterType.ResourceTypeKind != ResourceTypeKind.EntityCollection)
                {
                    throw new ArgumentException(Strings.ServiceOperation_BindingParameterMustBeEntityToUsePathExpression("resultSetPathExpression"));
                }

                if (this.kind == OperationKind.Action &&
                    !(this.bindingParameter.ParameterType.ResourceTypeKind == ResourceTypeKind.EntityType ||
                      this.bindingParameter.ParameterType.ResourceTypeKind == ResourceTypeKind.EntityCollection))
                {
                    throw new ArgumentException(Strings.ServiceOperation_ActionBindingMustBeEntityOrEntityCollection, "parameters");
                }

                if (this.resultSetPathExpression != null)
                {
                    this.resultSetPathExpression.SetBindingParameter(this.bindingParameter);
                }
            }

            Debug.Assert(this.kind != OperationKind.Action || string.CompareOrdinal(XmlConstants.HttpMethodPost, this.method) == 0, "HttpMethod must be POST for Actions.");
            Debug.Assert(this.resourceSet == null || this.resultSetPathExpression == null, "'resultSet' and 'resultSetPathExpression' cannot be both set by the constructor.");
        }
Пример #5
0
        /// <summary>
        /// Initializes a new <see cref="Operation"/> instance.
        /// </summary>
        /// <param name="name">name of the operation.</param>
        /// <param name="resultKind">Kind of result expected from this operation.</param>
        /// <param name="returnType">Return type of the operation.</param>
        /// <param name="resultSet">EntitySet of the result expected from this operation, must be null if <paramref name="resultSetPathExpression"/> is not null.</param>
        /// <param name="resultSetPathExpression">Path expression to calculate the result set of the operation, must be null if <paramref name="resultSet"/> is not null.</param>
        /// <param name="method">Protocol (for example HTTP) method the service operation responds to.</param>
        /// <param name="parameters">In-order parameters for this operation.</param>
        /// <param name="operationParameterBindingKind">the kind of the operation parameter binding (Never, Sometimes, Always).</param>
        /// <param name="kind">The kind of the current service operation.</param>
        internal Operation(
            string name,
            ServiceOperationResultKind resultKind,
            ResourceType returnType,
            ResourceSet resultSet,
            ResourceSetPathExpression resultSetPathExpression,
            string method,
            IEnumerable<OperationParameter> parameters,
            OperationParameterBindingKind operationParameterBindingKind,
            OperationKind kind)
        {
            WebUtil.CheckStringArgumentNullOrEmpty(name, "name");
            WebUtil.CheckServiceOperationResultKind(resultKind, "resultKind");
            WebUtil.CheckStringArgumentNullOrEmpty(method, "method");

            Debug.Assert(
                this.GetType() == typeof(ServiceOperation) && kind == OperationKind.ServiceOperation ||
                this.GetType() == typeof(ServiceAction) && kind == OperationKind.Action,
                "OperationKind and the current type doesn't match.");

            ValidateConstructorArguments(name, returnType, resultSet, resultSetPathExpression, method, operationParameterBindingKind, kind);
            this.name = name;
            this.resultKind = resultKind;
            this.returnType = returnType;
            this.resourceSet = resultSet;
            this.resultSetPathExpression = resultSetPathExpression;
            this.method = method;
            this.kind = kind;
            this.operationParameterBindingKind = operationParameterBindingKind;
            this.operationParameters = Operation.ValidateParameters(this.operationParameterBindingKind, parameters);

            if (this.operationParameterBindingKind != OperationParameterBindingKind.Never)
            {
                Debug.Assert(
                    this.operationParameterBindingKind == OperationParameterBindingKind.Always || this.operationParameterBindingKind == OperationParameterBindingKind.Sometimes,
                    "Value of operationParameterBindingKind was expected to be 'always' or 'sometimes'.");

                Debug.Assert(this.kind != OperationKind.ServiceOperation, "ServiceOperations should never be bindable.");
                this.bindingParameter = this.operationParameters.FirstOrDefault();
                if (this.bindingParameter == null)
                {
                    throw new ArgumentException(Strings.ServiceOperation_BindableOperationMustHaveAtLeastOneParameter, "operationParameterBindingKind");
                }

                if (resourceSet != null)
                {
                    throw new ArgumentException(Strings.Opereration_BoundOperationsMustNotSpecifyEntitySetOnlyEntitySetPath(this.name), "resourceSet");
                }

                if (resultSetPathExpression != null &&
                    this.bindingParameter.ParameterType.ResourceTypeKind != ResourceTypeKind.EntityType &&
                    this.bindingParameter.ParameterType.ResourceTypeKind != ResourceTypeKind.EntityCollection)
                {
                    throw new ArgumentException(Strings.ServiceOperation_BindingParameterMustBeEntityToUsePathExpression("resultSetPathExpression"));
                }

                if (this.kind == OperationKind.Action &&
                    !(this.bindingParameter.ParameterType.ResourceTypeKind == ResourceTypeKind.EntityType ||
                    this.bindingParameter.ParameterType.ResourceTypeKind == ResourceTypeKind.EntityCollection))
                {
                    throw new ArgumentException(Strings.ServiceOperation_ActionBindingMustBeEntityOrEntityCollection, "parameters");
                }

                if (this.resultSetPathExpression != null)
                {
                    this.resultSetPathExpression.SetBindingParameter(this.bindingParameter);
                }
            }

            Debug.Assert(this.kind != OperationKind.Action || string.CompareOrdinal(XmlConstants.HttpMethodPost, this.method) == 0, "HttpMethod must be POST for Actions.");
            Debug.Assert(this.resourceSet == null || this.resultSetPathExpression == null, "'resultSet' and 'resultSetPathExpression' cannot be both set by the constructor.");
        }