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 ...
随机推荐
- Table of Contents - Servlet & JSP
Servlet Servlet API Overview Java Web 访问资源的路径问题 getParameter 与 request.getAttribute 的区别 转发与重定向的区别 中文 ...
- django 学习-15 .Django文件上传(用户注册)
1.vim blog/views.py from django.shortcuts import render_to_responsefrom django.http import HttpR ...
- 重新审视事件对象event
前言:之前在学习事件对象event时,一是一直在chrome浏览器(作为主运行环境)下运行调试自个儿程序,二是可能当时对事件对象理解不透彻才导致现在对事件对象的用法陷入了一个大坑,遂以此篇博客记之. ...
- jquery页面滚动,菜单固定到顶部
// 菜单固定 $(function(){ //获取要定位元素距离浏览器顶部的距离 var navH = $("#topp").offset().top; //滚动条事件 $(wi ...
- apache commons-email1.3使用
apache commons-email1.3下载地址: https://repository.apache.org/content/repositories/orgapachecommons-0 ...
- C++学习——类的继承
公有继承(public).私有继承(private).保护继承(protected)是常用的三种继承方式. 1. 公有继承(public) 公有继承的特点是基类的公有成员和保护成员作为派生类的成员时, ...
- iOS Foundation框架 -3.利用NSNumber和NSValue将非OC对象类型数据存放到集合
1.Foundation框架中提供了很多的集合类如:NSArray,NSMutableArray,NSSet,NSMutableSet,NSDictionary,NSMutableDictionary ...
- mysql颠覆实战笔记(一)--设计一个项目需求,灌入一万数据先
版权声明:笔记整理者亡命小卒热爱自由,崇尚分享.但是本笔记源自www.jtthink.com(程序员在囧途)沈逸老师的<web级mysql颠覆实战课程 >.如需转载请尊重老师劳动,保留沈逸 ...
- php下载文件,添加响应头
//下载,添加响应头信息 header('Content-type:application/octet-stream'); header('Content-Disposition:attachment ...
- JSON结构
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript Programming Langu ...