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 ...
随机推荐
- css奇特用法之 IMG添加背景图片配合显示--效果惊艳
IMG标签本身是显示图片的,但通过CSS可以再为其设置背景图片,让其和自身的图片配合来显示,最终的效果会让你惊叹.当然,这个发现来自于老外,所以代码马上与大家分享.再此之前,我也从来没有想到过这个思路 ...
- JS辨别浏览器系统IOS或安卓
详细内容请点击 /* * 智能机浏览器版本信息: * */ (function($,window,document){ $.extend({ browser:{ ...
- 【CSS3】---属性选择器
在HTML中,通过各种各样的属性可以给元素增加很多附加的信息.例如,通过id属性可以将不同div元素进行区分. 在CSS2中引入了一些属性选择器,而CSS3在CSS2的基础上对属性选择器进行了扩展,新 ...
- MVC3 Razor视图引擎的基础语法
好久没有关注微软的开发了,今天看到了MVC3,顺便学习学习,我觉得Razor是个不错的做法,比使用<%%>简单多了,而且好看.首先“_”开头的cshtml文档将不能在服务器上访问,和asp ...
- JavaScript之canvas
num.push(x,y); 动画草图(举个栗子,我们把数字“2”给画出来): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transit ...
- ASP.NET发布WebService
1. 创建一个空的Web应用程序 2. 再添加一个Web服务 3. 在所创建Web服务内,编写一算法 4. 写完可直接运行查看结果 5. 项目->右键,发布此WebService 6. 发布至 ...
- Oracle 联合主键
alter table NCJSYD add constraints NCJSYD_pk primary key (YR,DQ);
- (转)在Windows上以服务方式运行 MSOPenTech/Redis
ServiceStack.Redis 使用教程里提到Redis最好还是部署到Linux下去,Windows只是用来做开发环境,现在这个命题发生改变了,在Windows上也可以部署生产环境的Redis, ...
- OLE-Excel基本操作
1.create object obj CREATE OBJECT EXCEL 'Excel.Application'. 这个obj通常指能够支持的应用程序 应用程序首先要满足OLE2的标准 同 ...
- AJAX项目中的一些总结
最近在做AJAX数据处理交互,遇到一些问题,备份以免以后忘记 1.参数地址问题 确保参数地址的正确性 ; 阅读后台源码,参数和返回值要确定: 一般的大点公司应该有规范: 2.关于ajax事件触发请求多 ...