<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          fastCSharp代碼生成的底層應(yīng)用框架

          聯(lián)合創(chuàng)作 · 2023-10-01 00:32

          fastCSharp是一個基于.NET元數(shù)據(jù)的代碼生成底層應(yīng)用框架,目標是打造一個“開發(fā)+運行效率雙優(yōu)的開源框架。
          經(jīng)過半年多的時間,除了與web開發(fā)直接相關(guān)的部分,都已經(jīng)在fastCSharp part 1.5中完成了重寫工作。
          fastCSharp現(xiàn)在實現(xiàn)的代碼生成實例主要有5個
          1、基于緩存查詢模式的ORM代碼生成實例(現(xiàn)在只支持MSSQL),自定義配置類是fastCSharp.setup.cSharp.sqlTable,同時支持反射模式fastCSharp.setup.cSharp.sqlTable.sqlTool。
          下面是ORM的model定義示例
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
          20
          21
          22
          23
          24
          25
          26
          27
          28
          29
          30
          31
          32
          33
          34
          35
          36
          37
          38
          39
          40
          41
          42
          43
          44
          45
          46
          47
          48
          49
              [fastCSharp.setup.cSharp.sqlTable(ConnectionName = "Connection1")]
              public partial class model1
              {
                  /// 
                  /// 自增列,一個表格只允許一個,如果不配置IsIdentity = true,將自動匹配名稱為 id 的成員
                  /// 
                  [fastCSharp.setup.sqlMember(IsIdentity = true)]
                  public int id;
           
                  /// 
                  /// 關(guān)鍵字1,多個關(guān)鍵字成員按照成員定義順序一致
                  /// 
                  [fastCSharp.setup.sqlMember(IsPrimaryKey = true)]
                  public int key1;
                  /// 
                  /// 關(guān)鍵字2
                  /// 
                  [fastCSharp.setup.sqlMember(IsPrimaryKey = true, IsAscii = true, MaxLength = 32)]
           
                  public string key2;
                  public enum EnumByte : byte
                  {
                       Enum1
                  }
                  /// 
                  /// 直接支持枚舉類型轉(zhuǎn)換,可以不指定SqlType = typeof(byte)
                  /// 
                  [fastCSharp.setup.sqlMember(SqlType = typeof(byte))]
                  public EnumByte key2;
           
                  /// 
                  /// 指定隱式類型轉(zhuǎn)換
                  /// 
                  [fastCSharp.setup.sqlMember(SqlType = typeof(string))]
                  public partial struct image
                  {
                      public string url;
                      /// 
                      /// 如果不能隱式類型轉(zhuǎn)換,必須實現(xiàn)互轉(zhuǎn)函數(shù)
                      /// 
                      public static implicit operator image(string url) { return new image { url = url }; }
                      public static implicit operator string(image image) { return image.url; }
                  }
                  /// 
                  /// 支持隱式類型轉(zhuǎn)換
                  /// 
                  [fastCSharp.setup.sqlMember(IsAscii = true, MaxLength = 64)]
                  public image icon = string.Empty;
              }
           fastCSharp的配置文件是一個純數(shù)據(jù)的json格式文件,比如 Connection1 的配置定義
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          {
          sql:    {
              checkConnection:["Connection1"],
              Connection1:
                  {
                  Diantou:{
                      Type:"sql2008",
                      Connection:"server=192.168.0.100;database=dbname;uid=username;pwd=password"
                      }
                  }
              }
          }
           下面是代碼生成模式示例,采用派生類的模式
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
              public partial class bModel1 : model1.sqlTable
              {
                  static bModel1()
                  {
                      if (SqlTool != null)
                      {
                          //定義緩存
                          Cache = new fastCSharp.sql.cache.whole.identityArray(SqlTool);
                          //定義緩存同步個性化處理事件
                          Cache.OnInserted += XXX;
                          Cache.OnUpdated += XXX;
                      }
                  }
              }
           有時候多個表格結(jié)構(gòu)相同,那么只需要定義一個model,比如可以
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
              public abstract class bModelBase : model1.sqlTable
                  where tableType : bModelBase
              {
              }
              public partial class bModel1_1 : bModelBase
              {
              }
              public partial class bModel1_N : bModelBase
              {
              }
           下面是反射模式示例
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
              public class bModel1 : model1
              {
                  private static readonly fastCSharp.setup.cSharp.sqlTable.sqlTool sqlTool = fastCSharp.setup.cSharp.sqlTable.sqlTool.Default;
                  static bModel1()
                  {
                      if (sqlTool != null)
                      {
                          //緩存定義與事件定義 和 代碼生成模式示例一樣
                      }
                  }   
              }
           有人說ORM不適應(yīng)于復(fù)雜的綜合查詢。真的嗎?我現(xiàn)在展示一段查詢代碼
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
                          return diantou.dataProxy.questionTopic.getTopicCache(id)
                              .getArray(value => diantou.dataProxy.question.get(value.linkId))
                              .getHash(value => value.bestAnswerId)
                              .getArray(value => diantou.dataProxy.answer.get(value))
                              .getFind(value => value != null)
                              .group(value => value.userId)
                              .getArray(value => new keyValue(diantou.dataProxy.user.get(value.Key), value.Value.Count))
                              .group(value => value.Key.grade0)
                              .getArray(value => new userStat
                              {
                                  grade0 = value.Key,
                                  count = value.Value.Count,
                                  path = topic.path.bestAnswer,
                                  users = value.Value.rangeSort((left, right) => right.Value - left.Value, 0, 6)
                                      .getArray(user => diantou.dataProxy.user.get(user.Key, userId))
                              });
           這個查詢需求是,先根據(jù)話題(topic)ID查找相關(guān)聯(lián)的問題(question)ID集合,然后找到這些問題的最佳答案(answer)ID集合,然后根據(jù)這些答案的用戶(user)ID分組統(tǒng)計答案數(shù)量,最后將這些用戶根據(jù)用戶等級分組,每個分組根據(jù)答案數(shù)量取前6個用戶。
          我個人認為基于ORM的查詢更簡單流暢。如果用SQL實現(xiàn)這個需求,該是一個多么復(fù)雜的SQL語句?如果需求有一點點變化,修改這個SQL語句該是多麻煩的事?
          2、數(shù)據(jù)類快速序列化代碼生成實例,自定義配置類是fastCSharp.setup.cSharp.serialize,同時支持反射模式。
          1
          2
          3
          4
          5
          6
          7
              /// 
              /// 僅僅選擇字段成員,反射模式可以不必配置
              /// 
              [fastCSharp.setup.cSharp.serialize(Filter = fastCSharp.setup.memberFilter.InstanceField)]
              public partial class model1
              {
              }
           代碼生成模式將實現(xiàn)接口fastCSharp.setup.cSharp.serialize.ISerialize
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
          20
          21
          22
          23
          24
          25
          26
          27
          28
          29
          30
          31
          32
          33
          34
          35
          36
          37
          38
                  /// 
                  /// 序列化接口
                  /// 
                  public interface ISerialize
                  {
                      /// 
                      /// 對象序列化
                      /// 
                      /// 序列化數(shù)據(jù)
                      byte[] Serialize();
                      /// 
                      /// 對象序列化
                      /// 
                      ///  數(shù)據(jù)流
                      void Serialize(memoryStream stream);
                      /// 
                      /// 對象序列化
                      /// 
                      ///  對象序列化器
                      void Serialize(dataSerializer serializer);
                      /// 
                      /// 反序列化
                      /// 
                      ///  序列化數(shù)據(jù)
                      bool DeSerialize(byte[] data);
                      /// 
                      /// 反序列化
                      /// 
                      ///  序列化數(shù)據(jù)
                      /// 起始位置
                      /// 結(jié)束位置
                      bool DeSerialize(byte[] data, int startIndex, out int endIndex);
                      /// 
                      /// 反序列化
                      /// 
                      /// 對象反序列化器
                      void DeSerialize(deSerializer deSerializer);
                  }
           如果自定義配置IsStreamSerialize = true,將實現(xiàn)接口fastCSharp.setup.cSharp.serialize.IStreamSerialize
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
                  /// 
                  /// 序列化流接口
                  /// 
                  public interface IStreamSerialize : ISerialize
                  {
                      /// 
                      /// 對象序列化
                      /// 
                      /// 數(shù)據(jù)流
                      void Serialize(Stream stream);
                      /// 
                      /// 對象序列化
                      /// 
                      /// 對象序列化器
                      void Serialize(streamSerializer serializer);
                  }
           反射模式的話,直接調(diào)用反射函數(shù)
          1
          2
          3
          fastCSharp.setup.cSharp.serialize.dataSerialize.Get(value);
          fastCSharp.setup.cSharp.serialize.streamSerialize.Get(value);
          fastCSharp.setup.cSharp.serialize.deSerialize.Get(data);
           
          3、TCP(靜態(tài)方法)調(diào)用代碼生成實例,自定義配置類是fastCSharp.setup.cSharp.tcpCall,支持泛型,支持跨類(只能支持單例),不支持反射模式。下面是示例:
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
          20
          21
          22
          23
          24
          25
          26
          27
          28
          29
          30
          31
          32
          33
          34
          35
          36
              /// 
              /// 序列化流接口
              /// 
              [fastCSharp.setup.cSharp.tcpCall(Service = "tcpCallVerifyAsynchronous", VerifyType = typeof(tcpVerifyAsynchronous))]
              public partial class tcpCallVerifyAsynchronous
              {
                  [fastCSharp.setup.cSharp.tcpCall(IsClientAsynchronous = false)]
                  protected static void action()
                  {
                      isServer = true;
                  }
                  [fastCSharp.setup.cSharp.tcpCall(IsClientAsynchronous = true)]
                  protected static void actionAsynchronous()
                  {
                      isServer = true;
                  }
                  [fastCSharp.setup.testCase]
                  internal static bool Test()
                  {
                      using (fastCSharp.testCase.tcpServer.tcpCallVerifyAsynchronous server = new fastCSharp.testCase.tcpServer.tcpCallVerifyAsynchronous())
                      {
                          if (!server.Start()) throw new Exception("tcpCallVerifyAsynchronous");
                          if (fastCSharp.testCase.tcpClient.tcpCallVerifyAsynchronous.defaultTcpServer.IsServer) throw new Exception("IsServer");
           
                          isServer = false;
                          tcpCall.tcpCallVerifyAsynchronous.action();
                          checkServer("action");
           
                          isServer = false;
                          isReturn = null;
                          tcpCall.tcpCallVerifyAsynchronous.actionAsynchronous(actionAsynchronousReturn);
                          checkIsReturn("actionAsynchronous");
                      }
                      return true;
                  }
              }
           
          4、TCP(動態(tài)方法)服務(wù)代碼生成實例,自定義配置類是fastCSharp.setup.cSharp.tcpServer,支持泛型,不支持跨類,不支持反射模式。下面是示例:
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
          20
          21
          22
          23
          24
          25
          26
          27
          28
          29
          30
          31
          32
          33
          34
              [fastCSharp.setup.cSharp.tcpServer(Service = "tcpServerVerifyAsynchronous", VerifyType = typeof(tcpVerifyAsynchronous))]
              public partial class tcpServerVerifyAsynchronous
              {
                  [fastCSharp.setup.cSharp.tcpServer(IsClientAsynchronous = false)]
                  protected void action()
                  {
                      isServer = true;
                  }
                  [fastCSharp.setup.cSharp.tcpServer(IsClientAsynchronous = true)]
                  protected void actionAsynchronous()
                  {
                      isServer = true;
                  }
                  [fastCSharp.setup.testCase]
                  internal static bool Test()
                  {
                      using (fastCSharp.testCase.tcpServer.tcpServerVerifyAsynchronous server = new fastCSharp.testCase.tcpServer.tcpServerVerifyAsynchronous())
                      {
                          if (!server.Start()) throw new Exception("tcpServerVerifyAsynchronous");
                          using (fastCSharp.testCase.tcpClient.tcpServerVerifyAsynchronous client = new fastCSharp.testCase.tcpClient.tcpServerVerifyAsynchronous())
                          {
                              isServer = false;
                              client.action();
                              checkServer("action");
           
                              isServer = false;
                              isReturn = null;
                              client.actionAsynchronous(actionAsynchronousReturn);
                              checkIsReturn("actionAsynchronous");
                          }
                      }
                      return true;
                  }
              }
          與TCP(靜態(tài)方法)調(diào)用代碼生成實例相似,可以參考一下fastCSharp.setup.tcpRegister。
          5、快速json處理代碼生成實例,自定義配置類是fastCSharp.setup.cSharp.ajax,同時支持反射模式。
          1
          2
          3
          4
          5
          6
          7
              /// 
              /// 可以選擇只生成 序列化 或者 反序列化 的代碼,反射模式可以不必配置
              /// 
              [fastCSharp.setup.cSharp.ajax(IsToJson = true, IsParseJson = true)]
              public partial class model1
              {
              }
          代碼生成模式將實現(xiàn)接口fastCSharp.setup.cSharp.ajax.IToJson與fastCSharp.setup.cSharp.ajax.IParseJson
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
          20
          21
          22
          23
          24
          25
          26
          27
          28
          29
          30
          31
          32
                  /// 
                  /// 對象轉(zhuǎn)換成JSON字符串接口
                  /// 
                  public interface IToJson
                  {
                      /// 
                      /// 對象轉(zhuǎn)換成JSON字符串
                      /// 
                      /// JSON字符串
                      string ToJson();
                      /// 
                      /// 對象轉(zhuǎn)換成JSON字符串
                      /// 
                      /// 對象轉(zhuǎn)換JSON字符串
                      void ToJson(toJsoner toJsoner);
                  }
                  /// 
                  /// JSON字符串轉(zhuǎn)換成對象接口
                  /// 
                  public interface IParseJson
                  {
                      /// 
                      /// JSON字符串轉(zhuǎn)換成對象
                      /// 
                      /// JSON字符串
                      void FromJson(string json);
                      /// 
                      /// JSON字符串解析節(jié)點換成對象
                      /// 
                      /// JSON字符串解析節(jié)點
                      void FromJson(jsonNode node);
                  }
          反射模式的話,直接調(diào)用反射函數(shù)
          1
          2
          fastCSharp.setup.cSharp.ajax.toJson.Get(value);
          fastCSharp.setup.cSharp.ajax.parseJson.Get(json);
           
          由于很多人不需要代碼生成那么好的運行效率,也不想配置初始環(huán)境,所以某些代碼生成實例提供了基于反射的實例實現(xiàn)。有的代碼生成實例需要生成代理實例(比如TCP調(diào)用),沒有辦法使用反射實現(xiàn)相同的效果。
          今天是我在現(xiàn)在這個公司工作的最后一天了,感謝李陶冶大牛這幾年對我的照顧,感謝你對于這個項目開源發(fā)展的支持。也許過幾天我就回老家“休息”了,在這里默默的祝福你和51nod能夠一切順利。
          最后歡迎對算法有興趣的朋友到基于fastCSharp開發(fā)的51nod OJ上去AC問題或者討論算法問題。
          瀏覽 22
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          編輯 分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          編輯 分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  www亚洲色 | 成人无码五月天 | 99精品视频免费看 | 天堂在线免费视屏 | 天天操天天摸天天干 |