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

推荐订阅源

IT之家
IT之家
Recent Announcements
Recent Announcements
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The GitHub Blog
The GitHub Blog
MyScale Blog
MyScale Blog
爱范儿
爱范儿
GbyAI
GbyAI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
美团技术团队
Y
Y Combinator Blog
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
Martin Fowler
Martin Fowler
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
M
MIT News - Artificial intelligence
博客园 - Franky
V
Visual Studio Blog
I
InfoQ
V
V2EX
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
博客园 - 司徒正美
L
LangChain 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;
        }
    }
}