在 2019-10-21 2019-10-21 发表的泛型当中我们说过一个很重要的点就是不同的泛型类,进入的时候其实在后台的时候创建了不同的类,所以进入的类其实是不同的由此我们可以想想出一种方法 来做缓存

 1 namespace YZMDAL
 2 {
 3     public class SQLBuilder<T> where T: BaseModel
 4     {
 5         private static string FindOne = null;
 6         private static string insertSql = null;
 7         private static string updatedb = null;
 8         private static string Deletesql = null;
 9         /// <summary>
10         /// 无参构造器
11         /// </summary>
12         static SQLBuilder()
13         {
14             {
15                 Type type = typeof(T);//获取类型
16                 string columnString = string.Join(",", type.GetProperties().Select(x => $"`{x.GetColumnMappingName()}`"));
17                 FindOne = $@"select {columnString} from  `{type.GetMappingName()}` where Id=@id";//type.GetMappingName()特性的语法糖
18             }
19             {
20                 Type type = typeof(T);//获取类型
21                 string columnString = string.Join(",", type.GetProperties().FilterKey().Select(x => $"`{x.GetColumnMappingName()}`"));//出去主键Id
22                 //遍历属性值
23                 string  ValuesString = string.Join(",", type.GetProperties().FilterKey().Select(x => $"@{x.GetColumnMappingName()}"));
24                 insertSql = $@"Insert into `{type.Name}`({columnString}) values({ValuesString})";
25             }
26             {
27                 Type type = typeof(T);
28                 //string Columnsql = string.Join(",", type.GetProperties().FilterKey().Select(x => $"{x.Name}=$'{x.GetValue(t)??DBNull.Value }'"));//为防止sql注入
29                 string Columnsql = string.Join(",", type.GetProperties().FilterKey().Select(x => $"`{x.Name}`=@{x.Name}"));//防止sql注入并使用sqlparament
30                 updatedb = $"UPDATE  `{type.Name}` SET {Columnsql} where Id=@Id ";
31             }
32             {
33                 //delete 方法
34                 Type type = typeof(T);
35                 //string Columnsql = string.Join(",", type.GetProperties().FilterKey().Select(x => $"{x.Name}=$'{x.GetValue(t)??DBNull.Value }'"));//为防止sql注入
36                 string Columnsql = string.Join(",", type.GetProperties().FilterKey().Select(x => $"{x.Name}=@{x.Name}"));//防止sql注入并使用sqlparament
37                 Deletesql = $" delete  from `{type.Name}`  where  Id = @Id ";
38             }
39         }
40
41         /// <summary>
42         /// 对外提供的接口
43         /// </summary>
44         /// <param name="sqlType"></param>
45         /// <returns></returns>
46         public static string GetSql (sqlType sqlType)
47         {
48             switch (sqlType)
49             {
50                 case sqlType.FindOne:
51                     return FindOne;
52                 case sqlType.insertSql:
53                     return insertSql;
54                 case sqlType.updatedb:
55                     return updatedb;
56                 case sqlType.Delete:
57                     return Deletesql;
58                 default:
59                     throw new Exception("我也不知道该写啥");
60             }
61         }
62         /// <summary>
63         /// 枚举传参
64         /// </summary>
65         public enum sqlType
66         {
67             FindOne  ,
68             insertSql,
69             updatedb,
70             Delete
71         }
72
73     }
74 }

例如以上代码:当不同的类型进入类的时候其实是进入了无数个复写的类 所有的静态方法都是不一样的所缓存的内容也是不一样的而且Static的生命周期是和进程一致的只要进程不中断,当前的static都是不会被回收的 由此我我们们可以将不长改动的数据进行缓存

而不用每次进行数据拼接 

01-09 03:47