Ejemplo n.º 1
0
    public object CreateResource(string containerName, string fullTypeName)
    {
        ResourceType type = null;

        if (_metadata.TryResolveResourceType(fullTypeName, out type))
        {
            var context  = GetContext();
            var resource = context.CreateResource(type);
            _actions.Add(() => context.AddResource(type, resource));
            return(resource);
        }
        throw new Exception(string.Format("Type {0} not found", fullTypeName));
    }
        /// <summary>
        /// Wrapper methods around metadata provider to get the ResourceType
        /// </summary>
        /// <param name="metadataProvider">metadata provider</param>
        /// <param name="typeName">type name</param>
        /// <returns>a resource type or throws</returns>
        protected ResourceType GetResourceType(IDataServiceMetadataProvider metadataProvider, string typeName)
        {
            ResourceType resourceType = null;

            ProviderImplementationSettings.Override(
                s => s.EnforceMetadataCaching = false,
                () => ExceptionUtilities.Assert(metadataProvider.TryResolveResourceType(typeName, out resourceType), "Cannot find a resource type '{0}' in the metadata provider", typeName));

            return(resourceType);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Get the resource type for the given clr type.
        /// </summary>
        /// <param name="metadataProvider">An instance of the IDataServiceMetadataProvider.</param>
        /// <param name="type">Clr type in question.</param>
        /// <param name="elementTypeName">Name of the element type if <paramref name="type"/> is a
        /// <see cref="DSPResource"/> type or a collection type of <see cref="DSPResource"/>.</param>
        /// <returns>The resource type for the given clr type.</returns>
        private static ResourceType GetResourceTypeFromType(IDataServiceMetadataProvider metadataProvider, Type type, string elementTypeName)
        {
            Debug.Assert(metadataProvider != null, "metadataProvider != null");
            if (type == typeof(void))
            {
                return(null);
            }

            ResourceType resourceType;
            Type         elementType = type;

            if (!type.IsPrimitive && type.IsGenericType && typeof(IEnumerable).IsAssignableFrom(type))
            {
                elementType = type.GetGenericArguments()[0];
            }

            if (elementType == typeof(DSPResource))
            {
                metadataProvider.TryResolveResourceType(elementTypeName, out resourceType);
            }
            else
            {
                resourceType = ResourceType.GetPrimitiveResourceType(elementType);
                if (resourceType == null && !metadataProvider.TryResolveResourceType(elementType.FullName, out resourceType))
                {
                    throw new DataServiceException(string.Format("The type '{0}' is unknown to the metadata provider.", elementType.FullName));
                }
            }

            if (type != elementType)
            {
                if (resourceType.ResourceTypeKind == ResourceTypeKind.EntityType)
                {
                    resourceType = ResourceType.GetEntityCollectionResourceType(resourceType);
                }
                else
                {
                    resourceType = ResourceType.GetCollectionResourceType(resourceType);
                }
            }

            return(resourceType);
        }
            public bool TryResolveServiceAction(DataServiceOperationContext operationContext, string serviceActionName, out ServiceAction serviceAction)
            {
                if (serviceActionName == "Action")
                {
                    IDataServiceMetadataProvider metadataProvider = (IDataServiceMetadataProvider)operationContext.GetService(typeof(IDataServiceMetadataProvider));
                    ResourceType resourceType;
                    metadataProvider.TryResolveResourceType(EntityTypeNameWithStringKey, out resourceType);
                    Assert.IsNotNull(resourceType);
                    serviceAction = new ServiceAction("Action", ResourceType.GetPrimitiveResourceType(typeof(string)), OperationParameterBindingKind.Always, new[] { new ServiceActionParameter("param1", ResourceType.GetEntityCollectionResourceType(resourceType)) }, null);
                    serviceAction.SetReadOnly();
                    return(true);
                }

                serviceAction = null;
                return(false);
            }
        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;
        }
