A dapperhelper-based T4 three layer

Source: Internet
Author: User

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:\ code \OADEMO\OADEMO.MODEL\INCLUDE\EF. Utility.CS.ttinclude "#>
<#@ include file= "D:\ code \oademo\oademo.model\include\outputhelper.ttinclude" #>
<#@ include file= "D:\ code \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>
This code is automatically generated by the T4 template
Build Time <#=datetime.now.tostring ("Yyyy-mm-dd HH:mm:ss") #>
Changes to this file may result in incorrect behavior, and if
Regenerate the code, and the changes will be lost.
</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:\ code \OADEMO\OADEMO.MODEL\INCLUDE\EF. Utility.CS.ttinclude "#>
<#@ include file= "D:\ code \oademo\oademo.model\include\outputhelper.ttinclude" #>
<#@ include file= "D:\ code \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>
This code is automatically generated by the T4 template
Build Time <#=datetime.now.tostring ("Yyyy-mm-dd HH:mm:ss") #> by Ding
Changes to this file may result in incorrect behavior, and if
Regenerate the code, and the changes will be lost.
</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>
Add a piece of data
</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>
//update a data
//</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 #>[email protected]<#= 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 #>[email protected]<#= 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>
Delete a piece of data
</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 #>[email protected]<#= 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>
Bulk Delete Data
</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>
Query based on PRIMARY key
</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 #>[email protected]<#= 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>
Querying all entities based on criteria
</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>
Get total number of bars
</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>
Get a list of data by page
</summary>
Public ienumerable<<#=tablename#>> select<#=tablename#>listbypage (string StrWhere, string by , int startIndex, int endindex,object param = null)
{
StringBuilder strSQL = new StringBuilder ();
Strsql.append ("SELECT * FROM (");
Strsql.append ("Select Row_number () over (");
if (!string. IsNullOrEmpty. Trim ()))
{
Strsql.append ("ORDER by T." +);
}
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:\ code \OADEMO\OADEMO.MODEL\INCLUDE\EF. Utility.CS.ttinclude "#>
<#@ include file= "D:\ code \oademo\oademo.model\include\outputhelper.ttinclude" #>
<#@ include file= "D:\ code \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>
This code is automatically generated by the T4 template
Build Time <#=datetime.now.tostring ("Yyyy-mm-dd HH:mm:ss") #> by Ding
Changes to this file may result in incorrect behavior, and if
Regenerate the code, and the changes will be lost.
</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>
Add a piece of data
</summary>
public bool Add<#=tablename#> (<#=tableName#> entity)
{
Return DAL. Insert<#=tablename#> (entity);
}

<summary>
Update One piece of data
</summary>
public bool Modify<#=tablename#> (<#=tableName#> entity)
{
Return DAL. Update<#=tablename#> (entity);
}

<summary>
Delete a piece of data
</summary>
public bool remove<#=tablename#> (int id)
{
Return DAL. Delete<#=tablename#> (ID);
}


<summary>
Bulk Delete Data
</summary>
public bool Remove<#=tablename#> (string idlist)
{
Return DAL. Delete<#=tablename#> (idlist);
}

<summary>
Querying an entity
</summary>
Public <#=tableName#> getone<#=tablename#> (int id)
{
Return DAL. Selectone<#=tablename#> (ID);
}


<summary>
Querying all entities based on criteria
</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>
Get a list of data by page
</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, I, start, End,param). ToList ();
}

}
}

<#

Manager. Endblock ();
}
Manager. Process (TRUE);
#>

A dapperhelper-based T4 three layer

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.