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;
}
}
}
CodeGenerator.cs的更多相关文章
- WCF3.5 SP1 参考源码索引
http://www.projky.com/dotnet/WCF3.5SP1/Microsoft/InfoCards/AccessibilityApplicationManager.cs.htmlht ...
- .NET 2.0 参考源码索引
http://www.projky.com/dotnet/2.0/Microsoft/CSharp/csharpcodeprovider.cs.htmlhttp://www.projky.com/do ...
- [C#] 剖析 AssemblyInfo.cs - 了解常用的特性 Attribute
剖析 AssemblyInfo.cs - 了解常用的特性 Attribute [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5944391.html 序 ...
- Atitit 软件架构方法的进化与演进cs bs soa roa msa attilax总结
Atitit 软件架构方法的进化与演进cs bs soa roa msa attilax总结 1.1. 软件体系架构是沿着单机到 CS 架构,再到 BS 的三层架构甚至多层架构逐步发展过来的,关于 ...
- 从java文件和CS文件里查询方法使用次数工具
前几天,领导让我找一下老系统(Java)里getRemoteUser方法都哪个文件用了,package是什么,方法被调用了多少次,当时因为着急,所以,直接人工找的,但是以后要是再出现,人工找就太讨厌了 ...
- 关于 WP 开发中.xaml 与.xaml.cs 的关系
今天我们先来看一下在WP8.1开发中最长见到的几个文件之间的关系.比较论证,在看这个问题之前我们简单看看.NET平台其他两个不同的框架: Windows Forms 先看看Window Forms中的 ...
- .net 用户控件ascx.cs注册js脚本代码无效果
在.net web项目中碰到一个比较奇怪的问题,网上没找到解决方案,先自己mark一下 问题描述: 添加一个用户控件ascx,在后端.cs添加js注册脚本,执行后没有弹出框 注册脚本为: this.P ...
- DateHelper.cs日期时间操作辅助类C#
//==================================================================== //** Copyright © classbao.com ...
- 仅用aspx文件实现Ajax调用后台cs程序。(实例)
仅用aspx文件实现Ajax调用后台cs无刷新程序.(实例) 两个文件:aaa.aspx 和aaa.aspx.cs 一.aaa.aspx <script type="text/java ...
随机推荐
- js 使某个页面不允许在子iframe中打开的解决办法
在页面中添加如下js代码<script> if (window.parent !== window.self) { window.parent.location.reload(); }&l ...
- 【开源项目SugarSite】ASP.NET MVC+ Layui+ SqlSugar+RestSharp项目讲解
SugarSite一个前端支持移动端的企业网站,目前只支持了简单功能,后续还会加上论坛等. 源码GIT地址: https://github.com/sunkaixuan/SugarSite 技术介绍 ...
- Nexus Repository Manager OSS 代理 p2 源
用maven管理构建Eclipse RCP项目时,可能会用到p2源: http://download.eclipse.org/releases/mars/ 内网用户肯定希望能通过nexus服务器代理, ...
- WPF学习之路(一) 初识WPF
参考<葵花宝典-WPF自学手册> VS2012 先创建第一个WPF小程序 1.创建WPF程序 2.查看Solution,WPF中xaml文件和cs文件经常成对出现 两个主要的类:APP(W ...
- sql server使用中遇到的问题记录
一.sql server 不能连接远程服务器,但可以连接本地的数据库 我目前用的是sql server 2012 sp1,用着用着突然就不能连接远程服务器上的数据库了,崩溃了一天... 修复试了,卸载 ...
- [20140117]疑似checkpoint堵塞数据库连接
注:这个说法是不成立的,问题已经解决,但是无法正确的定位到具体什么原因:[20140702]奇怪的应用程序超时 背景: 开发通过应用程序的日志发现间歇性的出现,数据库连接超时 原因: 只能大概猜测,没 ...
- 编写Java应用程序,定义Animal类,此类中有动物的属性:名称 name,腿的数量legs,统计动物的数量 count;方法:设置动物腿数量的方法 void setLegs(),获得腿数量的方法 getLegs(),设置动物名称的方法 setKind(),获得动物名称的方法 getKind(),获得动物数量的方法 getCount()。定义Fish类,是Animal类的子类,统计鱼的数量 co
package com.hanqi.test; public class Animal { private String name; private int legs; private int cou ...
- CentOS 7 网卡子接口的创建
OS:CentOS 7 在linux上创建vlan需要加载802.1q模块: 1.检测OS是否已经加载802.1q模块 [root@controller ~]# modinfo 8021q filen ...
- linux批量删除进程
在虚拟机用脚本跑了几十个client程序用来测试服务器,然后发现参数设置错误,得重来,就傻眼了,不知道怎么关这么多client进程,总不能一个一个关.还好,学习一下,想出了以下的命令. ps -e ...
- VBA宏 合并EXCEL
1.合并多个Excel工作簿 Sub MergeWorkbooks() Dim FileSet Dim i As Integer Application.ScreenUpdating = False ...