Example #1
0
        /// <summary>为数据模型创建实体程序集,无缓存</summary>
        /// <param name="name">程序集名</param>
        /// <param name="connName">连接名</param>
        /// <param name="tables">模型表</param>
        /// <returns></returns>
        public static EntityAssembly Create(String name, String connName, List <IDataTable> tables)
        {
            if (String.IsNullOrEmpty(name))
            {
                return(null);
            }
            if (tables == null)
            {
                return(null);
            }

            var asm = new EntityAssembly();

            asm.Name     = name;
            asm.ConnName = connName;
            asm.Tables   = tables;
            asm.CreateAll();

            asm.Compile();

            return(asm);
        }
Example #2
0
        /// <summary>为数据模型创建实体程序集,带缓存,依赖于表和字段名称,不依赖名称以外的信息。</summary>
        /// <param name="name"></param>
        /// <param name="tables"></param>
        /// <returns></returns>
        public static EntityAssembly CreateWithCache(String name, List <IDataTable> tables)
        {
            if (String.IsNullOrEmpty(name))
            {
                return(null);
            }
            if (tables == null)
            {
                return(null);
            }

            // 构建缓存Key
            var sb = new StringBuilder();

            sb.Append(name);
            foreach (var item in tables)
            {
                sb.Append("|");
                sb.Append(item.Name);
                foreach (var dc in item.Columns)
                {
                    sb.Append(",");
                    sb.Append(dc.Name);
                }
            }
            var key = DataHelper.Hash(sb.ToString());

            return(cache.GetItem <String, List <IDataTable> >(key, name, tables, (k, n, ts) =>
            {
                var asm = new EntityAssembly();
                asm.Name = n;
                asm.Tables = ts;
                asm.CreateAll();

                asm.Compile();
                return asm;
            }));
        }
Example #3
0
        /// <summary>为数据模型创建实体程序集,带缓存,依赖于表和字段名称,不依赖名称以外的信息。</summary>
        /// <param name="name"></param>
        /// <param name="tables"></param>
        /// <returns></returns>
        public static EntityAssembly CreateWithCache(String name, List<IDataTable> tables)
        {
            if (String.IsNullOrEmpty(name)) return null;
            if (tables == null) return null;

            // 构建缓存Key
            var sb = new StringBuilder();
            sb.Append(name);
            foreach (var item in tables)
            {
                sb.Append("|");
                sb.Append(item.Name);
                foreach (var dc in item.Columns)
                {
                    sb.Append(",");
                    sb.Append(dc.Name);
                }
            }
            var key = DataHelper.Hash(sb.ToString());

            return cache.GetItem<String, List<IDataTable>>(key, name, tables, (k, n, ts) =>
            {
                var asm = new EntityAssembly();
                asm.Name = n;
                asm.Tables = ts;
                asm.CreateAll();

                asm.Compile();
                return asm;
            });
        }
Example #4
0
        /// <summary>为数据模型创建实体程序集,无缓存</summary>
        /// <param name="name">程序集名</param>
        /// <param name="connName">连接名</param>
        /// <param name="tables">模型表</param>
        /// <returns></returns>
        public static EntityAssembly Create(String name, String connName, List<IDataTable> tables)
        {
            if (String.IsNullOrEmpty(name)) return null;
            if (tables == null) return null;

            var asm = new EntityAssembly();
            asm.Name = name;
            asm.ConnName = connName;
            asm.Tables = tables;
            asm.CreateAll();

            asm.Compile();

            return asm;
        }
Example #5
0
        /// <summary>代码生成测试</summary>
        /// <param name="dal"></param>
        public static void CodeTest(DAL dal)
        {
            //XTable table = dal.Tables[0];

            //foreach (XTable item in dal.Tables)
            //{
            //    if (item.Name == "Area")
            //    {
            //        table = item;
            //        break;
            //    }
            //}

            EntityAssembly asm = new EntityAssembly();
            asm.Dal = dal;
            asm.NameSpace = new System.CodeDom.CodeNamespace("XCode.Test.Entities");

            //EntityClass entity = asm.Create(table);
            //entity.Create();
            //entity.AddProperties();
            //entity.AddIndexs();
            //entity.AddNames();

            EntityClass entity = asm.Create("Area");
            String str = entity.GenerateCSharpCode();
            Console.WriteLine(str);

            CompilerResults rs = asm.Compile(null);
            foreach (String item in rs.Output)
            {
                Console.WriteLine(item);
            }

            //asm.CreateAll();

            //str = asm.GenerateCSharpCode();
            ////File.WriteAllText(dal.ConnName + ".cs", str);

            //Console.WriteLine(str);
        }