码迷,mamicode.com
首页 > 移动开发 > 详细

一个基于DapperHelper的t4三层

时间:2017-02-15 22:12:57      阅读:2627      评论:0      收藏:0      [点我收藏+]

标签:generated   mode   删除   tab   mod   env   format   add   tin   

Model

Model

Model

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Data.DataSetExtensions.dll" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System.Data.SqlClient" #>
<#@ include file="D:\代码\OADemo\OADemo.Model\include\EF.Utility.CS.ttinclude" #>
<#@ include file="D:\代码\OADemo\OADemo.Model\include\OutputHelper.ttinclude" #>
<#@ include file="D:\代码\OADemo\OADemo.Model\include\DbHelper.ttinclude" #>

<#
var manager = Manager.Create(Host, GenerationEnvironment);

foreach(DbTable dbTable in DbHelper.GetDbTables(config.ConnectionString, config.DbDatabase))
{

string item=dbTable.TableName;
manager.StartNewFile(item+".cs");#>
//------------------------------------------------------------------------------
// <auto-generated>
// 此代码由T4模板自动生成
// 生成时间 <#=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")#>
// 对此文件的更改可能会导致不正确的行为,并且如果
// 重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace OADemo.Model
{
public partial class <#=item#>
{
<# foreach(DbColumn column in DbHelper.GetDbColumns(config.ConnectionString, config.DbDatabase, item)) {#>

/// <summary>
/// <#=column.Remark#>
/// </summary>
public <#= column.CSharpType#><# if(column.CommonType.IsValueType && column.IsNullable){#>?<#}#> <#=column.ColumnName#> { get; set; }
<#}#> 
}
}

<#

manager.EndBlock(); 

manager.Process(true); 
#>

 DAL

 

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Data.DataSetExtensions.dll" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System.Data.SqlClient" #>
<#@ include file="D:\代码\OADemo\OADemo.Model\include\EF.Utility.CS.ttinclude" #>
<#@ include file="D:\代码\OADemo\OADemo.Model\include\OutputHelper.ttinclude" #>
<#@ include file="D:\代码\OADemo\OADemo.Model\include\DbHelper.ttinclude" #>

 

<#
var manager = Manager.Create(Host, GenerationEnvironment);
foreach(DbTable dbTable in DbHelper.GetDbTables(config.ConnectionString, config.DbDatabase))

string tableName=dbTable.TableName;
manager.StartNewFile(tableName+"Dal.cs");#>
//------------------------------------------------------------------------------
// <auto-generated>
// 此代码由T4模板自动生成
// 生成时间 <#=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")#> by ding
// 对此文件的更改可能会导致不正确的行为,并且如果
// 重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using OADemo.Model;
using OADemo.Tool;

namespace OADemo.DAL
{
public partial class <#=tableName+"Dal"#>
{

/// <summary>
/// 添加一条数据
/// </summary>
public bool Insert<#=tableName#>(<#=tableName#> entity)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("insert into <#=tableName#>(");
strSql.Append("<#= DbHelper.GetFileStr(tableName) #>");
strSql.Append(") values (");
strSql.Append("<#= DbHelper.GetParStr(tableName) #>");
strSql.Append(" );");

var newClass =
new {<# bool b=false;foreach(DbColumn column in DbHelper.GetDbColumns(config.ConnectionString, config.DbDatabase, tableName) ) {if(!column.IsIdentity) {if (b){ #>, <# } #><#= column.ColumnName #> = entity.<#= column.ColumnName #><# b=true;} #> 
<# }#>};

return DapperHelper.Excute(strSql.ToString(), newClass) > 0;
}

/// <summary>
/// 更新一条数据
/// </summary>
public bool Update<#=tableName#>(<#=tableName#> entity)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("update <#=tableName#> set ");
<#bool isFirst=true; foreach(DbColumn column in DbHelper.GetDbColumns(config.ConnectionString, config.DbDatabase, tableName) ) { if(!column.IsIdentity) { #>
<#if (!isFirst){ #>strSql.Append(","); <# } #>
strSql.Append("<#= column.ColumnName #>=@<#= column.ColumnName #>");
<# isFirst=false; }} #>
strSql.Append(" where ");
<# foreach(DbColumn column in DbHelper.GetDbColumns(config.ConnectionString, config.DbDatabase, tableName) ) { if(column.IsIdentity) {#>
strSql.Append("<#= column.ColumnName #>=@<#= column.ColumnName #>");
<# }} #>


var newClass =
new {<# bool u=false;foreach(DbColumn column in DbHelper.GetDbColumns(config.ConnectionString, config.DbDatabase, tableName) ) { if (u){ #>, <# } #><#= column.ColumnName #> = entity.<#= column.ColumnName #><# u=true; #> 
<# }#>};

 

return DapperHelper.Excute(strSql.ToString(), newClass) > 0;
}

/// <summary>
/// 删除一条数据
/// </summary>
public bool Delete<#=tableName#>(int id)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("delete from <#=tableName#> ");
strSql.Append(" where ");
<# foreach(DbColumn column in DbHelper.GetDbColumns(config.ConnectionString, config.DbDatabase, tableName) ) { if(column.IsIdentity) {#>
strSql.Append("<#= column.ColumnName #>=@<#= column.ColumnName #>");
<# }} #>

var newClass =
new {
<#foreach(DbColumn column in DbHelper.GetDbColumns(config.ConnectionString, config.DbDatabase, tableName) ) { if(column.IsIdentity) {#> <#= column.ColumnName #>= id<# break;}} #> 
};
return DapperHelper.Excute(strSql.ToString(), newClass) > 0;
}


/// <summary>
/// 批量删除数据
/// </summary>
public bool Delete<#=tableName#>(string idList)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("delete from <#=tableName#> ");
strSql.Append(" where ");
strSql.Append(" <#foreach(DbColumn column in DbHelper.GetDbColumns(config.ConnectionString, config.DbDatabase, tableName) ) { if(column.IsIdentity) {#><#= column.ColumnName #> <# break;}} #>");
strSql.Append(" in (" + idList + ") ");
return DapperHelper.Excute(strSql.ToString()) > 0;
}

/// <summary>
///根据 主键查询
/// </summary>
public <#=tableName#> SelectOne<#=tableName#>(int id)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select top 1 * from <#=tableName#> ");
strSql.Append(" where ");
<# foreach(DbColumn column in DbHelper.GetDbColumns(config.ConnectionString, config.DbDatabase, tableName) ) { if(column.IsIdentity) { #>
strSql.Append("<#= column.ColumnName #>=@<#= column.ColumnName #>");
<# }} #>
var newClass = new {
<#foreach(DbColumn column in DbHelper.GetDbColumns(config.ConnectionString, config.DbDatabase, tableName) ) { if(column.IsIdentity) {#><#= column.ColumnName #>= id<# break;}} #> 
};

return DapperHelper.FirstOrDefault<<#=tableName#>>(strSql.ToString(), newClass);

}


