/// <summary>
        /// Wrapper methods around metadata provider to get the ResourceSet
        /// </summary>
        /// <param name="metadataProvider">metadata provider</param>
        /// <param name="setName">set name</param>
        /// <returns>a resource set or throws</returns>
        protected ResourceSet GetResourceSet(IDataServiceMetadataProvider metadataProvider, string setName)
        {
            ResourceSet resourceSet = null;

            ProviderImplementationSettings.Override(
                s => s.EnforceMetadataCaching = false,
                () => ExceptionUtilities.Assert(metadataProvider.TryResolveResourceSet(setName, out resourceSet), "Cannot find a resource set '{0}' in the metadata provider", setName));

            return(resourceSet);
        }
예제 #2
0
        /// <summary>
        /// Initializes all service actions in the provider.
        /// </summary>
        /// <param name="operationContext">The operation context instance of the request.</param>
        private void InitializeServiceActions(DataServiceOperationContext operationContext)
        {
            if (operationContext == null)
            {
                throw new DataServiceException("operationContext must not be null!");
            }

            var actionInfos = Interlocked.Exchange(ref this.serviceActionInfos, new Dictionary <string, DSPServiceActionInfo>());

            if (actionInfos.Count > 0)
            {
                IDataServiceMetadataProvider metadataProvider = (IDataServiceMetadataProvider)operationContext.GetService(typeof(IDataServiceMetadataProvider));
                if (metadataProvider == null)
                {
                    throw new DataServiceException("DataServiceOperationContext.GetService(typeof(IDataServiceMetadataProvider)) must return a valid instance of the IDataServiceMetadataProvider.");
                }

                foreach (var entry in actionInfos)
                {
                    DSPServiceActionInfo actionInfo       = entry.Value;
                    DSPActionAttribute   actionAttribute  = actionInfo.ActionAttribute;
                    MethodInfo           actionMethodInfo = actionInfo.Method;

                    ResourceType returnType = DSPActionProvider.GetResourceTypeFromType(metadataProvider, actionInfo.Method.ReturnType, actionAttribute.ReturnElementTypeName);
                    var          parameters = DSPActionProvider.GetServiceActionParameters(metadataProvider, actionAttribute, actionMethodInfo);

                    ServiceAction action;
                    if (!string.IsNullOrEmpty(actionAttribute.ReturnSetPath))
                    {
                        if (actionAttribute.OperationParameterBindingKind != OperationParameterBindingKind.Always && actionAttribute.OperationParameterBindingKind != OperationParameterBindingKind.Sometimes)
                        {
                            throw new DataServiceException("DSPActionAttribute.IsBindable must be true when DSPActionAttribute.ReturnSetPath is not null.");
                        }

                        ResourceSetPathExpression pathExpression = new ResourceSetPathExpression(actionAttribute.ReturnSetPath);
                        action = new ServiceAction(actionMethodInfo.Name, returnType, OperationParameterBindingKind.Sometimes, parameters, pathExpression);
                    }
                    else
                    {
                        ResourceSet returnSet = null;
                        if (!string.IsNullOrEmpty(actionAttribute.ReturnSet))
                        {
                            metadataProvider.TryResolveResourceSet(actionAttribute.ReturnSet, out returnSet);
                        }

                        action = new ServiceAction(actionMethodInfo.Name, returnType, returnSet, actionAttribute.OperationParameterBindingKind, parameters);
                    }

                    action.CustomState = actionInfo;
                    action.SetReadOnly();
                    this.serviceActions.Add(actionMethodInfo.Name, action);
                }
            }
        }
