public void GenerateServiceOperations(TemplateFile templateFile)
        {
            StringBuilder serviceOps = new StringBuilder();

            foreach (MethodInfo method in typeof(NonClrTestWebService<>).GetMethods())
            {
                object[] attribs = method.GetCustomAttributes(false);

                bool isSingleResult = false;
                foreach (object attrib in attribs)
                {
                    if (attrib is Microsoft.OData.Service.SingleResultAttribute)
                    {
                        isSingleResult = true;
                        break;
                    }
                }

                foreach (object attrib in attribs)
                {
                    if (attrib is System.ServiceModel.Web.WebGetAttribute || attrib is System.ServiceModel.Web.WebInvokeAttribute)
                    {
                        string kind;
                        if (typeof(IQueryable).IsAssignableFrom(method.ReturnType))
                        {
                            if (isSingleResult)
                            {
                                kind = "ServiceOperationResultKind.QueryWithSingleResult";
                            }
                            else
                            {
                                kind = "ServiceOperationResultKind.QueryWithMultipleResults";
                            }
                        }
                        else if (typeof(IEnumerable).IsAssignableFrom(method.ReturnType)
                            && !typeof(RowComplexType).IsAssignableFrom(method.ReturnType)
                            && method.ReturnType != typeof(string) && method.ReturnType != typeof(byte[]))
                        {
                            // this will be incorrect for complex types which derive from IEnumerable, but its the best we can do for now
                            kind = "ServiceOperationResultKind.Enumeration";
                        }
                        else if (typeof(void).IsAssignableFrom(method.ReturnType))
                        {
                            kind = "ServiceOperationResultKind.Void";
                        }
                        else
                        {
                            // either primitive or complex
                            kind = "ServiceOperationResultKind.DirectValue";
                        }

                        string verb;
                        if (attrib is System.ServiceModel.Web.WebGetAttribute)
                            verb = "GET";
                        else
                            verb = (attrib as System.ServiceModel.Web.WebInvokeAttribute).Method;

                        serviceOps.AppendLine("serviceOpCreateParams.Add(new ServiceOperationCreationParams(\"" + method.Name + "\", " + kind + ", \"\", \"" + verb + "\"));");
                        break;
                    }
                }
            }

            foreach (ServiceOperation op in _workspace.ServiceContainer.ResourceContainers.OfType<ServiceOperation>())
            {
                if (!op.ServiceOperationResultKind.HasValue)
                    AstoriaTestLog.FailAndThrow("Cannot generate code for service operation because the result kind is not set");
                if (op.ExpectedTypeName == null)
                    AstoriaTestLog.FailAndThrow("Cannot generate code for service operation because the expected type name is not set");

                string kind = "ServiceOperationResultKind." + op.ServiceOperationResultKind.Value.ToString();
                serviceOps.AppendLine("serviceOpCreateParams.Add(new ServiceOperationCreationParams(\"" + op.Name + "\", " + kind + ", \"" + op.BaseType.Name + "\", \"" + op.Verb.ToHttpMethod() + "\"));");
            }

            templateFile.FileText = templateFile.FileText.Replace("[[GeneratedServiceOperations]]", serviceOps.ToString());
        }
