• CodeGenerator.cs


    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;
            }
        }
    }
    

      

  • 相关阅读:
    章节1:SQL语言简易入门
    章节0:MySQl学前知识储备
    iOS 设置导航栏全透明
    IOS修改Navigation Bar上的返回按钮文本颜色,箭头颜色以及导航栏按钮的颜色
    iOS import导入时没有提示的解决办法
    iOSAPP开发项目搭建
    如何搭建iOS项目基本框架
    UIWebView中JS与OC交互 WebViewJavascriptBridge的使用
    iOS概念之KVO(Key-Value Observing)
    oc调javascript方法(evaluateJavaScript:)&&js给oc发通知
  • 原文地址:https://www.cnblogs.com/bober/p/3892460.html
Copyright © 2020-2023  润新知