/// <summary>
/// 根据条件查询所有实体
/// </summary>
public IEnumerable<<#=tableName#>> Select<#=tableName#>List(string strWhere,object param = null)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select * ");
strSql.Append(" FROM <#=tableName#> ");
if (strWhere.Trim() != "")
{
strSql.Append(" where " + strWhere);
}

return DapperHelper.Query<<#=tableName#>>(strSql.ToString(),param );

}

/// <summary>
/// 获取总条数
/// </summary>
/// <param name="strWhere"></param>
/// <returns></returns>
public int GetDataRecord(string strWhere,object param = null)
{
string sql = "select count(*) from <#=tableName#> where 1 = 1 " + strWhere;
object obj = DapperHelper.ExecuteScalar(sql,param );
if (obj == null)
{
return 0;
}
return Convert.ToInt32(obj);
}

/// <summary>
/// 分页获取数据列表
/// </summary>
public IEnumerable<<#=tableName#>> Select<#=tableName#>ListByPage(string strWhere, string orderby, int startIndex, int endIndex,object param = null)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("SELECT * FROM ( ");
strSql.Append(" SELECT ROW_NUMBER() OVER (");
if (!string.IsNullOrEmpty(orderby.Trim()))
{
strSql.Append("order by T." + orderby);
}
else
{
strSql.Append("order by T.<#=DbHelper.GetPrimaryKey(tableName)#> desc");
}
strSql.Append(")AS Row, T.* from <#=tableName#> T ");
if (!string.IsNullOrEmpty(strWhere.Trim()))
{
strSql.Append(" WHERE 1 = 1  " + strWhere);
}
strSql.Append(" ) TT");
strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);