예제 #3
0
        public override IQueryable <EntityType> GetEntitySet <EntityType>(string entitySetName)
        {
            IDataServiceMetadataProvider idsmp = this.CurrentDataSource;
            ResourceSet set;

            if (!idsmp.TryResolveResourceSet(entitySetName, out set))
            {
                throw new DataServiceException("Could not find resource set with name '" + entitySetName + "'");
            }

            IDataServiceQueryProvider idsqp = this.CurrentDataSource;

            return(idsqp.GetQueryRootForResourceSet(set).OfType <EntityType>());
        }
        protected override IEnumerable<ServiceAction> LoadServiceActions(IDataServiceMetadataProvider dataServiceMetadataProvider)
        {
            ResourceType employeeType;
            dataServiceMetadataProvider.TryResolveResourceType("Microsoft.Test.OData.Services.AstoriaDefaultService.Employee", out employeeType);
            ResourceType computerDetailType;
            dataServiceMetadataProvider.TryResolveResourceType("Microsoft.Test.OData.Services.AstoriaDefaultService.ComputerDetail", out computerDetailType);
            ResourceType computerType;          
            dataServiceMetadataProvider.TryResolveResourceType("Microsoft.Test.OData.Services.AstoriaDefaultService.Computer", out computerType);
            ResourceType customerType;
            dataServiceMetadataProvider.TryResolveResourceType("Microsoft.Test.OData.Services.AstoriaDefaultService.Customer", out customerType);
            ResourceType auditInfoType;
            dataServiceMetadataProvider.TryResolveResourceType("Microsoft.Test.OData.Services.AstoriaDefaultService.AuditInfo", out auditInfoType);            
            ResourceSet computerSet;
            dataServiceMetadataProvider.TryResolveResourceSet("Computer", out computerSet);
            var increaseSalaryAction = new ServiceAction(
                 "IncreaseSalaries",
                 null,
                 null,
                 OperationParameterBindingKind.Always,
                 new[]
                {
                    new ServiceActionParameter("employees", ResourceType.GetEntityCollectionResourceType(employeeType)),
                    new ServiceActionParameter("n", ResourceType.GetPrimitiveResourceType(typeof(int))),
                });

            increaseSalaryAction.SetReadOnly();

            yield return increaseSalaryAction;

            var sackEmployeeAction = new ServiceAction(
                "Sack",
                null,
                null,
                OperationParameterBindingKind.Always,
                new[]
                {
                    new ServiceActionParameter("employee", employeeType), 
                });

            sackEmployeeAction.SetReadOnly();

            yield return sackEmployeeAction;

            var getComputerAction = new ServiceAction(
                "GetComputer",
                computerType,
                OperationParameterBindingKind.Always,
                new[]
                {
                    new ServiceActionParameter("computer", computerType)
                },
                new ResourceSetPathExpression("computer"));

            getComputerAction.SetReadOnly();

            yield return getComputerAction;
             
            var changeCustomerAuditInfoAction = new ServiceAction(
               "ChangeCustomerAuditInfo",
               null,
               null,
               OperationParameterBindingKind.Always,
               new[]
                {
                
                    new ServiceActionParameter("customer", customerType), 
                    new ServiceActionParameter("auditInfo", auditInfoType),
                });

            changeCustomerAuditInfoAction.SetReadOnly();

            yield return changeCustomerAuditInfoAction;

             var resetComputerDetailsSpecificationsAction = new ServiceAction(
                "ResetComputerDetailsSpecifications",
                null,
                null,
                OperationParameterBindingKind.Always,
                new[]
                {
                    new ServiceActionParameter("computerDetail", computerDetailType), 
                    new ServiceActionParameter("specifications", ResourceType.GetCollectionResourceType(ResourceType.GetPrimitiveResourceType(typeof(string)))),
                    new ServiceActionParameter("purchaseTime", ResourceType.GetPrimitiveResourceType(typeof(DateTimeOffset)))
                });

            resetComputerDetailsSpecificationsAction.SetReadOnly();

            yield return resetComputerDetailsSpecificationsAction;
        }
        protected override IEnumerable <ServiceAction> LoadServiceActions(IDataServiceMetadataProvider dataServiceMetadataProvider)
        {
            ResourceType employeeType;

            dataServiceMetadataProvider.TryResolveResourceType("Microsoft.Test.OData.Services.AstoriaDefaultService.Employee", out employeeType);
            ResourceType computerDetailType;

            dataServiceMetadataProvider.TryResolveResourceType("Microsoft.Test.OData.Services.AstoriaDefaultService.ComputerDetail", out computerDetailType);
            ResourceType computerType;

            dataServiceMetadataProvider.TryResolveResourceType("Microsoft.Test.OData.Services.AstoriaDefaultService.Computer", out computerType);
            ResourceType customerType;

            dataServiceMetadataProvider.TryResolveResourceType("Microsoft.Test.OData.Services.AstoriaDefaultService.Customer", out customerType);
            ResourceType auditInfoType;

            dataServiceMetadataProvider.TryResolveResourceType("Microsoft.Test.OData.Services.AstoriaDefaultService.AuditInfo", out auditInfoType);
            ResourceSet computerSet;

            dataServiceMetadataProvider.TryResolveResourceSet("Computer", out computerSet);
            var increaseSalaryAction = new ServiceAction(
                "IncreaseSalaries",
                null,
                null,
                OperationParameterBindingKind.Always,
                new[]
            {
                new ServiceActionParameter("employees", ResourceType.GetEntityCollectionResourceType(employeeType)),
                new ServiceActionParameter("n", ResourceType.GetPrimitiveResourceType(typeof(int))),
            });

            increaseSalaryAction.SetReadOnly();

            yield return(increaseSalaryAction);

            var sackEmployeeAction = new ServiceAction(
                "Sack",
                null,
                null,
                OperationParameterBindingKind.Always,
                new[]
            {
                new ServiceActionParameter("employee", employeeType),
            });

            sackEmployeeAction.SetReadOnly();

            yield return(sackEmployeeAction);

            var getComputerAction = new ServiceAction(
                "GetComputer",
                computerType,
                OperationParameterBindingKind.Always,
                new[]
            {
                new ServiceActionParameter("computer", computerType)
            },
                new ResourceSetPathExpression("computer"));

            getComputerAction.SetReadOnly();

            yield return(getComputerAction);

            var changeCustomerAuditInfoAction = new ServiceAction(
                "ChangeCustomerAuditInfo",
                null,
                null,
                OperationParameterBindingKind.Always,
                new[]
            {
                new ServiceActionParameter("customer", customerType),
                new ServiceActionParameter("auditInfo", auditInfoType),
            });

            changeCustomerAuditInfoAction.SetReadOnly();

            yield return(changeCustomerAuditInfoAction);

            var resetComputerDetailsSpecificationsAction = new ServiceAction(
                "ResetComputerDetailsSpecifications",
                null,
                null,
                OperationParameterBindingKind.Always,
                new[]
            {
                new ServiceActionParameter("computerDetail", computerDetailType),
                new ServiceActionParameter("specifications", ResourceType.GetCollectionResourceType(ResourceType.GetPrimitiveResourceType(typeof(string)))),
                new ServiceActionParameter("purchaseTime", ResourceType.GetPrimitiveResourceType(typeof(DateTimeOffset)))
            });

            resetComputerDetailsSpecificationsAction.SetReadOnly();

            yield return(resetComputerDetailsSpecificationsAction);
        }
        /// <summary>
        /// Wrapper methods around metadata provider to get the ResourceSet
        /// </summary>
        /// <param name="metadataProvider">metadata provider</param>
        /// <param name="setName">set name</param>
        /// <returns>a resource set or throws</returns>
        protected ResourceSet GetResourceSet(IDataServiceMetadataProvider metadataProvider, string setName)
        {
            ResourceSet resourceSet = null;

            ProviderImplementationSettings.Override(
               s => s.EnforceMetadataCaching = false,
               () => ExceptionUtilities.Assert(metadataProvider.TryResolveResourceSet(setName, out resourceSet), "Cannot find a resource set '{0}' in the metadata provider", setName));

            return resourceSet;
        }