博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FluentData-新型轻量级ORM 利用T4模板 批量生成多文件 实体和业务逻辑 代码
阅读量:5900 次
发布时间:2019-06-19

本文共 11051 字,大约阅读时间需要 36 分钟。

FluentData,它是一个轻量级框架,关注性能和易用性。 

下载地址:

利用T4模板,【MultipleOutputHelper.ttinclude】批量生成多文件

基本语法:

1. 初始化:获取文件模板 在T4模板导入

//导入MultipleOutputHelper.ttinclude文件 路径      <#@include file="$(SolutionDir)\ORM.Model\T4\MultipleOutputHelper.ttinclude"#>
//初始化Manager对象       var manager = Manager.Create(Host, GenerationEnvironment);

2.文件块:使用代码标识区分生成的代码块的范围

          manager.StartNewFile(tb_name+".cs");
         //代码块
         manager.EndBlock();

3.编译执行:使用Process方法,进行文件分割

manager.Process(true);
 

实践:

1.批量生成实体类

<#@ template debug="false" hostspecific="true" language="C#" #><#@ assembly name="System.Data" #><#@ assembly name="System.xml" #><#@ import namespace="System.Collections.Generic" #><#@ import namespace="System.Data.SqlClient" #><#@ import namespace="System.Data" #><#@ assembly name="System.Core" #><#@ import namespace="System.Linq" #>//  导入MultipleOutputHelper.ttinclude文件<#@include file="$(SolutionDir)\My.Model\T4\MultipleOutputHelper.ttinclude"#> <#                string connectionString= "server=qq;database=db;uid=sa;pwd=sa;";                SqlConnection conn = new SqlConnection(connectionString);        conn.Open();            string selectQuery ="SET FMTONLY ON; select * from @tableName; SET FMTONLY OFF;";        SqlCommand command = new SqlCommand(selectQuery,conn);        SqlDataAdapter ad = new SqlDataAdapter(command);        System.Data.DataSet ds = new DataSet();           var manager = Manager.Create(Host, GenerationEnvironment);          System.Data.DataTable schema = conn.GetSchema("Tables");        foreach(System.Data.DataRow row in schema.Rows)        {                ds.Tables.Clear();            string tb_name= row["TABLE_NAME"].ToString();                    command.CommandText = selectQuery.Replace("@tableName",row["TABLE_NAME"].ToString());            ad.FillSchema(ds, SchemaType.Mapped,tb_name);              manager.StartNewFile(tb_name+".cs");#>using FluentData;using System;using System.Collections.Generic;namespace My.Model{        ///     /// 实体-<#=tb_name#>     ///     public partial class <#=tb_name#>        {            <#        PushIndent("          ");        foreach (DataColumn dc in ds.Tables[0].Columns)         {            WriteLine("public " + dc.DataType.Name+ (dc.AllowDBNull && dc.DataType.Name.ToLower() != "string" ? "? ": " ") + dc.ColumnName + " { get; set; }");        }        PopIndent();        #>    }}    <#        manager.EndBlock();             }                    conn.Close();         manager.Process(true);          #>

 

2.批量生成基础业务逻辑

