经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
C#递归应用之实现JS文件的自动引用
来源:jb51  时间:2023/3/14 8:46:13  对本文有异议

背景

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

截图如下:

sys_tbl

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

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

需求

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

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

分析

1、返回结果类型

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

2、算法选择

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

代码实现

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

  1. /// <summary>
  2. /// define the gloable parameter to save the rel obj data
  3. /// </summary>
  4. public static Dictionary<string, string> <strong>deepRef </strong>= new Dictionary<string, string>();

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

  1. /// <summary>
  2. /// 通过表的字段 获取相关的引用字段依赖的对象 直到没有依赖为止
  3. /// </summary>
  4. /// <param name="headField">表字段列表</param>
  5. /// <returns></returns>
  6. public static Dictionary<string,string> getRelRef(List<IniItemsTableItem> headField)
  7. {
  8. HashSet<string> refset = new HashSet<string>();
  9. // HashSet<string> refset_result = new HashSet<string>();
  10. foreach (var item in headField)
  11. {
  12. if (!item.controlType.StartsWith("hz_"))// is not the field of reference
  13. {
  14. continue;
  15. }
  16. string[] fieldarr = item.dataIndex.Split('_');//the constructor is :[tabble]_[field]
  17.  
  18. refset.Add(fieldarr[0]);//the first prefix
  19.  
  20. }
  21. dataOperate dao = new dataOperate();
  22. dao.DBServer = "info";
  23. SqlConnection conn = dao.createCon();
  24. try
  25. {
  26. if (refset.Count > 0)
  27. {
  28. if (conn.State != ConnectionState.Open)
  29. conn.Open();//open connection
  30. deepRef = new Dictionary<string, string>();//clear the relation obj data
  31. getRef(conn, refset);
  32.  
  33. }
  34. return deepRef;
  35. }
  36. catch (Exception)
  37. {
  38.  
  39. throw;
  40. }
  41. finally
  42. {
  43. if (conn.State == ConnectionState.Open)
  44. conn.Close();
  45. }
  46.  
  47. }

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

  1. /// <summary>
  2. /// get the relation obj
  3. /// </summary>
  4. /// <param name="conn"></param>
  5. /// <param name="ref_field"></param>
  6. /// <returns></returns>
  7. public static HashSet<string> <strong>getRef</strong>(SqlConnection conn, HashSet<string> ref_field)
  8. {
  9. HashSet<string> refset = new HashSet<string>();
  10.  
  11. string refstr = string.Join("','", ref_field);
  12. if (refstr != "")
  13. {
  14. refstr = "'" + refstr + "'";
  15.  
  16. 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'";
  17. //get dataset relation obj
  18. DataSet ds = dataOperate.getDataset(conn, sql);
  19. if (ds != null && ds.Tables.Count > 0)
  20. {
  21. DataTable dt = ds.Tables[0];
  22. if (dt.Rows.Count > 0)
  23. {
  24. //rel ref exists
  25. for (int k = 0; k < dt.Rows.Count; k++)
  26. {
  27. string vt = dt.Rows[k].ItemArray[0].ToString();
  28. string vv = dt.Rows[k].ItemArray[1].ToString();
  29. refset.Add(vt);//save current ref
  30. if(!<strong>deepRef</strong>.ContainsKey(vt))
  31. <strong>deepRef</strong>.Add(vt, vv);// save all ref
  32.  
  33. }
  34. if (refset.Count > 0)// get the ref successfully
  35. {
  36. //recursion get
  37. getRef(conn, refset);
  38. }
  39.  
  40. }
  41.  
  42. }
  43.  
  44. }
  45. else
  46. {
  47. //no ref
  48. }
  49. return refset;
  50. }

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

  1. Dictionary<string,string> deepRef = ExtjsFun.getRelRef(HeadfieldSetup);
  2. if (deepRef != null)
  3. {
  4. foreach (var s in deepRef)
  5. {
  6. string tem_module = "";
  7. tem_module = s.Value;
  8. string[] moduleArr = tem_module.Split('_');
  9. if (tem_module.IndexOf("_") >= 0)
  10. tem_module = moduleArr[1];//module name for instance: p_machine ,the module is “machine” unless not included the underscore
  11. string fieldkind = "dialog";
  12. jslist.Add(MyComm.getFirstUp(tem_module) + "/" + ExtjsFun.GetJsModuleName(tem_module, s.Value, fieldkind) + ".js");
  13. fieldkind = "field";
  14. jslist.Add(MyComm.getFirstUp(tem_module) + "/" + ExtjsFun.GetJsModuleName(tem_module, s.Value, fieldkind) + ".js");
  15. }
  16. }

