软件编程
位置:首页>> 软件编程>> C#编程>> c#代码生成URL地址的示例

c#代码生成URL地址的示例

作者:超然  发布时间:2022-02-17 09:44:08 

标签:c#,url,代码生成
目录
  • “头疼”

  • “吃药”

    • 工具代码

    • 使用代码

“头疼”

自己在用Angular做项目时,前端要请求后端数据时的代码如下


this.http.get("url/xxx")

这是请求一个URL地址数据最简单的代码,但是如此简单的代码还会遇到一些头疼的问题

  • URL地址拼写错误,有可能折腾半天才发现😑

  • 后端修改了地址没通知到前端,测试不到位就炸了,不是所有项目都有那么规范的接口变更流程😑

  • 查看代码的时候,看着URL地址,这个地址用途是哈?还要翻接口文档😑

“吃药”

为了解决这个问题,我们需要一个包含所有接口的文件


import { environment } from 'src/environments/environment';
export const WebAPI = {
 /** 授权控制器 */
 Auth: {
 /** */
   Controller: `${environment.host}/api/Auth`,
   /** GET 获得用户 */
   GetUser: `${environment.host}/api/Auth/GetUser`,
   /** POST 登陆 */
   Login: `${environment.host}/api/Auth/Login`,
 },
}

那么请求代码就可以改成


this.http.get(WebAPI.Auth.GetUser)

但是维护这个文件是件吃力的事情,作为一个时刻想着如何偷懒的程序员,吃力的事情就让计算机去干,所以写一个代码生成工具。

工具代码


   public static class BuildWebApiToTS
   {
       public static string Build(Assembly assembly, string prefix = "api/")
       {
           List<Controller> controllers = GetApis(assembly);
           string code = CreateCode(controllers);
           return code.ToString();
       }

public static void BuildToFile(Assembly assembly, string path, string prefix = "api/")
       {
           var code = Build(assembly, prefix);
           string existsCode = "";
           if (System.IO.File.Exists(path) == true)
               existsCode = System.IO.File.ReadAllText(path);

if (existsCode != code)
               System.IO.File.WriteAllText(path, code);
       }

#region 构造代码

public static string CreateCode(List<Controller> controllers, string prefix = "api/")
       {
           StringBuilder code = new StringBuilder();
           code.AppendLine("import { environment } from 'src/environments/environment';");
           code.AppendLine("export const WebAPI = {");
           foreach (var coll in controllers.OrderBy(x => x.Name))
           {
               code.AppendLine($"  /** {coll.ApiComments?.Title} */");
               code.AppendLine($"  {coll.Name}: {{");
               code.AppendLine($"    Controller: `${{environment.host}}/{prefix}{coll.Name}`,");
               foreach (var action in coll.Actions.OrderBy(x => x.Name))
               {
                   code.AppendLine($"    /** {action.Type} {action.ApiComments?.Title} */");
                   code.AppendLine($"    {action.Name}: `${{environment.host}}/{prefix}{coll.Name}/{action.Name}`,");
               }
               code.AppendLine($"  }},");
           }
           code.AppendLine($"}};");
           return code.ToString();
       }

#endregion

#region 获得接口清单

public static List<Controller> GetApis(Assembly assembly)
       {
           List<Controller> controllers = new List<Controller>();
           var collTypes = assembly.GetTypes().Where(x => x.GetCustomAttributes(typeof(ApiControllerAttribute), false).Count() > 0);
           foreach (var collType in collTypes)
           {
               var controller = new Controller(collType.Name.Replace("Controller", ""));
               controller.ApiComments = collType.GetCustomAttribute<ApiCommentsAttribute>();
               controllers.Add(controller);
               controller.Actions.AddRange(GetTypeMembers(collType, typeof(HttpGetAttribute), "GET"));
               controller.Actions.AddRange(GetTypeMembers(collType, typeof(HttpPostAttribute), "POST"));
               controller.Actions.AddRange(GetTypeMembers(collType, typeof(HttpPutAttribute), "PUT"));
               controller.Actions.AddRange(GetTypeMembers(collType, typeof(HttpDeleteAttribute), "DELETE"));
           }
           return controllers;
       }
       private static List<Action> GetTypeMembers(Type type, Type whereType, string saveType)
       {
           var actonTypes = type.GetMembers().Where(x => x.GetCustomAttributes(whereType, false).Count() > 0);
           List<Action> actons = new List<Action>();
           foreach (var actonType in actonTypes)
           {
               var action = new Action(saveType, actonType.Name);
               action.ApiComments = actonType.GetCustomAttribute<ApiCommentsAttribute>();
               actons.Add(action);
           }
           return actons;
       }

public record Controller(string Name)
       {
           public ApiCommentsAttribute ApiComments { get; set; }
           public List<Action> Actions { get; set; } = new List<Action>();
       }

public record Action(string Type, string Name)
       {
           public ApiCommentsAttribute ApiComments { get; set; }
       }

#endregion
   }

public class ApiCommentsAttribute : Attribute
   {
       public string Title { get; set; }
       public ApiCommentsAttribute(string title)
       {
           Title = title;
       }
   }

使用代码


#if DEBUG
BuildWebApiToTS.BuildToFile(typeof(Program).Assembly, "ClientApp/src/app/web-api.ts");
#endif

上面代码大概的流程就是

  1. 利用反射读取程序集中包含ApiControllerAttribute特性的类

  2. 利用发射读取HttpGetAttributeHttpPostAttributeHttpPutAttributeHttpDeleteAttribute特性的方法

  3. 根据需要的格式生成代码

  4. 将代码文件写入ClientApp/src/app/web-api.ts
       

有了这个东西带来以下好处

  1. URL地址拼写错误问题,不存在的🎉

  2. 后端修改了接口,前端直接编译不过,立即发现问题🎉

  3. 鼠标停留,接口注释就显示了🎉

来源:https://www.cnblogs.com/TimChen44/archive/2021/04/10/14642328.html

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com