惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
Forbes - Security
Forbes - Security
WordPress大学
WordPress大学
P
Proofpoint News Feed
T
Threat Research - Cisco Blogs
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
Spread Privacy
Spread Privacy
D
Darknet – Hacking Tools, Hacker News & Cyber Security
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
P
Privacy International News Feed
A
About on SuperTechFans
T
Tailwind CSS Blog
I
InfoQ
S
Securelist
云风的 BLOG
云风的 BLOG
罗磊的独立博客
Recent Announcements
Recent Announcements
T
The Exploit Database - CXSecurity.com
B
Blog RSS Feed
V
Visual Studio Blog
Know Your Adversary
Know Your Adversary
The GitHub Blog
The GitHub Blog
Jina AI
Jina AI
腾讯CDC
Cyberwarzone
Cyberwarzone
有赞技术团队
有赞技术团队
AWS News Blog
AWS News Blog
博客园 - 【当耐特】
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
F
Full Disclosure
S
Secure Thoughts
博客园 - 司徒正美
J
Java Code Geeks
Y
Y Combinator Blog
Google Online Security Blog
Google Online Security Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
Help Net Security
Help Net Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Project Zero
Project Zero
T
Tenable Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tor Project blog
MyScale Blog
MyScale Blog
Scott Helme
Scott Helme
小众软件
小众软件
K
Kaspersky official blog

博客园 - 大力哥的技术

