Text Template Transformation Toolkit
Text Template Transformation Toolkit


<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #> public class Samsung
{
<# for(int i=;i<;i++) {#>
public string S<#=i#>{get;set;}
<#}#>
}
写一下控制块的代码
public class Samsung
{
public string S1{ get; set;}
public string S2{ get; set;}
public string S3{ get; set;}
public string S4{ get; set;}
}
看一下三星的旗舰
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #> public class Meizu
{
<# for(int i=;i<;i++) {#>
public string MX<#=i#>{get;set;}
<#}#>
}
修改下控制块代码,我想要梦想
public class Meizu
{
public string MX1{ get; set;}
public string MX2{ get; set;}
public string MX3{ get; set;}
public string MX4{ get; set;}
}
且容我看一眼梦想。
那么T4是有点帅,还是很有点帅。
那么很有点帅,或者相当帅吧。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Anmutu.OA.IDAL
{
/// <summary>
/// 此接口抽象了DAL实例里公共方法的约束。
/// </summary>
public interface IBaseDal<T> where T: class, new ()
{
T Add(T entity);
bool Update(T entity);
bool Delete(T entity);
int Delete( params int[] ids);
}
}
IBaseDal
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Anmutu.OA.Model; namespace Anmutu.OA.IDAL
{
/// <summary>
/// 创建一个接口,约定其返回类型是User类,参数是一个user实体。
/// </summary>
public interface IUserDal:IBaseDal<User>
{ }
}
IUserDal
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Anmutu.OA.Model;
using EntityState = System.Data.Entity.EntityState; namespace Anmutu.OA.DAL
{
/// <summary>
/// 把数据库访问层的公共方法抽出来实现。
/// </summary>
/// <typeparam name="T"></typeparam>
public particl class BaseDal<T> where T: class, new() //类。且有一个默认的构造函数。
{
//写在这里就没做到线程实例唯一了。此处亦就用简单工厂得到上下文实例。
private Model.AnmutuModelContainer db = new AnmutuModelContainer();
public T Add(T entity)
{
db.Set<T>().Add(entity);
db.SaveChanges();
return entity;
} public bool Update(T entity)
{
db.Entry(entity).State = EntityState.Modified;
return db.SaveChanges() > ;
} public bool Delete(T entity)
{
db.Entry(entity).State = EntityState.Deleted;
return db.SaveChanges() > ;
} public int Delete( params int[] ids)
{
foreach ( var id in ids)
{
//如若实体已经在内存中就在内存中拿,如果内存中没有则查询数据库。
var entity = db.Set<T>().Find(id);
db.Set<T>().Remove(entity);
}
return db.SaveChanges();
}
}
}
BaseDal
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Anmutu.OA.IDAL;
using Anmutu.OA.Model;
using EntityState = System.Data.Entity.EntityState; namespace Anmutu.OA.DAL
{
public partial class UserDal : BaseDal<User>,IUserDal //这里实现接口。
{ }
}
UserDal
笔者注:其中有代码少了PARTICAL关键字,如若改兴趣,你会发现是哪里的,笔者就不回去做修改了。
<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#>
<#@ output extension=".cs"#> <#
CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, );
MetadataTools ef = new MetadataTools(this); string inputFile = @"..\\Anmutu.OA.Model\\AumutuModel.edmx";
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
#> using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Anmutu.OA.IDAL;
using Anmutu.OA.Model;
using EntityState = System.Data.Entity.EntityState; namespace Anmutu.OA.DAL
{
<#
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
#>
public partial class <#=entity.Name#>Dal : I<#=entity.Name#>Dal
{ }
<#}#>
}
IUserDal的T4
Text Template Transformation Toolkit的更多相关文章
- C# Meta Programming - Let Your Code Generate Code - Introduction of The Text Template Transformation Toolkit(T4)
<#@ template language="C#" #> <#@ output extension=".cs" #> <#@ a ...
- T4((Text Template Transformation Toolkit))模版引擎之基础入门 C#中文本模板(.tt)的应用
1 关于C#中文本模板(.tt)的简单应用https://blog.csdn.net/zunguitiancheng/article/details/78011145 任何一个傻瓜都能写出计算机能理解 ...
- go语言的模板,text/template包
go语言的模板,text/template包 定义 模板就是将一组文本嵌入另一组文本里 传入string--最简单的替换 package main import ( "os" &q ...
- script "text/template"
<script type="text/template" id="orgItem"> <div class="{orgClass}& ...
- go标准库的学习-text/template
参考:https://studygolang.com/pkgdoc 导入方式: import "text/template" template包实现了数据驱动的用于生成文本输出的模 ...
- <script type="text/template">是干什么的,为什么要把html写在js中? 这是什么编程语言风格,都能这样用吗?
这一段存放了一个模板.在js里面,经常需要使用js往页面中插入html内容.比如这样: var number = 123; $('#d').append('<div class="t& ...
- 前端模板<script type="text/template" id="tmpl">
前端模板, 比连接字符串好用多了, 还可以使用循环\判断等语句, 减少工作量 <script type="text/template" id="member-tmp ...
- 关于MVC模板渲染的一点小事type="text/template"
先上一个demo,简单粗暴,请自便 <!DOCTYPE html> <html> <head lang="en"> <meta chars ...
- 关于 <script type='text/template' > 的妙用 / 使用jquery获取iframe加载完成事件
https://www.cnblogs.com/ddqyc/p/6200539.html <!DOCTYPE html> <html> <head> <met ...
随机推荐
- Asp.net MVC 4 模型的数据注释
[Bind(…)] Lists fields to exclude or include when binding parameter or form values to model properti ...
- Java之组合数组2
编写函数Fun,其功能是将两个两位数的正整数A.B合并为一个整数放在C中,将A数的十位和个位一次放在C的个位和十位上,A数的十位和个位一次放在C的百位和千位上.例如,当 A=16,B=35,调用该函数 ...
- 关于offsetWidth,clientWidth,与jquery的width()方法
offsetWidth包括了边框的宽度, clientWidth只是元素的宽度, 当元素的"display"属性为“none”的时候,用offsetWidth(clientWidt ...
- 集合类学习之HashMap经典储存 分拣存储与面向对象组合
HashMap:键值对(key-value) 通过对象来对对象进行索引,用来索引的对象叫做key,其对应的对象叫做value. 默认是1:1关系(一对一) 存在则覆盖,当key已经存在,则利用新的va ...
- Objective-C 【autorelease基本使用】
------------------------------------------- NSString中的内存管理问题 由于autoreleasepool的存在,对于内存管理就会很复杂,retain ...
- IE6和IE8细节问题
1.对于使用jQuery的ajax.IE6要求使用带有全部的属性:例如IE对下面代码中type:"POST",有严格的要求,如果没有该属性,则无法向后他发送请求 $.ajax({ ...
- (转)LR监控Linux系统性能计数器详解
从LR里面add measurement,填写linux机器的IP,出现所有unix/linux的计数器,包括cpu的,mem的,disk,network的.这里介绍几个常用的: (对于如何监控Lin ...
- JSON字符串和js对象转换
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON js数据类型: 字符串.数字 ...
- jacob 给word加印的功能
花了两天时间,参考了一些资料,总算是处理好了这样一个技术点. 关键的心得如下: 使用jacob,重点不是jacob本身,而是office的一些API资料.比如需要知道光标的移动, 包括上下左右的mov ...
- Linux多命令协作:管道及重定向