Exemplo n.º 2
0
        public void GenerateServiceOperations(TemplateFile templateFile)
        {
            StringBuilder serviceOps = new StringBuilder();

            foreach (MethodInfo method in typeof(NonClrTestWebService <>).GetMethods())
            {
                object[] attribs = method.GetCustomAttributes(false);

                bool isSingleResult = false;
                foreach (object attrib in attribs)
                {
                    if (attrib is Microsoft.OData.Service.SingleResultAttribute)
                    {
                        isSingleResult = true;
                        break;
                    }
                }

                foreach (object attrib in attribs)
                {
                    if (attrib is System.ServiceModel.Web.WebGetAttribute || attrib is System.ServiceModel.Web.WebInvokeAttribute)
                    {
                        string kind;
                        if (typeof(IQueryable).IsAssignableFrom(method.ReturnType))
                        {
                            if (isSingleResult)
                            {
                                kind = "ServiceOperationResultKind.QueryWithSingleResult";
                            }
                            else
                            {
                                kind = "ServiceOperationResultKind.QueryWithMultipleResults";
                            }
                        }
                        else if (typeof(IEnumerable).IsAssignableFrom(method.ReturnType) &&
                                 !typeof(RowComplexType).IsAssignableFrom(method.ReturnType) &&
                                 method.ReturnType != typeof(string) && method.ReturnType != typeof(byte[]))
                        {
                            // this will be incorrect for complex types which derive from IEnumerable, but its the best we can do for now
                            kind = "ServiceOperationResultKind.Enumeration";
                        }
                        else if (typeof(void).IsAssignableFrom(method.ReturnType))
                        {
                            kind = "ServiceOperationResultKind.Void";
                        }
                        else
                        {
                            // either primitive or complex
                            kind = "ServiceOperationResultKind.DirectValue";
                        }

                        string verb;
                        if (attrib is System.ServiceModel.Web.WebGetAttribute)
                        {
                            verb = "GET";
                        }
                        else
                        {
                            verb = (attrib as System.ServiceModel.Web.WebInvokeAttribute).Method;
                        }

                        serviceOps.AppendLine("serviceOpCreateParams.Add(new ServiceOperationCreationParams(\"" + method.Name + "\", " + kind + ", \"\", \"" + verb + "\"));");
                        break;
                    }
                }
            }

            foreach (ServiceOperation op in _workspace.ServiceContainer.ResourceContainers.OfType <ServiceOperation>())
            {
                if (!op.ServiceOperationResultKind.HasValue)
                {
                    AstoriaTestLog.FailAndThrow("Cannot generate code for service operation because the result kind is not set");
                }
                if (op.ExpectedTypeName == null)
                {
                    AstoriaTestLog.FailAndThrow("Cannot generate code for service operation because the expected type name is not set");
                }

                string kind = "ServiceOperationResultKind." + op.ServiceOperationResultKind.Value.ToString();
                serviceOps.AppendLine("serviceOpCreateParams.Add(new ServiceOperationCreationParams(\"" + op.Name + "\", " + kind + ", \"" + op.BaseType.Name + "\", \"" + op.Verb.ToHttpMethod() + "\"));");
            }

            templateFile.FileText = templateFile.FileText.Replace("[[GeneratedServiceOperations]]", serviceOps.ToString());
        }
        public void GenerateMetadata(TemplateFile templateFile)
        {
            StringBuilder complexTypes = new StringBuilder();
            StringBuilder complexTypeProperties = new StringBuilder();

            StringBuilder resourceTypes = new StringBuilder();
            StringBuilder resourceTypeProperties = new StringBuilder();

            StringBuilder resourceSets = new StringBuilder();

            StringBuilder setDictionary = new StringBuilder();
            StringBuilder typeDictionary = new StringBuilder();

            StringBuilder clrTypeClasses = new StringBuilder();

            // Complex Types
            foreach (ComplexType complexType in _workspace.ServiceContainer.ComplexTypes)
            {
                complexTypes.AppendLine(CreateType(complexType));
                complexTypes.AppendLine();

                // properties
                foreach (ResourceProperty property in complexType.Properties)
                {
                    if (complexType.BaseType == null || !complexType.BaseTypes.Any(baseType => baseType.Properties.Any(p => p.Name == property.Name)))
                        complexTypeProperties.AppendLine(CreateProperty(complexType, property));
                }

                // type Dictionary
                typeDictionary.AppendLine(String.Format("types.Add({0});", complexType.Name.ToLowerInvariant()));
            }
            typeDictionary.AppendLine();

            // Resource Types
            foreach (ResourceType type in _workspace.ServiceContainer.ResourceTypes)
            {
                bool lazyLoad = false;

                if (_workspace.Settings.UseLazyPropertyLoading)
                {
                    Random r = AstoriaTestProperties.Random;
                    if (r.Next(0, 2) == 0)
                    {
                        lazyLoad = true;
                        type.Facets.IsLazyLoaded = true;
                    }
                }

                resourceTypes.AppendLine(CreateType(type, lazyLoad));
                typeDictionary.AppendLine(String.Format("types.Add({0});", type.Name.ToLowerInvariant()));

                if (type.Facets.IsClrType)
                    clrTypeClasses.Append(CreateClrTypeClass(type));


                foreach (ResourceProperty property in type.Properties.Where(p => p.Facets.IsDeclaredProperty))
                {
                    if (type.BaseType == null || !type.BaseTypes.Any(baseType => baseType.Properties.Any(p => p.Name == property.Name)))
                        resourceTypeProperties.AppendLine(CreateProperty(type, property, lazyLoad));
                }
            }

            // Resource Sets
            foreach (ResourceContainer container in _workspace.ServiceContainer.ResourceContainers)
            {
                if (container is ServiceOperation)
                    continue;

                // resource sets
                string setName = container.Name;
                string setVariableName = container.Name + "EntitySet";
                string resourceTypeName = container.BaseType.Name.ToLowerInvariant();

                resourceSets.AppendLine(String.Format("ResourceSet {0} = new ResourceSet(\"{1}\", {2});", setVariableName, setName, resourceTypeName));

                if (container.Facets.MestTag != null)
                    resourceSets.AppendLine(String.Format("{0}.CustomState = \"{1}\";", setVariableName, container.Facets.MestTag));

                resourceSets.AppendLine();

                // Add to containers
                setDictionary.AppendLine(String.Format("containers.Add({0});", setVariableName));
            }

            StringBuilder completeCode = new StringBuilder();

            // Order is important!
            completeCode.Append(complexTypes.ToString());
            completeCode.Append(complexTypeProperties.ToString());
            completeCode.Append(resourceTypes.ToString());
            completeCode.Append(resourceSets.ToString());
            completeCode.Append(resourceTypeProperties.ToString());
            completeCode.Append(typeDictionary.ToString());
            completeCode.Append(setDictionary.ToString());

            templateFile.FileText = templateFile.FileText.Replace("[[GeneratedMetadata]]", completeCode.ToString());
            templateFile.FileText = templateFile.FileText.Replace("[[ClrBackingTypes]]", clrTypeClasses.ToString());
        }