Ejemplo n.º 6
0
        protected override IEnumerable <ServiceAction> LoadServiceActions(IDataServiceMetadataProvider dataServiceMetadataProvider)
        {
            ResourceType productType;

            dataServiceMetadataProvider.TryResolveResourceType("Microsoft.Test.OData.Services.AstoriaDefaultService.Product", out productType);

            ResourceType orderLineType;

            dataServiceMetadataProvider.TryResolveResourceType("Microsoft.Test.OData.Services.AstoriaDefaultService.OrderLine", out orderLineType);

            ResourceType personType;

            dataServiceMetadataProvider.TryResolveResourceType("Microsoft.Test.OData.Services.AstoriaDefaultService.Person", out personType);

            ResourceType employeeType;

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

            ResourceType specialEmployeeType;

            dataServiceMetadataProvider.TryResolveResourceType("Microsoft.Test.OData.Services.AstoriaDefaultService.SpecialEmployee", out specialEmployeeType);

            ResourceType contractorType;

            dataServiceMetadataProvider.TryResolveResourceType("Microsoft.Test.OData.Services.AstoriaDefaultService.Contractor", out contractorType);

            //
            // actions with the same name and non-related binding types
            //
            var RetrieveProductActionProduct = new ServiceAction(
                "RetrieveProduct",
                ResourceType.GetPrimitiveResourceType(typeof(int)),
                null,
                OperationParameterBindingKind.Sometimes,
                new[]
            {
                new ServiceActionParameter("product", productType),
            });

            RetrieveProductActionProduct.SetReadOnly();
            yield return(RetrieveProductActionProduct);

            var RetrieveProductActionOrderLine = new ServiceAction(
                "RetrieveProduct",
                ResourceType.GetPrimitiveResourceType(typeof(int)),
                null,
                OperationParameterBindingKind.Sometimes,
                new[]
            {
                new ServiceActionParameter("orderLine", orderLineType),
            });

            RetrieveProductActionOrderLine.SetReadOnly();
            yield return(RetrieveProductActionOrderLine);

            //
            // Collection bound actions with the same name
            //
            var increaseSalariesActionEmployee = new ServiceAction(
                "IncreaseSalaries",
                null,
                null,
                OperationParameterBindingKind.Sometimes,
                new[]
            {
                new ServiceActionParameter("employees", ResourceType.GetEntityCollectionResourceType(employeeType)),
                new ServiceActionParameter("n", ResourceType.GetPrimitiveResourceType(typeof(int))),
            });

            increaseSalariesActionEmployee.SetReadOnly();
            yield return(increaseSalariesActionEmployee);

            var increaseSalariesActionSpecialEmployee = new ServiceAction(
                "IncreaseSalaries",
                null,
                null,
                OperationParameterBindingKind.Sometimes,
                new[]
            {
                new ServiceActionParameter("specialEmployees", ResourceType.GetEntityCollectionResourceType(specialEmployeeType)),
                new ServiceActionParameter("n", ResourceType.GetPrimitiveResourceType(typeof(int))),
            });

            increaseSalariesActionSpecialEmployee.SetReadOnly();
            yield return(increaseSalariesActionSpecialEmployee);

            //
            // Actions with the same name and base/derived type binding parameters
            //
            var updatePersonInfoAction = new ServiceAction(
                "UpdatePersonInfo",
                null,
                null,
                OperationParameterBindingKind.Never,
                null);

            updatePersonInfoAction.SetReadOnly();
            yield return(updatePersonInfoAction);

            var updatePersonInfoActionPerson = new ServiceAction(
                "UpdatePersonInfo",
                null,
                null,
                OperationParameterBindingKind.Sometimes,
                new[]
            {
                new ServiceActionParameter("person", personType),
            });

            updatePersonInfoActionPerson.SetReadOnly();
            yield return(updatePersonInfoActionPerson);

            var updatePersonInfoActionEmployee = new ServiceAction(
                "UpdatePersonInfo",
                null,
                null,
                OperationParameterBindingKind.Sometimes,
                new[]
            {
                new ServiceActionParameter("employee", employeeType),
            });

            updatePersonInfoActionEmployee.SetReadOnly();
            yield return(updatePersonInfoActionEmployee);

            var updatePersonInfoActionSpecialEmployee = new ServiceAction(
                "UpdatePersonInfo",
                null,
                null,
                OperationParameterBindingKind.Sometimes,
                new[]
            {
                new ServiceActionParameter("specialEmployee", specialEmployeeType),
            });

            updatePersonInfoActionSpecialEmployee.SetReadOnly();
            yield return(updatePersonInfoActionSpecialEmployee);

            var updatePersonInfoActionContractor = new ServiceAction(
                "UpdatePersonInfo",
                null,
                null,
                OperationParameterBindingKind.Always,
                new[]
            {
                new ServiceActionParameter("contractor", contractorType),
            });

            updatePersonInfoActionContractor.SetReadOnly();
            yield return(updatePersonInfoActionContractor);

            //
            // Actions with the same name, base/derived type binding parameters, different non-binding parameters
            //
            var increaseEmployeeSalaryActionEmployee = new ServiceAction(
                "IncreaseEmployeeSalary",
                ResourceType.GetPrimitiveResourceType(typeof(bool)),
                null,
                OperationParameterBindingKind.Sometimes,
                new[]
            {
                new ServiceActionParameter("employee", employeeType),
                new ServiceActionParameter("n", ResourceType.GetPrimitiveResourceType(typeof(int))),
            });

            increaseEmployeeSalaryActionEmployee.SetReadOnly();
            yield return(increaseEmployeeSalaryActionEmployee);

            var increaseEmployeeSalaryActionSpecialEmployee = new ServiceAction(
                "IncreaseEmployeeSalary",
                ResourceType.GetPrimitiveResourceType(typeof(int)),
                null,
                OperationParameterBindingKind.Sometimes,
                new[]
            {
                new ServiceActionParameter("specialEmployee", specialEmployeeType),
            });

            increaseEmployeeSalaryActionSpecialEmployee.SetReadOnly();
            yield return(increaseEmployeeSalaryActionSpecialEmployee);
        }
        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 ResourceType
        /// </summary>
        /// <param name="metadataProvider">metadata provider</param>
        /// <param name="typeName">type name</param>
        /// <returns>a resource type or throws</returns>
        protected ResourceType GetResourceType(IDataServiceMetadataProvider metadataProvider, string typeName)
        {
            ResourceType resourceType = null;

            ProviderImplementationSettings.Override(
                s => s.EnforceMetadataCaching = false,
                () => ExceptionUtilities.Assert(metadataProvider.TryResolveResourceType(typeName, out resourceType), "Cannot find a resource type '{0}' in the metadata provider", typeName));

            return resourceType;
        }
        protected override IEnumerable<ServiceAction> LoadServiceActions(IDataServiceMetadataProvider dataServiceMetadataProvider)
        {
            ResourceType productType;
            dataServiceMetadataProvider.TryResolveResourceType("Microsoft.Test.OData.Services.AstoriaDefaultService.Product", out productType);

            ResourceType orderLineType;
            dataServiceMetadataProvider.TryResolveResourceType("Microsoft.Test.OData.Services.AstoriaDefaultService.OrderLine", out orderLineType);

            ResourceType personType;
            dataServiceMetadataProvider.TryResolveResourceType("Microsoft.Test.OData.Services.AstoriaDefaultService.Person", out personType);

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

            ResourceType specialEmployeeType;
            dataServiceMetadataProvider.TryResolveResourceType("Microsoft.Test.OData.Services.AstoriaDefaultService.SpecialEmployee", out specialEmployeeType);

            ResourceType contractorType;
            dataServiceMetadataProvider.TryResolveResourceType("Microsoft.Test.OData.Services.AstoriaDefaultService.Contractor", out contractorType);

            //
            // actions with the same name and non-related binding types
            //
            var RetrieveProductActionProduct = new ServiceAction(
                "RetrieveProduct",
                ResourceType.GetPrimitiveResourceType(typeof(int)),
                null,
                OperationParameterBindingKind.Sometimes,
                new[]
                {
                    new ServiceActionParameter("product", productType), 
                });
            RetrieveProductActionProduct.SetReadOnly();
            yield return RetrieveProductActionProduct;

            var RetrieveProductActionOrderLine = new ServiceAction(
                "RetrieveProduct",
                ResourceType.GetPrimitiveResourceType(typeof(int)),
                null,
                OperationParameterBindingKind.Sometimes,
                new[]
                {
                    new ServiceActionParameter("orderLine", orderLineType), 
                });
            RetrieveProductActionOrderLine.SetReadOnly();
            yield return RetrieveProductActionOrderLine;

            //
            // Collection bound actions with the same name
            //
            var increaseSalariesActionEmployee = new ServiceAction(
                "IncreaseSalaries",
                null,
                null,
                OperationParameterBindingKind.Sometimes,
                new[]
                {
                    new ServiceActionParameter("employees", ResourceType.GetEntityCollectionResourceType(employeeType)),
                    new ServiceActionParameter("n", ResourceType.GetPrimitiveResourceType(typeof(int))),
                });
            increaseSalariesActionEmployee.SetReadOnly();
            yield return increaseSalariesActionEmployee;

            var increaseSalariesActionSpecialEmployee = new ServiceAction(
                "IncreaseSalaries",
                null,
                null,
                OperationParameterBindingKind.Sometimes,
                new[]
                {
                    new ServiceActionParameter("specialEmployees", ResourceType.GetEntityCollectionResourceType(specialEmployeeType)),
                    new ServiceActionParameter("n", ResourceType.GetPrimitiveResourceType(typeof(int))),
                });
            increaseSalariesActionSpecialEmployee.SetReadOnly();
            yield return increaseSalariesActionSpecialEmployee;

            //
            // Actions with the same name and base/derived type binding parameters
            //
            var updatePersonInfoAction = new ServiceAction(
                "UpdatePersonInfo",
                null,
                null,
                OperationParameterBindingKind.Never,
                null);
            updatePersonInfoAction.SetReadOnly();
            yield return updatePersonInfoAction;

            var updatePersonInfoActionPerson = new ServiceAction(
                "UpdatePersonInfo",
                null,
                null,
                OperationParameterBindingKind.Sometimes,
                new[]
                {
                    new ServiceActionParameter("person", personType), 
                });
            updatePersonInfoActionPerson.SetReadOnly();
            yield return updatePersonInfoActionPerson;

            var updatePersonInfoActionEmployee = new ServiceAction(
                "UpdatePersonInfo",
                null,
                null,
                OperationParameterBindingKind.Sometimes,
                new[]
                {
                    new ServiceActionParameter("employee", employeeType), 
                });
            updatePersonInfoActionEmployee.SetReadOnly();
            yield return updatePersonInfoActionEmployee;

            var updatePersonInfoActionSpecialEmployee = new ServiceAction(
                "UpdatePersonInfo",
                null,
                null,
                OperationParameterBindingKind.Sometimes,
                new[]
                {
                    new ServiceActionParameter("specialEmployee", specialEmployeeType), 
                });
            updatePersonInfoActionSpecialEmployee.SetReadOnly();
            yield return updatePersonInfoActionSpecialEmployee;

            var updatePersonInfoActionContractor = new ServiceAction(
                "UpdatePersonInfo",
                null,
                null,
                OperationParameterBindingKind.Always,
                new[]
                {
                    new ServiceActionParameter("contractor", contractorType), 
                });
            updatePersonInfoActionContractor.SetReadOnly();
            yield return updatePersonInfoActionContractor;

            //
            // Actions with the same name, base/derived type binding parameters, different non-binding parameters
            //
            var increaseEmployeeSalaryActionEmployee = new ServiceAction(
                "IncreaseEmployeeSalary",
                ResourceType.GetPrimitiveResourceType(typeof(bool)),
                null,
                OperationParameterBindingKind.Sometimes,
                new[]
                {
                    new ServiceActionParameter("employee", employeeType),
                    new ServiceActionParameter("n", ResourceType.GetPrimitiveResourceType(typeof(int))),
                });
            increaseEmployeeSalaryActionEmployee.SetReadOnly();
            yield return increaseEmployeeSalaryActionEmployee;

            var increaseEmployeeSalaryActionSpecialEmployee = new ServiceAction(
                "IncreaseEmployeeSalary",
                ResourceType.GetPrimitiveResourceType(typeof(int)),
                null,
                OperationParameterBindingKind.Sometimes,
                new[]
                {
                    new ServiceActionParameter("specialEmployee", specialEmployeeType),
                });
            increaseEmployeeSalaryActionSpecialEmployee.SetReadOnly();
            yield return increaseEmployeeSalaryActionSpecialEmployee;
        }