<#@ template debug="false" hostspecific="true" language="C#" #><#@ assembly name="System.Data" #><#@ assembly name="System.xml" #><#@ import namespace="System.Collections.Generic" #><#@ import namespace="System.Data.SqlClient" #><#@ import namespace="System.Data" #><#@ assembly name="System.Core" #><#@ import namespace="System.Linq" #>//  导入MultipleOutputHelper.ttinclude文件<#@include file="$(SolutionDir)\MY.Model\T4\MultipleOutputHelper.ttinclude"#> <#                string connectionString= "server=QQ;database=DB;uid=sa;pwd=sa;";                SqlConnection conn = new SqlConnection(connectionString);        conn.Open();            string selectQuery ="SET FMTONLY ON; select * from @tableName; SET FMTONLY OFF;";        SqlCommand command = new SqlCommand(selectQuery,conn);        SqlDataAdapter ad = new SqlDataAdapter(command);        System.Data.DataSet ds = new DataSet();           var manager = Manager.Create(Host, GenerationEnvironment);          System.Data.DataTable schema = conn.GetSchema("Tables");        foreach(System.Data.DataRow row in schema.Rows)        {                ds.Tables.Clear();            string tb_name= row["TABLE_NAME"].ToString();                    command.CommandText = selectQuery.Replace("@tableName",row["TABLE_NAME"].ToString());            ad.FillSchema(ds, SchemaType.Mapped,tb_name);              manager.StartNewFile(tb_name+"Action.cs");#>using System;using System.Collections.Generic;using MY.Model;using FluentData;namespace MY.BLL{            ///     /// <#=tb_name#> 操作类    ///     public partial class <#=tb_name#>Action    {<#        string fkQuery = " SELECT f.name AS ForeignKey,";        fkQuery += " OBJECT_NAME(f.parent_object_id) AS TableName, ";        fkQuery += " COL_NAME(fc.parent_object_id, fc.parent_column_id) AS ColumnName, ";        fkQuery += " OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName, ";        fkQuery += " COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS ReferenceColumnName ";        fkQuery += " FROM ";        fkQuery += " sys.foreign_keys AS f ";        fkQuery += " INNER JOIN sys.foreign_key_columns AS fc ON f.OBJECT_ID = fc.constraint_object_id ";        fkQuery += " where OBJECT_NAME(f.parent_object_id) = '" +tb_name +"'";        List
fkColumnNames = new List
(); Dictionary
parentTables = new Dictionary
(); SqlCommand command2 = new SqlCommand(fkQuery,conn); using(var dr = command2.ExecuteReader()) { while(dr.Read()) { fkColumnNames.Add(dr["ColumnName"].ToString()); parentTables.Add(dr["ColumnName"].ToString(), dr["ReferenceTableName"].ToString()); } dr.Close(); } List
fkColumns = new List
(); foreach(string fkColumnName in fkColumnNames) { foreach (DataColumn dc in ds.Tables[0].Columns) { if(dc.ColumnName == fkColumnName) fkColumns.Add(dc); } } List
primaryKeyParamsWithTypes = new List
(); List
primaryKeyParams = new List
(); List
whereItems = new List
(); List
automapItems = new List
(); foreach(DataColumn pk in ds.Tables[0].PrimaryKey) { primaryKeyParamsWithTypes.Add(string.Format("{0} {1}", pk.DataType.Name, this.ToCamelCase(pk.ColumnName))); primaryKeyParams.Add(pk.ColumnName); whereItems.Add(string.Format("{0} = @{1}", pk.ColumnName, pk.ColumnName.ToLower())); automapItems.Add(string.Format("x.{0}", pk.ColumnName)); } string updateAutomap = string.Empty; if(automapItems.Count > 0){ if(automapItems.Count > 1) updateAutomap = "new { " + string.Join(", ", automapItems.ToArray()) + " }"; else updateAutomap = automapItems[0]; } #> public static <#=tb_name#> Select(<#=string.Join(", ", primaryKeyParamsWithTypes.ToArray())#>) { using(var context = db.Context()) { return context.Sql(" SELECT * FROM <#=tb_name#> WHERE <#=string.Join(" AND ", whereItems.ToArray())#> ") <#foreach(string pkp in primaryKeyParams) { WriteLine(".Parameter(\"" + pkp.ToLower() + "\", " + this.ToCamelCase(pkp) + ")"); } #> .QuerySingle<<#=tb_name#>>(); } } public static List<<#=tb_name#>> SelectAll() { return SelectAll(string.Empty); } public static List<<#=tb_name#>> SelectAll(string sortExpression) { return SelectAll(0, 0, sortExpression); } public static List<<#=tb_name#>> SelectAll(int startRowIndex, int maximumRows, string sortExpression) { using (var context = db.Context()) { var select = context.Select<<#=tb_name#>>(" * ") .From(" <#=tb_name#> "); if (maximumRows > 0) { if (startRowIndex == 0) startRowIndex = 1; select.Paging(startRowIndex, maximumRows); } if (!string.IsNullOrEmpty(sortExpression)) select.OrderBy(sortExpression); return select.QueryMany(); } } public static int CountAll() { using (var context = db.Context()) { return context.Sql(" SELECT COUNT(*) FROM <#=tb_name#> ") .QuerySingle
(); } } <# foreach(DataColumn dc in fkColumns) { #> public static List<<#=tb_name#>> SelectBy<#=parentTables[dc.ColumnName]#>(<#=dc.DataType.Name#> <#=this.ToCamelCase(dc.ColumnName)#>) { return SelectBy<#=parentTables[dc.ColumnName]#>(<#=this.ToCamelCase(dc.ColumnName)#>, string.Empty); } public static List<<#=tb_name#>> SelectBy<#=parentTables[dc.ColumnName]#>(<#=dc.DataType.Name#> <#=this.ToCamelCase(dc.ColumnName)#>, string sortExpression) { return SelectBy<#=parentTables[dc.ColumnName]#>(<#=this.ToCamelCase(dc.ColumnName)#>, 0, 0, sortExpression); } public static List<<#=tb_name#>> SelectBy<#=parentTables[dc.ColumnName]#>(<#=dc.DataType.Name#> <#=this.ToCamelCase(dc.ColumnName)#>, int startRowIndex, int maximumRows, string sortExpression) { using (var context = db.Context()) { var select = context.Select<<#=tb_name#>>(" * ") .From(" <#=tb_name#> ") .Where(" <#=dc.ColumnName#> = @<#=dc.ColumnName.ToLower()#> ") .Parameter("<#=dc.ColumnName.ToLower()#>", <#=this.ToCamelCase(dc.ColumnName)#>); if (maximumRows > 0) { if (startRowIndex == 0) startRowIndex = 1; select.Paging(startRowIndex, maximumRows); } if (!string.IsNullOrEmpty(sortExpression)) select.OrderBy(sortExpression); return select.QueryMany(); } } public static int CountBy<#=parentTables[dc.ColumnName]#>(<#=dc.DataType.Name#> <#=this.ToCamelCase(dc.ColumnName)#>) { using (var context = db.Context()) { return context.Sql(" SELECT COUNT(*) FROM <#=tb_name#> WHERE <#=dc.ColumnName#> = @<#=dc.ColumnName.ToLower()#>") .Parameter("<#=dc.ColumnName.ToLower()#>", <#=this.ToCamelCase(dc.ColumnName)#>) .QuerySingle
(); } } <#}#><#if(ds.Tables[0].PrimaryKey != null && ds.Tables[0].PrimaryKey.Length == 1 && ds.Tables[0].PrimaryKey[0].AutoIncrement) {#> public static bool Insert(<#=tb_name#> <#=this.ToCamelCase(tb_name)#>) { using (var context = db.Context()) { int id = context.Insert<<#=tb_name#>>("<#=tb_name#>", <#=this.ToCamelCase(tb_name)#>) .AutoMap(x => x.<#=primaryKeyParams[0]#>) .ExecuteReturnLastId
(); <#=this.ToCamelCase(tb_name)#>.<#=primaryKeyParams[0]#> = id; return id > 0; } }<#}else{#> public static bool Insert(<#=tb_name#> <#=this.ToCamelCase(tb_name)#>) { using (var context =db.Context()) { return context.Insert<<#=tb_name#>>("<#=tb_name#>", <#=this.ToCamelCase(tb_name)#>) .Execute() > 0; } }<#}#> public static bool Update(<#=tb_name#> <#=this.ToCamelCase(tb_name)#>) { using (var context = db.Context()) { return context.Update<<#=tb_name#>>("<#=tb_name#>", <#=this.ToCamelCase(tb_name)#>) .AutoMap(x => <#=updateAutomap#>) <#foreach(string pkp in primaryKeyParams){#> .Where("<#=pkp#>", <#=this.ToCamelCase(tb_name)#>.<#=pkp#>) <#}#> .Execute() > 0; } } public static bool Delete(<#=tb_name#> <#=this.ToCamelCase(tb_name)#>) { return Delete(<#=string.Join(", ", primaryKeyParams.Select(x=> this.ToCamelCase(tb_name) + "." + x).ToArray())#>); } public static bool Delete(<#=string.Join(", ", primaryKeyParamsWithTypes.ToArray())#>) { using (var context = db.Context()) { return context.Sql(" DELETE FROM Product WHERE <#=string.Join(" AND ", whereItems.ToArray())#> ") <#foreach(string pkp in primaryKeyParams) { WriteLine(".Parameter(\"" + pkp.ToLower() + "\", " + this.ToCamelCase(pkp) + ")"); }#> .Execute() > 0; } } } } <# manager.EndBlock(); } conn.Close(); manager.Process(true); #> <#+ public string ToCamelCase(string value) { if(string.IsNullOrEmpty(value)) return string.Empty; string firstLetter = value.Substring(0, 1); string rest = value.Substring(1, value.Length - 1); return firstLetter.ToLower() + rest; }#>

