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 ...
随机推荐
- 解决ashx文件下的Session“未将对象引用设置到对象的实例”
using System; using System.Collections.Generic; using System.Linq; using System.Web; using PPT_DAL; ...
- 入门必须掌握8个DOS命令
一,ping 它是用来检查网络是否通畅或者网络连接速度的命令.作为一个生活在网络上的管理员或者黑客来说,ping命令是第一个必须掌握的DOS命令,它所利用的原理是这样的:网络上的机器都有唯一确定的IP ...
- OSPF多区域配置
1.配置三台路由器IP R1(config)#INTER S1/0 R1(config-if)#IP ADDress 192.1.12.1 255.255.255.0 R1(config-if)#no ...
- 备忘-zTree v3.5 Demo 演示
zTree v3.5 Demo 演示: http://www.ztree.me/v3/demo.php#_110
- 通信行业OSS支撑系统软件研发思考
一般的,对所谓大型.通信行业.OSS支撑软件系统,我们可宏观定义以下几点: 以年计的研发周期 以几十人计的研发团队 以百计的业务菜单功能点 以千计的数据库表 以万计的业务术语指标 以亿计的数据表记录 ...
- VxWorks 6.9 内核编程指导之读书笔记 -- VxWorks kernel application (一)
#1 什么是内核应用程序? #2 开发内核应用程序注意事项 什么是内核应用程序? 内核应用程序不同于RTP程序,它允许在内核态,与操作系统使用相同的地址空间.因此,它与操作系统会相互干扰.它可以编译成 ...
- OC3-xml文件解析
<?xml version="1.0"?> <xml_api_reply version="1"> <cities> < ...
- 直播开始:'云榨汁机'诞生记--聊聊JavaScript中的'业务建模'
闭包是JavaScript中的一个重要特性,在之前的博文中,我们说闭包是一个'看似简单,其实很有内涵'的特性.当我们用JavaScript来实现相对复杂的业务建模时,我们可以如何利用'闭包'这个特性呢 ...
- 10款基于jquery的web前端特效及源码下载
1.jQuery时间轴插件:jQuery Timelinr 这是一款可用于展示历史和计划的时间轴插件,尤其比较适合一些网站展示发展历程.大事件等场景.该插件基于jQuery,可以滑动切换.水平和垂直滚 ...
- Codevs 1080 线段树联系
题目描述 Description 一行N个方格,开始每个格子里都有一个整数.现在动态地提出一些问题和修改:提问的形式是求某一个特定的子区间[a,b]中所有元素的和:修改的规则是指定某一个格子x,加上或 ...