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文件引用的资料请关注猪先飞其它相关文章!
原文出处:https://www.cnblogs.com/hztech/p/17202728.html
相关文章
- 下面小编来给大家演示几个php操作zip文件的实例,我们可以读取zip包中指定文件与删除zip包中指定文件,下面来给大这介绍一下。 从zip压缩文件中提取文件 代...2016-11-25
- 本文主要介绍了C#中new的几种用法,具有很好的参考价值,下面跟着小编一起来看下吧...2020-06-25
- 这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
- 这篇文章主要介绍了C#实现简单的Http请求的方法,以实例形式较为详细的分析了C#实现Http请求的具体方法,需要的朋友可以参考下...2020-06-25
- 这篇文章主要为大家详细介绍了SpringBoot实现excel文件生成和下载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-09
- 这篇文章主要介绍了c# 三种方法调用WebService接口的相关资料,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-07-07
- 要替换字符串中的内容我们只要利用php相关函数,如strstr,str_replace,正则表达式了,那么我们要替换目录所有文件的内容就需要先遍历目录再打开文件再利用上面讲的函数替...2016-11-25
- 又码了一个周末的代码,这次在做一些关于文件上传的东西。(PHP UPLOAD)小有收获项目是一个BT种子列表,用户有权限上传自己的种子,然后配合BT TRACK服务器把种子的信息写出来...2016-11-25
- 我们在使用C#做项目的时候,基本上都需要制作登录界面,那么今天我们就来一步步看看,如果简单的实现登录界面呢,本文给出2个例子,由简入难,希望大家能够喜欢。...2020-06-25
- 本篇文章主要说明的是与php文件上传的相关配置的知识点。PHP文件上传功能配置主要涉及php.ini配置文件中的upload_tmp_dir、upload_max_filesize、post_max_size等选项,下面一一说明。打开php.ini配置文件找到File Upl...2015-10-21
- 举一个案例:复制代码 代码如下:<?phpclass Downfile { function downserver($file_name){$file_path = "./img/".$file_name;//转码,文件名转为gb2312解决中文乱码$file_name = iconv("utf-8","gb2312",$file_name...2014-06-07
- 这篇文章主要介绍了C#中list用法,结合实例形式分析了C#中list排序、运算、转换等常见操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)
这篇文章主要介绍了使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25- 分享给大家php判断上传文件类型的方法,大家一起学习学习。/** * 读取文件前几个字节 判断文件类型 * @return String */ function checkTitle($filename){ $file=fopen($filename, "rb"); $bin=fread($file, 2); /...2015-10-21
nodejs文件操作模块FS(File System)常用函数简明总结
件系统操作相关的函数挺多的。首先可以分为两大类。一类是异步+回调的。 一类是同步的。在这里只对异步的进行整理,同步的只需要在函数名称后面加上Sync即可1. 首先是一类最常规的读写函数,函数名称和形式,应该是起源于C...2014-06-07- 通常php.ini的位置在:复制代码 代码如下:/etc目录下或/usr/local/lib目录下。如果你还是找不到php.ini或者找到了php.ini修改后不生效(其实是没找对),请使用如下办法:1.新建php文件,写入如下代码复制代码 代码如下:<?phpe...2014-05-31
- 共享一段使用PHP下载CSS文件中的图片的代码 复制代码 代码如下: <?php //note 设置PHP超时时间 set_time_limit(0); //note 取得样式文件内容 $styleFileContent = file_get_contents('images/style.css'); //not...2013-10-04
- 引言今天在教别人使用protobuf的时候,无意中发现了一个php cli模式下的诡异问题,费了老半天的找到解决方法了,这里拿出来分享下。问题描述我们这边最先引入了protobuf协议,使用的是allegro/php-protobuf这个扩展安装的。...2015-10-21
- 今天小编在这里就来给photoshop的这一款软件的使用者们来说下AI源文件转photoshop图像变模糊问题的解决教程,各位想知道具体解决方法的使用者们,那么下面就快来跟着小编...2016-09-14