CTRL+S  自动生成

 下载地址:

 

完成!即可快速开发了!

    本文转自曾祥展博客园博客,原文链接:http://www.cnblogs.com/zengxiangzhan/p/3250105.html
,如需转载请自行联系原作者
你可能感兴趣的文章
一次惨绝人寰的前端题
查看>>
Asynchronous JS: Callbacks, Listeners, Control Flow Libs and Promises
查看>>
SQL-游标-查询数据库中的所有表的数据个数
查看>>
【HTTP】另类的POST头数据 RFC1867协议格式简析
查看>>
sql索引创建
查看>>
计算机书籍控下载
查看>>
IDEA 2017破解 license server激活
查看>>
mysql数据库和oracle数据库之间互相导入备份
查看>>
git stash 命令
查看>>
IDEA引入外部jar包的方法
查看>>
Javascript的this用法
查看>>
ubuntu install wxpython & ulipad
查看>>
文件的定位与出错检查
查看>>
JavaScript Diagramming
查看>>
SQL Server 2005实现负载均衡
查看>>
【指针】c/c++中指针学习的两个绝好例子[转]
查看>>
【Spring】【原创】Spring干什么,做什么
查看>>
Dto2Entity,WCF+ADO.EF数据传输对象和实体对象转换
查看>>
RESTful 接口规范
查看>>
poj_1464 动态规划
查看>>