public Task ExtendModelAsync(
            ModelContext context,
            CancellationToken cancellationToken)
        {
            var model = context.Model;
            var entityContainer = model.EntityContainer as EdmEntityContainer;

            foreach (ActionMethodInfo actionInfo in this.ActionInfos)
            {
                var returnTypeReference = ConventionalActionProvider.GetReturnTypeReference(actionInfo.Method.ReturnType);
                var action = new EdmAction(entityContainer.Namespace, actionInfo.ActionName, returnTypeReference);

                foreach (ParameterInfo parameter in actionInfo.Method.GetParameters())
                {
                    EdmOperationParameter actionParam = new EdmOperationParameter(
                        action,
                        parameter.Name,
                        ConventionalActionProvider.GetTypeReference(parameter.ParameterType));

                    action.AddParameter(actionParam);
                }

                model.AddElement(action);

                if (!action.IsBound)
                {
                    EdmActionImport actionImport = new EdmActionImport(entityContainer, action.Name, action);
                    entityContainer.AddElement(actionImport);
                }
            }
            return Task.FromResult<object>(null);
        }
Beispiel #2
0
        /// <summary>
        /// Creates and adds a parameter to this function (as the last parameter).
        /// </summary>
        /// <param name="name">The name of the parameter being added.</param>
        /// <param name="type">The type of the parameter being added.</param>
        /// <returns>Created parameter.</returns>
        public EdmOperationParameter AddParameter(string name, IEdmTypeReference type)
        {
            EdmOperationParameter parameter = new EdmOperationParameter(this, name, type);

            this.parameters.Add(parameter);
            return(parameter);
        }
        private static void BuildOperationParameters(EdmOperation operation, MethodInfo method, IEdmModel model)
        {
            foreach (ParameterInfo parameter in method.GetParameters())
            {
                var parameterTypeReference = GetTypeReference(parameter.ParameterType, model);
                var operationParam = new EdmOperationParameter(
                    operation,
                    parameter.Name,
                    parameterTypeReference);

                operation.AddParameter(operationParam);
            }
        }
 private static void AddProcedureParameters(EdmOperation operation, ProcedureConfiguration procedure, Dictionary<Type, IEdmType> edmTypeMap)
 {
     foreach (ParameterConfiguration parameter in procedure.Parameters)
     {
         bool isParameterOptional = parameter.OptionalParameter;
         IEdmTypeReference parameterTypeReference = GetEdmTypeReference(edmTypeMap, parameter.TypeConfiguration, nullable: isParameterOptional);
         IEdmOperationParameter operationParameter = new EdmOperationParameter(operation, parameter.Name, parameterTypeReference);
         operation.AddParameter(operationParameter);
     }
 }
 private static void AddProcedureParameters(EdmOperation operation, ProcedureConfiguration procedure, Dictionary<Type, IEdmType> edmTypeMap)
 {
     foreach (ParameterConfiguration parameter in procedure.Parameters)
     {
         // TODO: http://aspnetwebstack.codeplex.com/workitem/417
         bool isParameterOptional = EdmLibHelpers.IsNullable(parameter.TypeConfiguration.ClrType);
         IEdmTypeReference parameterTypeReference = GetEdmTypeReference(edmTypeMap, parameter.TypeConfiguration, nullable: isParameterOptional);
         IEdmOperationParameter operationParameter = new EdmOperationParameter(operation, parameter.Name, parameterTypeReference);
         operation.AddParameter(operationParameter);
     }
 }
        public void ConstructibleModelODataTestModelWithFunctionImport()
        {
            EdmModel model = new EdmModel();

            EdmComplexType complexType = new EdmComplexType("TestModel", "ComplexType");
            EdmStructuralProperty primitiveProperty = complexType.AddStructuralProperty("PrimitiveProperty", EdmCoreModel.Instance.GetString(false));
            EdmStructuralProperty complexProperty = complexType.AddStructuralProperty("ComplexProperty", new EdmComplexTypeReference(complexType, false));
            model.AddElement(complexType);

            EdmEntityType entity = new EdmEntityType("TestNS", "EntityType");
            EdmStructuralProperty entityId = entity.AddStructuralProperty("ID", EdmCoreModel.Instance.GetInt32(false));
            entity.AddKeys(entityId);
            EdmStructuralProperty entityComplexProperty = entity.AddStructuralProperty("ComplexProperty", new EdmComplexTypeReference(complexType, false));
            model.AddElement(entity);

            EdmEnumType enumType = new EdmEnumType("TestNS", "EnumType");
            model.AddElement(enumType);

            EdmEntityContainer container = new EdmEntityContainer("TestNS", "TestContainer");
            EdmAction primitiveOperationAction = new EdmAction("TestNS", "FunctionImport_Primitive", null);
            EdmOperationParameter primitiveParameter = new EdmOperationParameter(primitiveOperationAction, "primitive", EdmCoreModel.Instance.GetString(true));
            primitiveOperationAction.AddParameter(primitiveParameter);
            model.AddElement(primitiveOperationAction);
            container.AddActionImport("FunctionImport_Primitive", primitiveOperationAction);

            EdmAction primitiveCollectionOperationAction = new EdmAction("TestNS", "FunctionImport_PrimitiveCollection", null);
            EdmOperationParameter primitiveCollectionParameter = new EdmOperationParameter(primitiveCollectionOperationAction, "primitiveCollection", EdmCoreModel.GetCollection(EdmCoreModel.Instance.GetString(true)));
            primitiveCollectionOperationAction.AddParameter(primitiveCollectionParameter);
            model.AddElement(primitiveCollectionOperationAction);
            container.AddActionImport("FunctionImport_PrimitiveCollection", primitiveCollectionOperationAction);

            EdmAction complexOperationAction = new EdmAction("TestNS", "FunctionImport_Complex", null);
            EdmOperationParameter complexParameter = new EdmOperationParameter(complexOperationAction, "complex", new EdmComplexTypeReference(complexType, true));
            complexOperationAction.AddParameter(complexParameter);
            model.AddElement(complexOperationAction);
            container.AddActionImport("FunctionImport_Complex", complexOperationAction);

            EdmAction complexCollectionOperationAction = new EdmAction("TestNS", "FunctionImport_ComplexCollection", null);
            EdmOperationParameter complexCollectionParameter = new EdmOperationParameter(complexCollectionOperationAction, "complexCollection", EdmCoreModel.GetCollection(new EdmComplexTypeReference(complexType, true)));
            complexCollectionOperationAction.AddParameter(complexCollectionParameter);
            model.AddElement(complexCollectionOperationAction);
            container.AddActionImport("FunctionImport_ComplexCollection", complexCollectionOperationAction);

            EdmAction entityOperationAction = new EdmAction("TestNS", "FunctionImport_Entry", null);
            EdmOperationParameter entityParameter = new EdmOperationParameter(entityOperationAction, "entry", new EdmEntityTypeReference(entity, true));
            entityOperationAction.AddParameter(entityParameter);
            model.AddElement(entityOperationAction);
            container.AddActionImport("FunctionImport_Entry", entityOperationAction);

            EdmAction feedOperationAction = new EdmAction("TestNS", "FunctionImport_Feed", null);
            EdmOperationParameter feedParameter = new EdmOperationParameter(feedOperationAction, "feed", EdmCoreModel.GetCollection(new EdmEntityTypeReference(entity, true)));
            feedOperationAction.AddParameter(feedParameter);
            model.AddElement(feedOperationAction);
            container.AddActionImport("FunctionImport_Feed", feedOperationAction);

            EdmAction streamOperationAction = new EdmAction("TestNS", "FunctionImport_Stream", null);
            EdmOperationParameter streamParameter = new EdmOperationParameter(streamOperationAction, "stream", EdmCoreModel.Instance.GetStream(true));
            streamOperationAction.AddParameter(streamParameter);
            model.AddElement(streamOperationAction);
            container.AddActionImport("FunctionImport_Stream", streamOperationAction);
            
            EdmAction enumOperationAction = new EdmAction("TestNS", "FunctionImport_Enum", null);
            EdmOperationParameter enumParameter = new EdmOperationParameter(enumOperationAction, "enum", new EdmEnumTypeReference(enumType, true));
            enumOperationAction.AddParameter(enumParameter);
            model.AddElement(enumOperationAction);
            container.AddActionImport("FunctionImport_Enum", enumOperationAction);

            model.AddElement(container);

            this.BasicConstructibleModelTestSerializingStockModel(ODataTestModelBuilder.ODataTestModelWithFunctionImport, model);
        }
        public void ConstructibleModelODataTestModelAnnotationTestWithoutAnnotation()
        {
            EdmModel model = new EdmModel();

            EdmComplexType address = new EdmComplexType("TestModel", "Address");
            EdmStructuralProperty addressStreet = address.AddStructuralProperty("Street", EdmCoreModel.Instance.GetString(true));
            EdmStructuralProperty addressZip = address.AddStructuralProperty("Zip", EdmCoreModel.Instance.GetInt32(false));
            model.AddElement(address);

            EdmEntityType person = new EdmEntityType("TestModel", "PersonType");
            EdmStructuralProperty personID = person.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(false));
            EdmStructuralProperty personName = person.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(false));
            EdmStructuralProperty personAddress = person.AddStructuralProperty("Address", new EdmComplexTypeReference(address, false));
            EdmStructuralProperty personPicture = person.AddStructuralProperty("Picture", EdmCoreModel.Instance.GetStream(false));
            person.AddKeys(personID);
            model.AddElement(person);

            EdmEntityContainer container = new EdmEntityContainer("TestModel", "DefaultContainer");
            EdmEntitySet peopleSet = container.AddEntitySet("People", person);
            EdmEntitySet personSet = container.AddEntitySet("PersonType", person);

            EdmAction serviceOperationAction = new EdmAction("TestModel", "ServiceOperation1", EdmCoreModel.Instance.GetInt32(false));
            EdmOperationParameter a = new EdmOperationParameter(serviceOperationAction, "a", EdmCoreModel.Instance.GetInt32(true));
            EdmOperationParameter b = new EdmOperationParameter(serviceOperationAction, "b", EdmCoreModel.Instance.GetString(true));
            serviceOperationAction.AddParameter(a);
            serviceOperationAction.AddParameter(b);
            model.AddElement(serviceOperationAction);
            container.AddActionImport("ServiceOperation1", serviceOperationAction);
            model.AddElement(container);

            this.BasicConstructibleModelTestSerializingStockModel(ODataTestModelBuilder.ODataTestModelAnnotationTestWithoutAnnotations, model);
        }
        public void ConstructibleModelODataTestModelBasicTest()
        {
            EdmModel model = new EdmModel();

            EdmComplexType address = new EdmComplexType("TestModel", "Address");
            model.AddElement(address);

            EdmStructuralProperty addressStreet = address.AddStructuralProperty("Street", EdmCoreModel.Instance.GetString(true));
            EdmStructuralProperty addressZip = new EdmStructuralProperty(address, "Zip", EdmCoreModel.Instance.GetInt32(false));
            address.AddProperty(addressZip);

            EdmEntityType office = new EdmEntityType("TestModel", "OfficeType");
            EdmStructuralProperty officeId = office.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(false));
            office.AddKeys(officeId);
            EdmStructuralProperty officeAddress = office.AddStructuralProperty("Address", new EdmComplexTypeReference(address, false));
            model.AddElement(office);

            EdmEntityType city = new EdmEntityType("TestModel", "CityType");
            EdmStructuralProperty cityId = city.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(false));
            city.AddKeys(cityId);
            EdmStructuralProperty cityName = city.AddStructuralProperty("Name", EdmCoreModel.Instance.GetString(true));
            EdmStructuralProperty citySkyline = city.AddStructuralProperty("Skyline", EdmCoreModel.Instance.GetStream(false));
            EdmStructuralProperty cityMetroLanes = city.AddStructuralProperty("MetroLanes", EdmCoreModel.GetCollection(EdmCoreModel.Instance.GetString(true)));
            EdmNavigationProperty cityHall = city.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "CityHall", Target = office, TargetMultiplicity = EdmMultiplicity.Many });
            EdmNavigationProperty dol = city.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "DOL", Target = office, TargetMultiplicity = EdmMultiplicity.Many });
            EdmNavigationProperty policeStation = city.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "PoliceStation", Target = office, TargetMultiplicity = EdmMultiplicity.One });
            model.AddElement(city);

            EdmEntityType cityWithMap = new EdmEntityType("TestModel", "CityWithMapType", city);
            model.AddElement(cityWithMap);

            EdmEntityType cityOpenType = new EdmEntityType("TestModel", "CityOpenType", city, false, true);
            model.AddElement(cityOpenType);

            EdmEntityType person = new EdmEntityType("TestModel", "Person");
            EdmStructuralProperty personId = person.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(false));
            person.AddKeys(personId);
            model.AddElement(person);

            EdmNavigationProperty friend = person.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo() { Name = "Friend", Target = person, TargetMultiplicity = EdmMultiplicity.Many });

            EdmEntityType employee = new EdmEntityType("TestModel", "Employee", person);
            EdmStructuralProperty companyName = employee.AddStructuralProperty("CompanyName", EdmCoreModel.Instance.GetString(true));
            model.AddElement(employee);

            EdmEntityType manager = new EdmEntityType("TestModel", "Manager", employee);
            EdmStructuralProperty level = manager.AddStructuralProperty("Level", EdmCoreModel.Instance.GetInt32(false));
            model.AddElement(manager);

            EdmEntityContainer container = new EdmEntityContainer("TestModel", "DefaultContainer");
            EdmEntitySet officeSet = new EdmEntitySet(container, "OfficeType", office);
            container.AddElement(officeSet);
            EdmEntitySet citySet = container.AddEntitySet("CityType", city);
            EdmEntitySet personSet = container.AddEntitySet("Person", person);

            citySet.AddNavigationTarget(cityHall, officeSet);
            citySet.AddNavigationTarget(dol, officeSet);
            citySet.AddNavigationTarget(policeStation, officeSet);
            personSet.AddNavigationTarget(friend, personSet);

            EdmAction serviceOperationAction = new EdmAction("TestModel", "ServiceOperation1", EdmCoreModel.Instance.GetInt32(true));
            EdmOperationParameter a = new EdmOperationParameter(serviceOperationAction, "a", EdmCoreModel.Instance.GetInt32(true));
            serviceOperationAction.AddParameter(a);
            serviceOperationAction.AddParameter("b", EdmCoreModel.Instance.GetString(true));
            model.AddElement(serviceOperationAction);
            
            container.AddActionImport("ServiceOperation1", serviceOperationAction);
            
            model.AddElement(container);

            this.BasicConstructibleModelTestSerializingStockModel(ODataTestModelBuilder.ODataTestModelBasicTest, model);
        }
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="model">The model this instance belongs to.</param>
        /// <param name="operation">The resource operation underlying this function import.</param>
        /// <param name="namespaceName">The namespace of the operation.</param>
        /// <remarks>This constructor assumes that the entity set for this service operation has already be created.</remarks>
        protected internal MetadataProviderEdmOperation(MetadataProviderEdmModel model, OperationWrapper operation, string namespaceName)
        {
            Debug.Assert(model != null, "model != null");
            Debug.Assert(operation != null, "operation != null");

            this.model = model;
            this.ServiceOperation = operation;
            this.Namespace = namespaceName;

            if (operation.Kind == OperationKind.Action)
            {
                this.isBound = this.ServiceOperation.BindingParameter != null;
            }
            else
            {
                Debug.Assert(operation.Kind == OperationKind.ServiceOperation, "serviceOperation.Kind == OperationKind.ServiceOperation");
                Debug.Assert(operation.OperationParameterBindingKind == OperationParameterBindingKind.Never, "operation.OperationParameterBindingKind == OperationParameterBindingKind.Never");
                this.isBound = DefaultIsBindable;
            }

            // EntitySetPath=<path string>
            ResourceSetPathExpression resultSetPathExpression = operation.ResultSetPathExpression;
            this.entitySetPath = resultSetPathExpression == null ? null : resultSetPathExpression.PathExpression;

#if DEBUG
            ResourceType returnType = operation.ReturnType;
            ResourceSetWrapper resultSet = operation.ResourceSet;

            Debug.Assert(
                returnType == null || returnType.ResourceTypeKind == ResourceTypeKind.EntityCollection || returnType.ResourceTypeKind == ResourceTypeKind.EntityType || (resultSet == null && resultSetPathExpression == null),
                "resultSet and resultSetPathExpression must be both null when the return type is not an entity type or an entity collection type.");
            Debug.Assert(
                (returnType == null || returnType.ResourceTypeKind != ResourceTypeKind.EntityCollection && returnType.ResourceTypeKind != ResourceTypeKind.EntityType) || (resultSet != null || resultSetPathExpression != null),
                "One of resultSet or resultSetPathExpression must be set when the return type is either an entity type or an entity collection type.");
            Debug.Assert(resultSet == null || resultSetPathExpression == null, "resultSet and resultSetPathExpression cannot be both set.");
#endif

            string mimeType = operation.MimeType;
            if (!string.IsNullOrEmpty(mimeType))
            {
                model.SetMimeType(this, mimeType);
            }

            switch (this.ServiceOperation.OperationParameterBindingKind)
            {
                case OperationParameterBindingKind.Always:
                    break;

                default:
                    Debug.Assert(
                        this.ServiceOperation.OperationParameterBindingKind == OperationParameterBindingKind.Sometimes ||
                        this.ServiceOperation.OperationParameterBindingKind == OperationParameterBindingKind.Never,
                        "this.ServiceOperation.OperationParameterBindingKind == OperationParameterBindingKind.Sometimes || this.ServiceOperation.OperationParameterBindingKind == OperationParameterBindingKind.Never");
                    break;
            }

            ReadOnlyCollection<OperationParameter> operationParameters = operation.Parameters;
            if (operationParameters != null && operationParameters.Count > 0)
            {
                List<IEdmOperationParameter> list = new List<IEdmOperationParameter>(operationParameters.Count);
                foreach (OperationParameter parameter in operationParameters)
                {
                    IEdmTypeReference parameterType = this.model.EnsureTypeReference(parameter.ParameterType, /*annotations*/ null);
                    EdmOperationParameter edmParameter = new EdmOperationParameter(this, parameter.Name, parameterType);
                    list.Add(edmParameter);
                }

                this.parameters = new ReadOnlyCollection<IEdmOperationParameter>(list);
            }

            this.ReturnType = this.CreateReturnTypeReference();
        }
        public static EdmModel OperationImportParameterWithAnnotationModel()
        {
            EdmModel model = new EdmModel();

            EdmEntityContainer container = new EdmEntityContainer("Default", "Container");
            EdmAction simpleOperationAction = new EdmAction("Default", "SimpleFunction", EdmCoreModel.Instance.GetInt32(false));
            EdmOperationParameter simpleOperationName = new EdmOperationParameter(simpleOperationAction, "Name", EdmCoreModel.Instance.GetString(true));
            simpleOperationAction.AddParameter(simpleOperationName);
            model.AddElement(simpleOperationAction);
            EdmOperationImport simpleOperation = new EdmActionImport(container, "SimpleFunction", simpleOperationAction);
            container.AddElement(simpleOperation);
            model.AddElement(container);

            var annotationElement = 
                new XElement("{http://foo}Annotation", 1);
            var annotation = new EdmStringConstant(EdmCoreModel.Instance.GetString(false), annotationElement.ToString());
            annotation.SetIsSerializedAsElement(model, true);
            model.SetAnnotationValue(simpleOperationName, "http://foo", "Annotation", annotation);

            return model;
        }
        public static EdmModel AnnotationWithValueModel()
        {
            EdmModel model = new EdmModel();

            EdmOperation simpleOperation = new EdmFunction("DefaultNamespace", "SimpleFunction", EdmCoreModel.Instance.GetString(true));
            EdmOperationParameter simpleOperationId = new EdmOperationParameter(simpleOperation, "Id", EdmCoreModel.Instance.GetInt32(true));
            simpleOperation.AddParameter(simpleOperationId);
            model.AddElement(simpleOperation);

            XElement annotationElement =
                new XElement("{http://foo}Annotation",
                    "Value 1.0"
                );
            var annotation = new EdmStringConstant(EdmCoreModel.Instance.GetString(false), annotationElement.ToString());
            annotation.SetIsSerializedAsElement(model, true);
            model.SetAnnotationValue(simpleOperationId, "http://foo", "Annotation", annotation);

            return model;
        }
        public static IEdmModel ModelWithInvalidFunctionReturnType()
        {
            EdmModel model = new EdmModel();
            
            IEdmTypeReference badTypeRef = new StubEdmTypeReference { Definition = new StubEdmType(), IsNullable = false };

            EdmFunction func1 = new EdmFunction("Default", "Foo", badTypeRef);

            EdmOperationParameter par1 = new EdmOperationParameter(func1, "Parameter", badTypeRef);
            func1.AddParameter(par1);

            model.AddElement(func1);

            return model;
        }
        public static IEdmModel ModelWithInvalidOperationParameterType()
        {
            EdmModel model = new EdmModel();

            EdmOperation a = new EdmAction("Foo", "Bar", EdmCoreModel.Instance.GetString(false));

            IEdmTypeReference badTypeRef = new StubEdmTypeReference { Definition = new StubEdmType(), IsNullable = false };
            EdmOperationParameter par1 = new EdmOperationParameter(a, "Parameter", badTypeRef);
            a.AddParameter(par1);

            model.AddElement(a);

            return model;
        }
Beispiel #14
0
 /// <summary>
 /// Creates and adds a parameter to this function (as the last parameter).
 /// </summary>
 /// <param name="name">The name of the parameter being added.</param>
 /// <param name="type">The type of the parameter being added.</param>
 /// <returns>Created parameter.</returns>
 public EdmOperationParameter AddParameter(string name, IEdmTypeReference type)
 {
     EdmOperationParameter parameter = new EdmOperationParameter(this, name, type);
     this.parameters.Add(parameter);
     return parameter;
 }