return DapperHelper.Query<<#=tableName#>>(strSql.ToString(),param );
}

}
}

<#

manager.EndBlock(); 

manager.Process(true); 
#>

 

BLL

 

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Data.DataSetExtensions.dll" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System.Data.SqlClient" #>
<#@ include file="D:\代码\OADemo\OADemo.Model\include\EF.Utility.CS.ttinclude" #>
<#@ include file="D:\代码\OADemo\OADemo.Model\include\OutputHelper.ttinclude" #>
<#@ include file="D:\代码\OADemo\OADemo.Model\include\DbHelper.ttinclude" #>

<#
var manager = Manager.Create(Host, GenerationEnvironment);
foreach(DbTable dbTable in DbHelper.GetDbTables(config.ConnectionString, config.DbDatabase))

string tableName=dbTable.TableName;
manager.StartNewFile(tableName+"Bll.cs");#>
//------------------------------------------------------------------------------
// <auto-generated>
// 此代码由T4模板自动生成
// 生成时间 <#=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")#> by ding
// 对此文件的更改可能会导致不正确的行为,并且如果
// 重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OADemo.DAL;
using OADemo.Model;

namespace OADemo.BLL
{
public partial class <#=tableName#>Business
{

<#=tableName#>Dal dal = new <#=tableName#>Dal();

/// <summary>
/// 添加一条数据
/// </summary>
public bool Add<#=tableName#>(<#=tableName#> entity)
{
return dal.Insert<#=tableName#>(entity);;
}

/// <summary>
/// 更新一条数据
/// </summary>
public bool Modify<#=tableName#>(<#=tableName#> entity)
{
return dal.Update<#=tableName#>(entity);
}

/// <summary>
/// 删除一条数据
/// </summary>
public bool Remove<#=tableName#>(int id)
{
return dal.Delete<#=tableName#>(id);
}


/// <summary>
/// 批量删除数据
/// </summary>
public bool Remove<#=tableName#>(string idList)
{
return dal.Delete<#=tableName#>(idList);
}

/// <summary>
/// 查询一个实体
/// </summary>
public <#=tableName#> GetOne<#=tableName#>(int id)
{
return dal.SelectOne<#=tableName#>(id);
}


/// <summary>
/// 根据条件查询所有实体
/// </summary>
public List<<#=tableName#>> Get<#=tableName#>List(string strWhere,object param = null) )
{
return dal.Select<#=tableName#>List(strWhere,param).ToList();
}

 

public int GetDataRecord(string strWhere,object param = null))
{
return dal.GetDataRecord(strWhere,param);
}

/// <summary>
/// 分页获取数据列表
/// </summary>
public List<<#=tableName#>> GetOA_UserListByPage(int pageIndex,int pageSize,string strWhere, string orderby,out int dataRecord,out int pageCount,object param = null))
{
dataRecord = GetDataRecord(strWhere,param);
pageCount = (int)Math.Ceiling(dataRecord * 1.0 / pageSize);

if (pageIndex<=0)
{
pageIndex = 1;
}
if (pageIndex>= pageCount)
{
pageIndex = pageCount;
}

int start = (pageIndex - 1) * pageSize + 1;
int end = pageIndex * pageSize;

return dal.Select<#=tableName#>ListByPage(strWhere, orderby, start, end,param).ToList();
}

 

 

 

}
}

<#

manager.EndBlock(); 

manager.Process(true); 
#>

一个基于DapperHelper的t4三层

标签:generated   mode   删除   tab   mod   env   format   add   tin   

原文地址:http://www.cnblogs.com/artjs/p/6403399.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!