示例#1
0
 private void ReadServices(IEdmModel model, Dictionary <IEdmType, TypeTransferObject> mapping, List <ITransferObject> list)
 {
     foreach (IEdmEntityContainerElement entitySet in model.EntityContainer.Elements)
     {
         HttpServiceTransferObject service = new HttpServiceTransferObject
         {
             Name     = entitySet.Name,
             Route    = entitySet.Name,
             Language = ODataLanguage.Instance
         };
         if (entitySet is IEdmNavigationSource navigationSource)
         {
             service.Actions.Add(this.ReadAction(HttpServiceActionTypeTransferObject.Get, mapping, navigationSource));
             service.Actions.Add(this.ReadAction(HttpServiceActionTypeTransferObject.Post, mapping, navigationSource));
             service.Actions.Add(this.ReadAction(HttpServiceActionTypeTransferObject.Put, mapping, navigationSource));
             service.Actions.Add(this.ReadAction(HttpServiceActionTypeTransferObject.Patch, mapping, navigationSource));
             service.Actions.Add(this.ReadAction(HttpServiceActionTypeTransferObject.Delete, mapping, navigationSource));
         }
         list.Add(service);
     }
 }
示例#2
0
        private void Read(OpenApiReadConfiguration configuration, OpenApiDocument document)
        {
            HttpServiceTransferObject service = new HttpServiceTransferObject
            {
                Name = configuration.Name ?? document.Info.Description.Replace("Class ", "").Trim()
            };

            foreach (KeyValuePair <string, OpenApiPathItem> pathPair in document.Paths)
            {
                foreach (KeyValuePair <OperationType, OpenApiOperation> operationPair in pathPair.Value.Operations)
                {
                    HttpServiceActionTypeTransferObject type   = operationPair.Key.ToActionType();
                    HttpServiceActionTransferObject     action = new HttpServiceActionTransferObject
                    {
                        Name  = pathPair.Key.Split('/').Last(),
                        Route = pathPair.Key,
                        Type  = type,
                        RequireBodyParameter = type.IsBodyParameterRequired()
                    };
                    foreach (OpenApiParameter parameter in operationPair.Value.Parameters)
                    {
                        action.Parameters.Add(new HttpServiceActionParameterTransferObject
                        {
                            Name = parameter.Name,
                            Type = this.Read(configuration, parameter.Schema)
                        });
                    }
                    OpenApiMediaType content = operationPair.Value.RequestBody.Content.Single().Value;
                    if (action.RequireBodyParameter)
                    {
                        action.Parameters.Add(new HttpServiceActionParameterTransferObject
                        {
                            Name     = "request",
                            Type     = this.Read(configuration, content.Schema),
                            FromBody = true
                        });
                    }
                    else
                    {
                        foreach (KeyValuePair <string, OpenApiSchema> propertyPair in content.Schema.Properties)
                        {
                            action.Parameters.Add(new HttpServiceActionParameterTransferObject
                            {
                                Name = propertyPair.Key,
                                Type = this.Read(configuration, propertyPair.Value)
                            });
                        }
                    }
                    OpenApiResponse response = operationPair.Value.Responses.SingleOrDefault(x => x.Key == "200").Value;
                    if (response == null)
                    {
                        action.ReturnType = new TypeTransferObject {
                            Name = "void"
                        };
                    }
                    else
                    {
                        action.ReturnType = this.Read(configuration, response.Content.Single().Value.Schema);
                    }
                    service.Actions.Add(action);
                }
            }
        }
        public virtual void Read(AspDotNetReadConfiguration configuration, List <ITransferObject> transferObjects)
        {
            configuration.Controller.AssertIsNotNull($"ASP: {nameof(configuration.Controller)}");
            configuration.Controller.Name.AssertIsNotNull($"ASP: {nameof(configuration.Controller)}.{nameof(configuration.Controller.Name)}");
            configuration.Controller.Namespace.AssertIsNotNull($"ASP: {nameof(configuration.Controller)}.{nameof(configuration.Controller.Namespace)}");
            Logger.Trace($"Read ASP.NET controller {configuration.Controller.Namespace}{configuration.Controller.Name}...");
            Type type = GeneratorTypeLoader.Get(configuration, configuration.Controller.Assembly, configuration.Controller.Namespace, configuration.Controller.Name);

            if (type == null)
            {
                return;
            }

            HttpServiceTransferObject controller = new HttpServiceTransferObject();

            controller.Name     = type.Name;
            controller.Language = ReflectionLanguage.Instance;

            Attribute routeAttribute = type.GetCustomAttributes().FirstOrDefault();

            controller.Route = routeAttribute?.GetType().GetProperty("Template")?.GetValue(routeAttribute)?.ToString();

            MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
            foreach (MethodInfo method in methods)
            {
                this.modelReader.Read(method.ReturnType, transferObjects);
                foreach (Attribute attribute in method.GetCustomAttributes())
                {
                    Type attributeType = attribute.GetType();
                    HttpServiceActionTransferObject action = new HttpServiceActionTransferObject();
                    action.ReturnType = method.ReturnType.ToTransferObject();
                    if (action.ReturnType.Name == "ActionResult")
                    {
                        action.ReturnType = action.ReturnType.Generics.Single().Type;
                    }
                    action.Route = attributeType.GetProperty("Template")?.GetValue(attribute)?.ToString();
                    int methodNameIndex = 1;
                    while (true)
                    {
                        string actionName = $"{method.Name}{(methodNameIndex > 1 ? methodNameIndex.ToString() : "")}";
                        if (controller.Actions.All(x => !x.Name.Equals(actionName)))
                        {
                            action.Name = actionName;
                            break;
                        }
                        methodNameIndex++;
                    }
                    ParameterInfo[] parameters = method.GetParameters();
                    foreach (ParameterInfo parameter in parameters)
                    {
                        this.modelReader.Read(parameter.ParameterType, transferObjects);
                    }
                    switch (attributeType.Name)
                    {
                    case "HttpGetAttribute":
                        action.Type = HttpServiceActionTypeTransferObject.Get;
                        break;

                    case "HttpPostAttribute":
                        action.Type = HttpServiceActionTypeTransferObject.Post;
                        break;

                    case "HttpPatchAttribute":
                        action.Type = HttpServiceActionTypeTransferObject.Patch;
                        break;

                    case "HttpPutAttribute":
                        action.Type = HttpServiceActionTypeTransferObject.Put;
                        break;

                    case "HttpDeleteAttribute":
                        action.Type = HttpServiceActionTypeTransferObject.Delete;
                        break;

                    case "ConditionalAttribute":
                        // Ignore these attributes
                        continue;

                    default:
                        Logger.Warning($"Unknown controller action attribute {attributeType.Name}");
                        continue;
                    }
                    action.RequireBodyParameter = action.Type.IsBodyParameterRequired();
                    foreach (ParameterInfo parameter in parameters)
                    {
                        HttpServiceActionParameterTransferObject actionParameter = new HttpServiceActionParameterTransferObject();
                        actionParameter.Name        = parameter.Name;
                        actionParameter.Type        = parameter.ParameterType.ToTransferObject();
                        actionParameter.FromBody    = action.RequireBodyParameter && parameter.GetCustomAttributes().Any(parameterAttribute => parameterAttribute.GetType().Name == "FromBodyAttribute");
                        actionParameter.Inline      = action.Route != null && action.Route.Contains($"{{{parameter.Name}}}");
                        actionParameter.InlineIndex = actionParameter.Inline ? action.Route.IndexOf($"{{{parameter.Name}}}", StringComparison.Ordinal) : 0;
                        action.Parameters.Add(actionParameter);
                    }
                    if (action.RequireBodyParameter)
                    {
                        if (action.Parameters.Count == 0)
                        {
                            throw new InvalidOperationException($"Can not write {method.Name}. {action.Type} requires at least one parameter, but no parameter found.");
                        }
                        if (action.Parameters.Count == 1)
                        {
                            action.Parameters.Single().FromBody = true;
                        }
                        else if (action.Parameters.All(x => !x.FromBody))
                        {
                            throw new InvalidOperationException($"Can not write {method.Name}. {action.Type} requires at least one parameter marked with [FromBody] or can have only one parameter");
                        }
                    }
                    controller.Actions.Add(action);
                }
            }
            transferObjects.Add(controller);
        }