多边形效果 完成卸载vs2010后再安装 图片集合,可用作商品列表 无可奈何花落去 Uncaught TypeError: Cannot read property 'msie' of undefined 年月日控件 SQL GETDATE()日期格式化函数 股票操作要点 用户中心 - 博客园 京东分页 相册分类列表页 在线运行Javascript,Jquery,HTML,CSS代码 点击事件后动画提示 近舍网临时笔记 一些广告代码 爱可有—之最经典 爱可有网络社区需要定义 鼠标移动时缩小图片显示说明 一些正则验证-JS
CodeGenerator.cs
大力哥的技术 · 2014-08-05 · via 博客园 - 大力哥的技术
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CiCeng.EntityCodeGenerator.Lib
{
    //code = CodeGenerator.GeneratorBussinessCode(t, namespaceName);
    //filePath = Path.Combine(folderPath, "B"+CodeGenerator.GetPascalName(t) + ".cs");
    //File.WriteAllText(filePath, code);

    //code = CodeGenerator.GeneratorDataAccessCode(t, namespaceName);
    //filePath = Path.Combine(folderPath, CodeGenerator.GetPascalName(t) + "DA.cs");
    //File.WriteAllText(filePath, code);
    
    //textBoxNamespace.Text = "AiCareYou.Entity";
    //textBoxServer.Text="116.255.226.20,1435";
    //textBoxUserID.Text = "s517030db0";
    //textBoxPwd.Text ="songbo920bober";

    public static class CodeGenerator
    {
        public static string Generate(string connKey, string catalog, string tableName, List<Column> columns, string namespaceName, string prefix, string suffix, bool serializable, bool wcf, string commonNamespace)
        {
            StringBuilder content = new StringBuilder();
            content.Append(@"using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Runtime.Serialization;
");
            if (wcf)
            {
                content.Append(@"
using System.Runtime.Serialization;");
            }

            if (commonNamespace == null || commonNamespace.Trim().Length <= 0)
            {
//                content.Append(@"
//
//using NeweggSoft.Lib.DataPersister;");
            }
            else
            {
                content.AppendFormat(@"

using {0};", commonNamespace);
            }
            content.Append(@"

");
            content.AppendFormat("namespace {0}.Entity", namespaceName);
            content.Append(@"
{
    ");
            //if (catalog == null || catalog == string.Empty)
            //{
            //    content.AppendFormat("[Table(\"{0}\"){1}{2}]", tableName, (serializable ? ", Serializable" : string.Empty), (wcf ? ", DataContract" : string.Empty));
            //}
            //else if (connKey == null || connKey == string.Empty)
            //{
            //    content.AppendFormat("[Table(\"{0}\", \"{1}\"){2}{3}]", tableName, catalog, (serializable ? ", Serializable" : string.Empty), (wcf ? ", DataContract" : string.Empty));
            //}
            //else
            //{
            //    content.AppendFormat("[Table(\"{0}\", \"{1}\", \"{2}\"){3}{4}]", tableName, catalog, connKey, (serializable ? ", Serializable" : string.Empty), (wcf ? ", DataContract" : string.Empty));
            //}
            //content.Append(@"[Serializable]");
            //content.Append(@"
    //");
    //        content.Append(@"[DataContract]");
    //        content.Append(@"
   // ");
            content.AppendFormat("public partial class E{0}", prefix + GetPascalName(tableName) + suffix);
            content.Append(@"
    {
        ");

            if (columns != null && columns.Count > 0)
            {
                int i = 0;
                foreach (Column column in columns)
                {
                    if (i > 0)
                    {
                        content.Append(@"

        ");
                    }
                    //content.AppendFormat("[Field(\"{0}\", SqlDbType.{1}", column.DbName, column.DbType.ToString());
                    //if (column.IsPrimaryKey)
                    //{
                    //    content.AppendFormat(", PKType.{0}", column.IsAutoIncrease ? "AutoIncrease" : (column.DataType == typeof(Guid) ? "Guid" : "ExclusiveValue"));
                    //}
                    //content.AppendFormat("){0}]", (wcf ? ", DataMember" : string.Empty));
//                    content.AppendFormat("[DataMember]");
//                    content.Append(@"
//        ");
                    content.AppendFormat("public {0} {1}", GetAbbreviateTypeName(column.DataType), GetPascalName(column.DbName));
                    content.Append(@" { get; set; }");
                    i++;
                }
            }

            content.Append(@"
    }
}

");
            return content.ToString();
        }

        public static string GeneratorDataAccessCode(string tableName, string namespaceName)
        {

            StringBuilder content = new StringBuilder();
            content.AppendFormat(@"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CiCeng.Utility;
using CiCeng.Utility.DataAccess;

using {0}.Entity;
", namespaceName);

            content.Append(@"

");
            content.AppendFormat("namespace {0}.SqlDataAccess", namespaceName);
            content.Append(@"
{
    ");
            content.AppendFormat("public class {0}DA", tableName);
            content.Append(@"
    {
        ");

            content.AppendFormat("public static {0} Load(int sysNo)", tableName);
            content.Append(@"
        {
        ");
            content.AppendFormat("  DataCommand dataCommand = DataCommandManager.GetDataCommand({1}{0}_Load{0}{1});", tableName, '"');
            content.Append(@"
        ");
            content.AppendFormat("  dataCommand.SetParameterValue({0}SysNo{0}, sysNo);", '"');
            content.Append(@"
        ");
            content.AppendFormat("  return dataCommand.ExecuteEntity<{0}>();", tableName);
            content.Append(@"
        }

        ");

            content.AppendFormat("public static {0} Create({0} item)", tableName);
            content.Append(@"
        {
        ");
            content.AppendFormat("  DataCommand dc = DataCommandManager.GetDataCommand({1}{0}_Create{0}{1});", tableName, '"');
            content.Append(@"
        ");
            content.AppendFormat("  dc.SetParameterValue<{0}>(item);", tableName);
            content.Append(@"
        ");
            content.Append(@"   dc.ExecuteNonQuery();
        ");
            content.AppendFormat("  item.SysNo = (int)dc.GetParameterValue({0}@SysNo{0});", '"');
            content.Append(@"
            return item;
        }

        ");

            content.AppendFormat("public static void Update({0} entity)", tableName);
            content.Append(@"
        {
        ");
            content.AppendFormat("  DataCommand dc = DataCommandManager.GetDataCommand({1}{0}_Update{0}{1});", tableName, '"');
            content.Append(@"
            dc.SetParameterValue(entity);
            dc.ExecuteNonQuery();
        }

");

            content.AppendFormat("public static QueryResult<{0}> GetPager{0}(QueryEntityInfo queryInfo)", tableName);
            content.Append(@"
        {
        ");
            content.AppendFormat("var cmd = DataCommandManager.CreateCustomDataCommandFromConfig({1}{0}_GetPager{1});", tableName, '"');

            content.Append(@"
            PagingInfoEntity page = new PagingInfoEntity();
            page.SortField = string.Empty;
            page.MaximumRows = pageSize;
            page.StartRowIndex = pageSize * (pager-1);
            ");
            content.AppendFormat("using (var sqlBuilder = new DynamicQuerySqlBuilder(cmd.CommandText, cmd, page, {1}A.SysNo DESC{1}))", tableName, '"');
            content.Append(@"
            {
        ");
            content.AppendFormat("sqlBuilder.ConditionConstructor.AddCondition(QueryConditionRelationType.AND, {1}A.Status{1}, DbType.String, {1}@Status{1}, QueryConditionOperatorType.Equal, show);", tableName, '"');
            content.Append(@"
                if(category!=0)
        ");

            content.AppendFormat("sqlBuilder.ConditionConstructor.AddCondition(QueryConditionRelationType.AND,{1}A.CategoryID{1}, DbType.String, {1}@CategoryID{1}, QueryConditionOperatorType.Equal, category);", tableName, '"');
//            content.AppendFormat(@"
//                cmd.CommandText = sqlBuilder.BuildQuerySql();
//                var dt = cmd.ExecuteDataTable();
            content.Append(@"

        ");
            content.AppendFormat("List<{0}> orderList = dataCommand.ExecuteEntityList<{0}>();", tableName);

            content.Append(@"

        ");
            content.AppendFormat("int totalCount = Convert.ToInt32(dataCommand.GetParameterValue({1}@TotalCount{1}));", tableName, '"');
            content.Append(@"

        ");
            content.Append(@"int pageIndex = queryInfo.PagingInfo.PageIndex;

            if ((pageIndex * queryInfo.PagingInfo.PageSize) > totalCount)
            {
                if (totalCount != 0 && (totalCount % queryInfo.PagingInfo.PageSize) == 0)
                {
                    pageIndex = totalCount / queryInfo.PagingInfo.PageSize;
                }
                else
                {
                    pageIndex = totalCount / queryInfo.PagingInfo.PageSize + 1;
                }
            }
");
            content.AppendFormat("QueryResult<{0}> result = new QueryResult<{0}>();", tableName);
            content.Append(@"

        ");
            content.Append(@"result.ResultList = result;
            result.PageInfo = new PageInfo();
            result.PageInfo.TotalCount = totalCount;
            result.PageInfo.PageIndex = pageIndex;
            result.PageInfo.PageSize = queryInfo.PagingInfo.PageSize;
            result.PageInfo.SortBy = queryInfo.PagingInfo.SortBy;

            return result;
        }
    }
}

");






//            content.AppendFormat("totalCount = Convert.ToInt32(cmd.GetParameterValue({0}@TotalCount{0}));", '"');
//            content.Append(@"
//        ");
//            content.AppendFormat("List<E{0}> list = new List<E{0}>();", tableName);
//            content.Append(@"
//                if (dt != null && dt.Rows.Count > 0)
//                {
//                    foreach (DataRow dr in dt.Rows)
//                    {
//");
//            content.AppendFormat("list.Add(DataMapper.GetEntity<E{0}>(dr));", tableName);
//            content.Append(@"
//                    }
//                    return list;
//                }
//                return null;
//            }
//        }
//    }
//}
//        ");

            return content.ToString();
        }

        public static string GeneratorBussinessCode(string tableName, string namespaceName)
        {
            StringBuilder content = new StringBuilder();
            content.Append(@"using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AiCareYou.SqlDataAccess;
using AiCareYou.Entity;
using System.Data;
using System.Net;
using System.IO;


");
            content.AppendFormat("namespace {0}.Facade", namespaceName);
            content.Append(@"
{
    ");
            content.AppendFormat("public class {0}Facade", tableName);
            content.Append(@"
    {
        ");
            //            content.AppendFormat("private {0}DA da = new {0}DA();", tableName); content.Append(@"
            //
            //        ");
            content.AppendFormat("  public static {0} Load(int sysNo)", tableName);
            content.Append(@"
        {
    ");
            content.AppendFormat("         return {0}DA.Load(sysNo);", tableName);
            content.Append(@"
         }
    ");


            content.AppendFormat("  public static {0} Create({0} item)", tableName);
            content.Append(@"
        {
    ");
            content.AppendFormat("         return {0}DA.Create(item);", tableName);
            content.Append(@"
        }
    ");


            content.AppendFormat("  public static void Update({0} item)", tableName);
            content.Append(@"
        {
    ");
            content.AppendFormat("         {0}DA.Update(item);", tableName);
            content.Append(@"
        }
    ");
            content.AppendFormat("  public static PagedResult<{0}> GetPgaed{0}()", tableName);
            content.Append(@"
        {
    ");
            content.AppendFormat("         return {0}DA.GetGetPgaed{0}();", tableName);
            content.Append(@"
        }
    ");

            content.AppendFormat("  public static QueryResult<{0}> GetQuery{0}()", tableName);
            content.Append(@"
        {
    ");
            content.AppendFormat("         return {0}DA.GetQuery{0}();", tableName);
            content.Append(@"
        }
    ");

            content.Append(@"
        }
    }
}
        ");

            return content.ToString();
        }
        
        private static string GetAbbreviateTypeName(Type type)
        {
            if (type == typeof(void))
            {
                return "void";
            }
            if (type == typeof(int))
            {
                return "int?";
            }
            if (type == typeof(string))
            {
                return "string";
            }
            if (type == typeof(object))
            {
                return "object";
            }
            if (type == typeof(decimal))
            {
                return "decimal?";
            }
            if (type == typeof(float))
            {
                return "float?";
            }
            if (type == typeof(double))
            {
                return "double?";
            }
            if (type == typeof(sbyte))
            {
                return "sbyte?";
            }
            if (type == typeof(sbyte[]))
            {
                return "sbyte[]";
            }
            if (type == typeof(byte))
            {
                return "byte?";
            }
            if (type == typeof(byte[]))
            {
                return "byte[]";
            }
            if (type == typeof(short))
            {
                return "short?";
            }
            if (type == typeof(ushort))
            {
                return "ushort?";
            }
            if (type == typeof(uint))
            {
                return "uint?";
            }
            if (type == typeof(long))
            {
                return "long?";
            }
            if (type == typeof(ulong))
            {
                return "ulong?";
            }
            if (type == typeof(char))
            {
                return "char?";
            }
            if (type == typeof(bool))
            {
                return "bool?";
            }
            string name = type.Name;
            return type.IsValueType ? name + "?" : name;
        }

        public static string GetPascalName(string name)
        {
            if ((int)name[0] > (int)'Z')
            {
                name = name[0].ToString().ToUpper() + name.Substring(1);
            }
            return name;
        }
    }
}