服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#递归应用之实现JS文件的自动引用

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

2023-06-12 16:22hztech C#

这篇文章主要为大家详细介绍了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<string, string> 了。

2、算法选择

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

代码实现

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

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

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

?
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
/// <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();
            }
 
        }

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

?
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
50
51
/// <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文件引用路径

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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");
                }
            }

最终结果 如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<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>

完事代码如下:

?
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/// <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

延伸 · 阅读

精彩推荐
  • C#Unity3D使用Shader实现腐蚀消失

    Unity3D使用Shader实现腐蚀消失

    这篇文章主要为大家详细介绍了Unity3D使用Shader实现腐蚀消失,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    星空不语5142022-07-08
  • C#C#调用百度API实现活体检测的方法

    C#调用百度API实现活体检测的方法

    这篇文章主要给大家介绍了关于C#调用百度API实现活体检测的方法,文中通过示例代码介绍的非常详细,对大家学习或者使用C#具有一定的参考学习价值,需...

    三人行工作室10222022-08-05
  • C#C# 连接本地数据库的实现示例

    C# 连接本地数据库的实现示例

    本文主要介绍了C# 连接本地数据库的实现示例,文中根据实例编码详细介绍的十分详尽,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    Morris_6612023-02-23
  • C#C# WPF 自定义按钮的方法

    C# WPF 自定义按钮的方法

    这篇文章主要介绍了C# WPF 自定义按钮的方法,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下...

    louzi5022022-11-04
  • C#JWT.net 操作实践方法

    JWT.net 操作实践方法

    下面小编就为大家分享一篇JWT.net 操作实践方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    星辉Johnson7522022-02-16
  • C#C#实现关闭子窗口而不释放子窗口对象的方法

    C#实现关闭子窗口而不释放子窗口对象的方法

    下面小编就为大家带来一篇C#实现关闭子窗口而不释放子窗口对象的方法 。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来...

    C#教程网12232021-12-20
  • C#C#实现的封装CURD到SqlHelper类用法简单分析

    C#实现的封装CURD到SqlHelper类用法简单分析

    这篇文章主要介绍了C#实现的封装CURD到SqlHelper类用法,涉及数据库相关配置方法及SqlHelper类的简单使用技巧,代码中包含了较为详尽的注释便于理解,需要的朋...

    _Vincent9792022-02-10
  • C#UGUI实现随意调整Text中的字体间距

    UGUI实现随意调整Text中的字体间距

    这篇文章主要为大家详细介绍了UGUI实现随意调整字体间距的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    feiyuezouni4692022-07-09