验证码: 看不清楚,换一张 查询 注册会员,免验证
  • {{ basic.site_slogan }}
  • 打开微信扫一扫,
    您还可以在这里找到我们哟

    关注我们

C#递归应用之如何实现JS文件的自动引用

阅读:1121 来源:乙速云 作者:代码code

C#递归应用之如何实现JS文件的自动引用

      背景

      两张表,分别是 :sys_tbl,和 sys_field,其中:sys_tbl 是系统所有表的信息,包含两个字段 :code(表名),name(表描述信息);sys_fld 是记录第张表中的字段 的名称(field)和描述信息(table) , 

      截图如下:

      sys_tbl

      C#递归应用之如何实现JS文件的自动引用

      C#递归应用之如何实现JS文件的自动引用

      其中,字段 名称包含对其它名称(对象) 的引用,写法为:表名(去除下划线)_引用字段 ,如:einventory_cinvcode,就是对表 e_inventory 中字段 cinvcode的引用。

      每张表都在系统 中对应有不同功能的js文件(模块)如: 编辑窗体(dialog类型),跨域选择窗体(field)类型等

      需求

      在一个综合的页面中,会对其它对象进行引用(依赖),而这种引用最终体现在对js文件的包含。同时被引用的js 文件(一级引用)还有自已关联对象(二级引用).....如此下去,直到N 级引用。

      所以现在需要逐层找到对象的引用,直到最后不依赖任何对象为止,并把这些js文件生成引用包含路径(不重复)

      分析

      1、返回结果类型

      得到这些js引用,不能重复,但是对象之间引用是可以存在交叉的,在整个引用链条上,同一个对象会被 多个对象引用,所以简单的字符串数组是避免不了重复的。但是在C#中的HashSet可以做到不重复,同时这个去重工作是自动完成的,所以存结果如果是简单的value类型,用 HashSet就可以,但是因为字段中表的信息已经被格式化过,所以还要把格式化之前的表名称取出与字段对应,所以需要一个key-value类型的数据 ,那就是 Dictionary 了。

      2、算法选择

      因为查找引用,是层层进行的,而且下一层引用的要用到上一层的引用结果,所以这里选择递归算法

      代码实现

      声明一个全局变量,记录所有的表与字段的对应的数据,因为需要确保数据key 唯一性所以选择Dicctionary类型

      /// 
              /// define the gloable parameter to save the rel obj data
              /// 
              public static Dictionary deepRef = new Dictionary();

      入口函数,完成准备工作,(数据库连接,参数准备)

      /// 
              /// 通过表的字段 获取相关的引用字段依赖的对象 直到没有依赖为止
              /// 
              /// 表字段列表
              /// 
              public static Dictionary  getRelRef(List headField)
              {
                  HashSet refset = new HashSet();
                 // HashSet refset_result = new HashSet();
                  foreach (var item in headField)
                  {
                      if (!item.controlType.StartsWith("hz_"))// is not the field of  reference
                      {
                          continue;
                      }
                      string[] fieldarr = item.dataIndex.Split('_');//the constructor is :[tabble]_[field]
      
                      refset.Add(fieldarr[0]);//the first prefix
      
                  }
                  dataOperate dao = new dataOperate();
                  dao.DBServer = "info";
                  SqlConnection conn = dao.createCon();
                  try
                  {
                      if (refset.Count > 0)
                      {
                          
                          if (conn.State != ConnectionState.Open)
                              conn.Open();//open connection
                          deepRef = new Dictionary();//clear the relation obj data
                          getRef(conn, refset);
                       
      
                      }
                      return deepRef;
                  }
                  catch (Exception)
                  {
      
                      throw;
                  }
                  finally
                  {
                      if (conn.State == ConnectionState.Open)
                          conn.Close();
                  }
      
              }

      递归函数,最终完成在数据库中获取字段依赖的对象的获取

      /// 
              /// get the relation obj 
              /// 
              /// 
              /// 
              /// 
              public static HashSet getRef(SqlConnection conn, HashSet ref_field)
              {
                  HashSet refset = new HashSet();
      
                  string refstr = string.Join("','", ref_field);
                  if (refstr != "")
                  {
                      refstr = "'" + refstr + "'";
      
                      string sql = "SELECT dbo.GetSplitOfIndex(b.[field],'_',1) as refcode,a.[code]  FROM   (  select   replace(code,'_','') as uncode,* from     [SevenWOLDev].[dbo].[sys_tbl_view] )   as a inner join [SevenWOLDev].[dbo].[sys_fld_view]  as b    on a.uncode=dbo.GetSplitOfIndex(b.[field],'_',1)    where replace([table],'_','') in (" + refstr + ") and uitype='ref'";
                      //get dataset relation obj
                      DataSet ds = dataOperate.getDataset(conn, sql);
                      if (ds != null && ds.Tables.Count > 0)
                      {
                          DataTable dt = ds.Tables[0];
                          if (dt.Rows.Count > 0)
                          {
                              //rel ref exists
                              for (int k = 0; k < dt.Rows.Count; k++)
                              {
                                  string vt = dt.Rows[k].ItemArray[0].ToString();
                                   string vv = dt.Rows[k].ItemArray[1].ToString();
                                  refset.Add(vt);//save current ref
                                  if(!deepRef.ContainsKey(vt))
                                      deepRef.Add(vt, vv);// save all ref
      
                                   
                              }
                              if (refset.Count > 0)// get the ref successfully
                              {
                                  //recursion get
                                  getRef(conn, refset);
                              }
      
                          }
      
                      }
      
                  }
                  else
                  { 
                      //no ref
                  }
                  return refset;
              }

      对函数进行调用,并组织出js文件引用路径

      Dictionary deepRef = ExtjsFun.getRelRef(HeadfieldSetup);
                  if (deepRef != null)
                  {
                      foreach (var s in deepRef)
                      {
                          string tem_module = "";
                          tem_module = s.Value;
                          string[] moduleArr = tem_module.Split('_');
                          if (tem_module.IndexOf("_") >= 0)
                              tem_module = moduleArr[1];//module name for instance: p_machine ,the module is “machine” unless not included the underscore
                          string fieldkind = "dialog";
                          jslist.Add(MyComm.getFirstUp(tem_module) + "/" + ExtjsFun.GetJsModuleName(tem_module, s.Value, fieldkind) + ".js");
                          fieldkind = "field";
                          jslist.Add(MyComm.getFirstUp(tem_module) + "/" + ExtjsFun.GetJsModuleName(tem_module, s.Value, fieldkind) + ".js");
                      }
                  }

      最终结果 如下:

      
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            

      完事代码如下:

      /// 
              /// define the gloable parameter to save the rel obj data
              /// 
              public static Dictionary deepRef = new Dictionary();
              /// 
              /// 通过表的字段 获取相关的引用字段依赖的对象 直到没有依赖为止
              /// 
              /// 表字段列表
              /// 
              public static Dictionary  getRelRef(List headField)
              {
                  HashSet refset = new HashSet();
                 // HashSet refset_result = new HashSet();
                  foreach (var item in headField)
                  {
                      if (!item.controlType.StartsWith("hz_"))// is not the field of  reference
                      {
                          continue;
                      }
                      string[] fieldarr = item.dataIndex.Split('_');//the constructor is :[tabble]_[field]
      
                      refset.Add(fieldarr[0]);//the first prefix
      
                  }
                  dataOperate dao = new dataOperate();
                  dao.DBServer = "info";
                  SqlConnection conn = dao.createCon();
                  try
                  {
                      if (refset.Count > 0)
                      {
                          
                          if (conn.State != ConnectionState.Open)
                              conn.Open();//open connection
                          deepRef = new Dictionary();//clear the relation obj data
                          getRef(conn, refset);
                       
      
                      }
                      return deepRef;
                  }
                  catch (Exception)
                  {
      
                      throw;
                  }
                  finally
                  {
                      if (conn.State == ConnectionState.Open)
                          conn.Close();
                  }
      
      
                  
      
              }
             
              /// 
              /// get the relation obj 
              /// 
              /// 
              /// 
              /// 
              public static HashSet getRef(SqlConnection conn, HashSet ref_field)
              {
                  HashSet refset = new HashSet();
      
                  string refstr = string.Join("','", ref_field);
                  if (refstr != "")
                  {
                      refstr = "'" + refstr + "'";
      
                      string sql = "SELECT dbo.GetSplitOfIndex(b.[field],'_',1) as refcode,a.[code]  FROM   (  select   replace(code,'_','') as uncode,* from     [SevenWOLDev].[dbo].[sys_tbl_view] )   as a inner join [SevenWOLDev].[dbo].[sys_fld_view]  as b    on a.uncode=dbo.GetSplitOfIndex(b.[field],'_',1)    where replace([table],'_','') in (" + refstr + ") and uitype='ref'";
                      //get dataset relation obj
                      DataSet ds = dataOperate.getDataset(conn, sql);
                      if (ds != null && ds.Tables.Count > 0)
                      {
                          DataTable dt = ds.Tables[0];
                          if (dt.Rows.Count > 0)
                          {
                              //rel ref exists
                              for (int k = 0; k < dt.Rows.Count; k++)
                              {
                                  string vt = dt.Rows[k].ItemArray[0].ToString();
                                   string vv = dt.Rows[k].ItemArray[1].ToString();
                                  refset.Add(vt);//save current ref
                                  if(!deepRef.ContainsKey(vt))
                                      deepRef.Add(vt, vv);// save all ref
      
                                   
                              }
                              if (refset.Count > 0)// get the ref successfully
                              {
                                  //recursion get
                                  getRef(conn, refset);
                              }
      
                          }
      
                      }
      
      
      
      
                  }
                  else
                  { 
                      //no ref
                  }
                  return refset;
              }
    分享到:
    *特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: hlamps#outlook.com (#换成@)。
    相关文章
    {{ v.title }}
    {{ v.description||(cleanHtml(v.content)).substr(0,100)+'···' }}
    你可能感兴趣
    推荐阅读 更多>