IoRyYBigProject(代码自动生成器)的最终目的:
•当有一个新项目时,只需要通过表创建工具来根据需求创建表,并且生成相对应的类和DLL,然后直接引用到项目中,就把浏览,新增,修改,删除等方法自动生成成功,调用之前写好的底层类和WCF相关的类.剩下的就是做一些此项目独特的需求和增加Web界面或者winform界面了.
更简单的如,一般的项目只需要用界面设计数据库和通过VS所见即所得生成界面,然后再写极少量的代码就可以了.例如新增的Button里面调用代码生成器里面的新增方法.
由于前一段时间看了些linux的东西,此项目完全开源!!!!
我将会再后面的博客中逐渐完善此项目.
2016-1-19:
经过最近几天对此项目的进展,有如下情况.
1,表创建工具的作用仅仅局限于把一些常用表储存起来,直接创建,在表创建工具中创建表是肯定没有直接在 managerment studio中创建表方便的,我在这个工具上肯定干不过微软的商业项目,所以此表创建工具最大的作用是多存几个常用表,然后在这些表上微调.
2,通过最近几天的编码编写,进一步熟悉了泛型,感觉可以熟练应用了,和Linq一样,开窍了.
3,本来打算用类的实例化来在WCF中传送数据,通过思考,觉得越来越没有直接发送sql语句方便,因为类的实例化目前来说限制很大,仅仅是传送几个属性,而类的绑定也需要属性,如果要实例化传送类,必然会带一些属性之外的东西,如果把这些属性之外的东西变成属性,类的绑定又会有问题,然后参考了Linq2Sql,发现他们把表作为DataClasses1DataContext的一个属性处理,这样如果把整个DataClasses1DataContext都实例化传送,又觉得效率太低,所以目前的决定是在WCF传送中,还是加密的SQL语句.
下面是源码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
/************************************************************************************
* 作者 袁东辉 时间:2016-1
* Email windy_23762872@126.com 253625488@qq.com
* 作用 2016BigProject http://blog.csdn.net/yuandonghuia/article/details/50514985
* VS版本 2010 2013
***********************************************************************************/
namespace CreateDataTableTool
{
/// <summary>
/// 这个类是主要的类,生成的工作基本都在此类
/// </summary>
public static class common
{
#region 预设变量
public static string inamespace = "CreateDataTableTool";
public static string calltype = "IoRyClass";
public static string IoRyClassXML = null;
public static string WCFIPport = "";
public static string WebAPIURL="";
#endregion
#region 拼接的字符串 长的字符串都已经用txt的方式保存了,要不会死人的
static string Tou(string tablename)
{
return @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
/************************************************************************************
* 作者 袁东辉 时间:2016-1
* Email windy_23762872@126.com 253625488@qq.com
* 作用 代码生成器生成的View和Table类
* VS版本 2010 2013
***********************************************************************************/
namespace " + inamespace + @"
{
/// <summary>
/// iy前缀(IoRyY缩写)+表名称为类名
/// </summary>
partial class iy" + tablename + @" : IoRyRow
{";
}
static string ViewWei = @"
}
}
}";
#endregion
/// <summary>
/// 主函数,将目标库中所有的表和视图生成类,并且根据选择生成处理类
/// </summary>
/// <param name="iic"></param>
public static void create(IoRyNP.IoRyClass iic)
{
//获取数据库中的表,视图信息.
string sql = @"
select b.name as tablename,a.name as cname,c.name as ctype,COLUMNPROPERTY( a.id,a.name,'IsIdentity') as IsIdentity,
(case when (SELECT count(*) FROM sysobjects
WHERE (name in (SELECT name FROM sysindexes
WHERE (id = a.id) AND (indid in
(SELECT indid FROM sysindexkeys
WHERE (id = a.id) AND (colid in
(SELECT colid FROM syscolumns WHERE (id = a.id) AND (name = a.name)))))))
AND (xtype = 'PK'))>0 then 'true' else 'false' end) ckey,
a.isnullable,b.xtype from syscolumns a
join sysobjects b on a.id=b.id
join systypes c on a.xtype=c.xusertype
where a.id in (select id from sysobjects where xtype in ('U','V'))";
DataTable dt = iic.GetTable(sql);
//创建文件夹
if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "CreateClass"))
{
Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "CreateClass");
}
//取得所有的表,视图名称
var ltnames = dt.AsEnumerable().Select(x => x.Field<string>("tablename")).Distinct();
//遍历所有的表,视图
foreach (var item in ltnames)
{
CreatViewCS(dt, item);
CreatTableCS(dt, item);
}
//写其他的文件
string mystr = "";
switch (calltype)
{
case "IoRyClass":
//写IoRyFunction类
mystr = myRead("IoRyClass\\function1.txt");
mystr = mystr.Replace("CreateDataTableTool", common.inamespace);
mystr += @"
public static string IoRyClassXmlPath = " + "\"" + IoRyClassXML + "\"" + @";
";
mystr += myRead("IoRyClass\\function2.txt");
myWrite(mystr, "IoRyFunction");
mystr = myRead("IoRyClass\\col.txt");
mystr = mystr.Replace("CreateDataTableTool", common.inamespace);
myWrite(mystr, "IoRyCol");
mystr = myRead("IoRyClass\\entity.txt");
mystr = mystr.Replace("CreateDataTableTool", common.inamespace);
myWrite(mystr, "IoRyEntity");
mystr = myRead("IoRyClass\\rowinterface.txt");
mystr = mystr.Replace("CreateDataTableTool", common.inamespace);
myWrite(mystr, "IoRyRowInterface");
//调试的时候先把生成DLL去了,总是影响跟踪代码
//System.IO.File.Copy("IoRyClass\\IoRyClass.dll", "CreateClass\\IoRyClass.dll", true);
//System.IO.File.Copy("IoRyClass\\IoRyClass.dll", "CreateClass\\IoRyClass.xml", true);
System.IO.File.Copy("constring.xml", "CreateClass\\" + IoRyClassXML, true);
break;
case "WCF":
//写IoRyFunction类
mystr = myRead("WCFv3\\function1.txt");
mystr = mystr.Replace("CreateDataTableTool", common.inamespace);
mystr += @"
public static string mxml = " + "\"" + IoRyClassXML + "\";" + @"
public static string url = " + "\"" + WCFIPport + "\";";
mystr += myRead("WCFv3\\function2.txt");
myWrite(mystr, "IoRyFunction");
mystr = myRead("WCFv3\\col.txt");
mystr = mystr.Replace("CreateDataTableTool", common.inamespace);
myWrite(mystr, "IoRyCol");
mystr = myRead("WCFv3\\entity.txt");
mystr = mystr.Replace("CreateDataTableTool", common.inamespace);
myWrite(mystr, "IoRyEntity");
mystr = myRead("WCFv3\\rowinterface.txt");
mystr = mystr.Replace("CreateDataTableTool", common.inamespace);
myWrite(mystr, "IoRyRowInterface");
mystr = myRead("WCFv3\\SortBindingCollection.txt");
mystr = mystr.Replace("CreateDataTableTool", common.inamespace);
myWrite(mystr, "SortBindingCollection");
//调试的时候先把生成DLL去了,总是影响跟踪代码
//System.IO.File.Copy("WCFv3\\IoRyClass.dll", "CreateClass\\IoRyClass.dll", true);
//System.IO.File.Copy("WCFv3\\IoRyWCFClientV3.dll", "CreateClass\\IoRyWCFClientV3.dll", true);
//System.IO.File.Copy("WCFv3\\IoRyClass.dll", "CreateClass\\IoRyClass.xml", true);
//System.IO.File.Copy("WCFv3\\IoRyWCFClientV3.dll", "CreateClass\\IoRyWCFClientV3.xml", true);
break;
case "WebAPI":
break;
}
}
/// <summary>
/// 生成视图文件
/// </summary>
static void CreatViewCS(DataTable dtsql,string tablename)
{
var zidus = dtsql.AsEnumerable().Where(x => x.Field<string>("tablename") == tablename && x.Field<string>("xtype").Contains("V"));
if (zidus.Count() == 0)
{
return;
}
string Zhong = "";
string Zhong2 = @"
/// <summary>
/// 实现IoRyTable的接口
/// </summary>
public void SetData(DataRow dr)
{";
foreach (var ziduitem in zidus)
{
string it = igetctype(ziduitem.Field<string>("ctype"));
Zhong += @"
/// <summary>
/// i前缀+列名为字段名
/// </summary>
public " + it + " i" + ziduitem.Field<string>("cname") + @" { get; set; }
";
Zhong2 += @"
i" + ziduitem.Field<string>("cname") + " = dr.Field<" + it + ">(\"" + ziduitem.Field<string>("cname") + "\");";
}
string lei = Tou(tablename) + Zhong + Zhong2 + ViewWei;
myWrite(lei, "View_" + tablename);
}
/// <summary>
/// 生成表文件
/// </summary>
static void CreatTableCS(DataTable dtsql, string tablename)
{
var zidus = dtsql.AsEnumerable().Where(x => x.Field<string>("tablename") == tablename && x.Field<string>("xtype").Contains("U"));
if (zidus.Count() == 0)
{
return;
}
//处理属性部分
string Zhong = "";
//处理接口部分
string Zhong2 = @"
/// <summary>
/// 实现IoRyTable的接口
/// </summary>
public void SetData(DataRow dr)
{";
//处理初始化函数部分
string Zhong3 = @"
/// <summary>
/// 初始化函数
/// </summary>
public iy" + tablename + @"()
{";
foreach (var ziduitem in zidus)
{
string it = igetctype(ziduitem.Field<string>("ctype"));
string name = ziduitem.Field<string>("cname");
//处理属性部分
Zhong += @"
" + it + " _i" + name + @";
/// <summary>
/// 数据库" + name + @"字段
/// </summary>
public " + it + " i" + name + @"
{
get
{
return _i" + name + @";
}
set
{
_i" + name + @" = value;
if (value == null)
{
LIC.Where(x => x.ioryName == " + "\"" + name + "\"" + @").First().ioryValueNull = true;
}
else
{
LIC.Where(x => x.ioryName == " + "\"" + name + "\"" + @").First().ioryValueNull = false;
}
LIC.Where(x => x.ioryName == " + "\"" + name + "\"" + @").First().ioryValueChange = true;
LIC.Where(x => x.ioryName == " + "\"" + name + "\"" + @").First().ioryValue = Convert.ToString(value);
}
}
";
//处理接口部分
Zhong2 += @"
i" + name + " = dr.Field<" + it + ">(\"" + name + "\");";
//处理初始化函数部分
Zhong3 += @"
LIC.Add(new IoRyCol
{
ioryName = " + "\"" + name + "\"" + @",
ioryType = " + "\"" + it + "\"" + @",
IsIdentity = " + Convert.ToBoolean(ziduitem.Field<int>("IsIdentity")).ToString().ToLower() + @",
IsKey = " + ziduitem.Field<string>("ckey") + @",
IsNull = " + Convert.ToBoolean(ziduitem.Field<int>("isnullable")).ToString().ToLower() + @",
ioryValueNull = true,
ioryValueChange = false
});";
}
//处理接口部分的尾巴
Zhong2 += @"
foreach (var item in LIC)
{
item.ioryValueChange = false;
}
}";
//处理初始化函数部分的尾巴
Zhong3 += @"
}
string tablename = " + "\"" + tablename + "\"" + ";";
string wei = "";
switch (calltype)
{
case "IoRyClass":
wei = "IoRyClass\\wei.txt";
break;
case "WCF":
wei = "WCFv3\\wei.txt";
break;
case "WebAPI":
wei = "WebAPI\\wei.txt";
break;
}
string lei = Tou(tablename) + Zhong + Zhong2 + Zhong3 + myRead(wei);
myWrite(lei, "Table_" + tablename);
}
public static string igetctype(string sqltype)
{
switch (sqltype)
{
case "nvarchar":
return "string";
case "varchar":
return "string";
case "char":
return "string";
case "datetime":
return "DateTime?";
case "int":
return "int?";
case "tinyint"://网上查说和 Byte 对应,但是极少用到暂时用这个 自己设计的数据库别找这个别扭
return "Byte";
case "ntext"://老类型 自己设计的数据库别找这个别扭
return "string";
case "decimal":
return "decimal?";
case "float":
return "double?";
case "image":
return "byte[]";
case "bigint":
return "long?";
case "uniqueidentifier":
return "Guid?";
default:
return "未设置此类型";
}
}
static void myWrite(string content, string tabelname)
{
FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "CreateClass\\" + tabelname + ".cs", FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
sw.Write(content);
sw.Flush();
sw.Close();
}
static string myRead(string path)
{
using (FileStream fsRead = new FileStream(path, FileMode.Open))
{
int fsLen = (int)fsRead.Length;
byte[] heByte = new byte[fsLen];
int r = fsRead.Read(heByte, 0, heByte.Length);
string myStr = System.Text.Encoding.UTF8.GetString(heByte);
return myStr;
}
}
}
}