Exemplo n.º 4
0
        public void GenerateMetadata(TemplateFile templateFile)
        {
            StringBuilder complexTypes          = new StringBuilder();
            StringBuilder complexTypeProperties = new StringBuilder();

            StringBuilder resourceTypes          = new StringBuilder();
            StringBuilder resourceTypeProperties = new StringBuilder();

            StringBuilder resourceSets = new StringBuilder();

            StringBuilder setDictionary  = new StringBuilder();
            StringBuilder typeDictionary = new StringBuilder();

            StringBuilder clrTypeClasses = new StringBuilder();

            // Complex Types
            foreach (ComplexType complexType in _workspace.ServiceContainer.ComplexTypes)
            {
                complexTypes.AppendLine(CreateType(complexType));
                complexTypes.AppendLine();

                // properties
                foreach (ResourceProperty property in complexType.Properties)
                {
                    if (complexType.BaseType == null || !complexType.BaseTypes.Any(baseType => baseType.Properties.Any(p => p.Name == property.Name)))
                    {
                        complexTypeProperties.AppendLine(CreateProperty(complexType, property));
                    }
                }

                // type Dictionary
                typeDictionary.AppendLine(String.Format("types.Add({0});", complexType.Name.ToLowerInvariant()));
            }
            typeDictionary.AppendLine();

            // Resource Types
            foreach (ResourceType type in _workspace.ServiceContainer.ResourceTypes)
            {
                bool lazyLoad = false;

                if (_workspace.Settings.UseLazyPropertyLoading)
                {
                    Random r = AstoriaTestProperties.Random;
                    if (r.Next(0, 2) == 0)
                    {
                        lazyLoad = true;
                        type.Facets.IsLazyLoaded = true;
                    }
                }

                resourceTypes.AppendLine(CreateType(type, lazyLoad));
                typeDictionary.AppendLine(String.Format("types.Add({0});", type.Name.ToLowerInvariant()));

                if (type.Facets.IsClrType)
                {
                    clrTypeClasses.Append(CreateClrTypeClass(type));
                }


                foreach (ResourceProperty property in type.Properties.Where(p => p.Facets.IsDeclaredProperty))
                {
                    if (type.BaseType == null || !type.BaseTypes.Any(baseType => baseType.Properties.Any(p => p.Name == property.Name)))
                    {
                        resourceTypeProperties.AppendLine(CreateProperty(type, property, lazyLoad));
                    }
                }
            }

            // Resource Sets
            foreach (ResourceContainer container in _workspace.ServiceContainer.ResourceContainers)
            {
                if (container is ServiceOperation)
                {
                    continue;
                }

                // resource sets
                string setName          = container.Name;
                string setVariableName  = container.Name + "EntitySet";
                string resourceTypeName = container.BaseType.Name.ToLowerInvariant();

                resourceSets.AppendLine(String.Format("ResourceSet {0} = new ResourceSet(\"{1}\", {2});", setVariableName, setName, resourceTypeName));

                if (container.Facets.MestTag != null)
                {
                    resourceSets.AppendLine(String.Format("{0}.CustomState = \"{1}\";", setVariableName, container.Facets.MestTag));
                }

                resourceSets.AppendLine();

                // Add to containers
                setDictionary.AppendLine(String.Format("containers.Add({0});", setVariableName));
            }

            StringBuilder completeCode = new StringBuilder();

            // Order is important!
            completeCode.Append(complexTypes.ToString());
            completeCode.Append(complexTypeProperties.ToString());
            completeCode.Append(resourceTypes.ToString());
            completeCode.Append(resourceSets.ToString());
            completeCode.Append(resourceTypeProperties.ToString());
            completeCode.Append(typeDictionary.ToString());
            completeCode.Append(setDictionary.ToString());

            templateFile.FileText = templateFile.FileText.Replace("[[GeneratedMetadata]]", completeCode.ToString());
            templateFile.FileText = templateFile.FileText.Replace("[[ClrBackingTypes]]", clrTypeClasses.ToString());
        }