C#遞歸應用之實現JS文件的自動引用
背景
兩張表,分別是 :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類型
/// <summary> /// define the gloable parameter to save the rel obj data /// </summary> public static Dictionary<string, string> <strong>deepRef </strong>= new Dictionary<string, string>();
入口函數,完成準備工作,(數據庫連接,參數準備)
/// <summary> /// 通過表的字段 獲取相關的引用字段依賴的對象 直到沒有依賴為止 /// </summary> /// <param name="headField">表字段列表</param> /// <returns></returns> public static Dictionary<string,string> getRelRef(List<IniItemsTableItem> headField) { HashSet<string> refset = new HashSet<string>(); // HashSet<string> refset_result = new HashSet<string>(); 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<string, string>();//clear the relation obj data getRef(conn, refset); } return deepRef; } catch (Exception) { throw; } finally { if (conn.State == ConnectionState.Open) conn.Close(); } }
遞歸函數,最終完成在數據庫中獲取字段依賴的對象的獲取
/// <summary> /// get the relation obj /// </summary> /// <param name="conn"></param> /// <param name="ref_field"></param> /// <returns></returns> public static HashSet<string> <strong>getRef</strong>(SqlConnection conn, HashSet<string> ref_field) { HashSet<string> refset = new HashSet<string>(); 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(!<strong>deepRef</strong>.ContainsKey(vt)) <strong>deepRef</strong>.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<string,string> 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"); } }
最終結果 如下:
<script src="../../dept/demoApp/scripts/Sprice/SpriceE_spriceGridfield.js" type="text/javascript"></script> <script src="../../dept/demoApp/scripts/Org/OrgE_orgDialog.js" type="text/javascript"></script> <script src="../../dept/demoApp/scripts/Supplier/SupplierOa_supplierDialog.js" type="text/javascript"></script> <script src="../../dept/demoApp/scripts/Supplier/SupplierOa_supplierField.js" type="text/javascript"></script> <script src="../../dept/demoApp/scripts/Unit/UnitE_unitDialog.js" type="text/javascript"></script> <script src="../../dept/demoApp/scripts/Unit/UnitE_unitField.js" type="text/javascript"></script> <script src="../../dept/demoApp/scripts/Wh/WhWh_whDialog.js" type="text/javascript"></script> <script src="../../dept/demoApp/scripts/Wh/WhWh_whField.js" type="text/javascript"></script> <script src="../../dept/demoApp/scripts/Mold/MoldP_moldDialog.js" type="text/javascript"></script> <script src="../../dept/demoApp/scripts/Mold/MoldP_moldField.js" type="text/javascript"></script> <script src="../../dept/demoApp/scripts/Suppliercategory/SuppliercategoryOa_suppliercategoryDialog.js" type="text/javascript"></script> <script src="../../dept/demoApp/scripts/Suppliercategory/SuppliercategoryOa_suppliercategoryField.js" type="text/javascript"></script> <script src="../../dept/demoApp/scripts/Bank/BankOa_bankDialog.js" type="text/javascript"></script> <script src="../../dept/demoApp/scripts/Bank/BankOa_bankField.js" type="text/javascript"></script> <script src="../../dept/demoApp/scripts/Machine/MachineP_machineDialog.js" type="text/javascript"></script> <script src="../../dept/demoApp/scripts/Machine/MachineP_machineField.js" type="text/javascript"></script> <script src="../../dept/demoApp/scripts/Inventory/InventoryE_inventoryDialog.js" type="text/javascript"></script> <script src="../../dept/demoApp/scripts/Basedocment/BasedocmentOa_basedocmentDialog.js" type="text/javascript"></script> <script src="../../dept/demoApp/scripts/Basedocment/BasedocmentOa_basedocmentField.js" type="text/javascript"></script> <script src="../../dept/demoApp/scripts/Basedoctype/BasedoctypeOa_basedoctypeDialog.js" type="text/javascript"></script> <script src="../../dept/demoApp/scripts/Basedoctype/BasedoctypeOa_basedoctypeField.js" type="text/javascript"></script> <script src="../../dept/demoApp/scripts/Tax/TaxE_taxDialog.js" type="text/javascript"></script> <script src="../../dept/demoApp/scripts/Tax/TaxE_taxField.js" type="text/javascript"></script>
完事代碼如下:
/// <summary> /// define the gloable parameter to save the rel obj data /// </summary> public static Dictionary<string, string> deepRef = new Dictionary<string, string>(); /// <summary> /// 通過表的字段 獲取相關的引用字段依賴的對象 直到沒有依賴為止 /// </summary> /// <param name="headField">表字段列表</param> /// <returns></returns> public static Dictionary<string,string> getRelRef(List<IniItemsTableItem> headField) { HashSet<string> refset = new HashSet<string>(); // HashSet<string> refset_result = new HashSet<string>(); 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<string, string>();//clear the relation obj data getRef(conn, refset); } return deepRef; } catch (Exception) { throw; } finally { if (conn.State == ConnectionState.Open) conn.Close(); } } /// <summary> /// get the relation obj /// </summary> /// <param name="conn"></param> /// <param name="ref_field"></param> /// <returns></returns> public static HashSet<string> getRef(SqlConnection conn, HashSet<string> ref_field) { HashSet<string> refset = new HashSet<string>(); 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; }
以上就是C#遞歸應用之實現JS文件的自動引用的詳細內容,更多關於C#遞歸實現JS文件引用的資料請關註WalkonNet其它相關文章!