最终结果 如下:

  1. <script src="../../dept/demoApp/scripts/Sprice/SpriceE_spriceGridfield.js" type="text/javascript"></script>
  2. <script src="../../dept/demoApp/scripts/Org/OrgE_orgDialog.js" type="text/javascript"></script>
  3. <script src="../../dept/demoApp/scripts/Supplier/SupplierOa_supplierDialog.js" type="text/javascript"></script>
  4. <script src="../../dept/demoApp/scripts/Supplier/SupplierOa_supplierField.js" type="text/javascript"></script>
  5. <script src="../../dept/demoApp/scripts/Unit/UnitE_unitDialog.js" type="text/javascript"></script>
  6. <script src="../../dept/demoApp/scripts/Unit/UnitE_unitField.js" type="text/javascript"></script>
  7. <script src="../../dept/demoApp/scripts/Wh/WhWh_whDialog.js" type="text/javascript"></script>
  8. <script src="../../dept/demoApp/scripts/Wh/WhWh_whField.js" type="text/javascript"></script>
  9. <script src="../../dept/demoApp/scripts/Mold/MoldP_moldDialog.js" type="text/javascript"></script>
  10. <script src="../../dept/demoApp/scripts/Mold/MoldP_moldField.js" type="text/javascript"></script>
  11. <script src="../../dept/demoApp/scripts/Suppliercategory/SuppliercategoryOa_suppliercategoryDialog.js" type="text/javascript"></script>
  12. <script src="../../dept/demoApp/scripts/Suppliercategory/SuppliercategoryOa_suppliercategoryField.js" type="text/javascript"></script>
  13. <script src="../../dept/demoApp/scripts/Bank/BankOa_bankDialog.js" type="text/javascript"></script>
  14. <script src="../../dept/demoApp/scripts/Bank/BankOa_bankField.js" type="text/javascript"></script>
  15. <script src="../../dept/demoApp/scripts/Machine/MachineP_machineDialog.js" type="text/javascript"></script>
  16. <script src="../../dept/demoApp/scripts/Machine/MachineP_machineField.js" type="text/javascript"></script>
  17. <script src="../../dept/demoApp/scripts/Inventory/InventoryE_inventoryDialog.js" type="text/javascript"></script>
  18. <script src="../../dept/demoApp/scripts/Basedocment/BasedocmentOa_basedocmentDialog.js" type="text/javascript"></script>
  19. <script src="../../dept/demoApp/scripts/Basedocment/BasedocmentOa_basedocmentField.js" type="text/javascript"></script>
  20. <script src="../../dept/demoApp/scripts/Basedoctype/BasedoctypeOa_basedoctypeDialog.js" type="text/javascript"></script>
  21. <script src="../../dept/demoApp/scripts/Basedoctype/BasedoctypeOa_basedoctypeField.js" type="text/javascript"></script>
  22. <script src="../../dept/demoApp/scripts/Tax/TaxE_taxDialog.js" type="text/javascript"></script>
  23. <script src="../../dept/demoApp/scripts/Tax/TaxE_taxField.js" type="text/javascript"></script>

完事代码如下:

  1. /// <summary>
  2. /// define the gloable parameter to save the rel obj data
  3. /// </summary>
  4. public static Dictionary<string, string> deepRef = new Dictionary<string, string>();
  5. /// <summary>
  6. /// 通过表的字段 获取相关的引用字段依赖的对象 直到没有依赖为止
  7. /// </summary>
  8. /// <param name="headField">表字段列表</param>
  9. /// <returns></returns>
  10. public static Dictionary<string,string> getRelRef(List<IniItemsTableItem> headField)
  11. {
  12. HashSet<string> refset = new HashSet<string>();
  13. // HashSet<string> refset_result = new HashSet<string>();
  14. foreach (var item in headField)
  15. {
  16. if (!item.controlType.StartsWith("hz_"))// is not the field of reference
  17. {
  18. continue;
  19. }
  20. string[] fieldarr = item.dataIndex.Split('_');//the constructor is :[tabble]_[field]
  21.  
  22. refset.Add(fieldarr[0]);//the first prefix
  23.  
  24. }
  25. dataOperate dao = new dataOperate();
  26. dao.DBServer = "info";
  27. SqlConnection conn = dao.createCon();
  28. try
  29. {
  30. if (refset.Count > 0)
  31. {
  32. if (conn.State != ConnectionState.Open)
  33. conn.Open();//open connection
  34. deepRef = new Dictionary<string, string>();//clear the relation obj data
  35. getRef(conn, refset);
  36.  
  37. }
  38. return deepRef;
  39. }
  40. catch (Exception)
  41. {
  42.  
  43. throw;
  44. }
  45. finally
  46. {
  47. if (conn.State == ConnectionState.Open)
  48. conn.Close();
  49. }
  50.  
  51.  
  52.  
  53. }
  54. /// <summary>
  55. /// get the relation obj
  56. /// </summary>
  57. /// <param name="conn"></param>
  58. /// <param name="ref_field"></param>
  59. /// <returns></returns>
  60. public static HashSet<string> getRef(SqlConnection conn, HashSet<string> ref_field)
  61. {
  62. HashSet<string> refset = new HashSet<string>();
  63.  
  64. string refstr = string.Join("','", ref_field);
  65. if (refstr != "")
  66. {
  67. refstr = "'" + refstr + "'";
  68.  
  69. 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'";
  70. //get dataset relation obj
  71. DataSet ds = dataOperate.getDataset(conn, sql);
  72. if (ds != null && ds.Tables.Count > 0)
  73. {
  74. DataTable dt = ds.Tables[0];
  75. if (dt.Rows.Count > 0)
  76. {
  77. //rel ref exists
  78. for (int k = 0; k < dt.Rows.Count; k++)
  79. {
  80. string vt = dt.Rows[k].ItemArray[0].ToString();
  81. string vv = dt.Rows[k].ItemArray[1].ToString();
  82. refset.Add(vt);//save current ref
  83. if(!deepRef.ContainsKey(vt))
  84. deepRef.Add(vt, vv);// save all ref
  85.  
  86. }
  87. if (refset.Count > 0)// get the ref successfully
  88. {
  89. //recursion get
  90. getRef(conn, refset);
  91. }
  92.  
  93. }
  94.  
  95. }
  96.  
  97.  
  98.  
  99.  
  100. }
  101. else
  102. {
  103. //no ref
  104. }
  105. return refset;
  106. }

以上就是C#递归应用之实现JS文件的自动引用的详细内容,更多关于C#递归实现JS文件引用的资料请关注w3xue其它相关文章!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号