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 ...
随机推荐
- 写jQuery插件
如今做web开发,jquery 几乎是必不可少的,就连vs神器在2010版本开始将Jquery 及ui 内置web项目里了.至于使用jquery好处这里就不再赘述了,用过的都知道.今天我们来讨论下jq ...
- 干货:Android 源码使用心得分享
我相信很多初学者会和我一样经常在网上去找Android开发源码,但是往往因为运行不起来非常的懊恼!在做爱开发网站的时候,收集App代码时就遇到了这种困难,我相信网络上面的源码大部分在发布前 ...
- C#中数组、ArrayList和List<T>三者的发展历程
在C#中数组,ArrayList,List使我们用的最多的类型之一.他们共同的作用都是能够存储一组对象. 那么问题来了: (1)为什么要有三个一样作用的东西呢?他们都很完美吗? (2)谁先出生,又是因 ...
- init: Associated with Deployer 'Catalina:type=Deployer,host=localhost'
四月 12, 2014 1:54:12 上午 org.apache.catalina.core.ApplicationContext log信息: HTMLManager: init: Associ ...
- 构造高度自适应的textarea
高度自适应的textarea,这个需求还是比较常见的,随着用户的输入textarea的高度自动变化,这样输入较少的时候可以节省空间,输入多的时候可以不出现滚动条,让内容尽可能的展现在用户的视线内. 可 ...
- 'hibernate.dialect' must be set when no Connection avalable
'hibernate.dialect' must be set when no Connection avalable 当连接不可用时,必须要设置Hibernate方言 搜索大多数的解决办法是:首先查 ...
- Change http port in bitnami stack
My case goes like this. I installed bitnami redmine first with port 80 for http service, but got pro ...
- POD数据了解
Plain old data (普通旧的数据); POD 是Plain Old Data的簡寫,是指一些系統的int, char, float.指標.array之類的資料型別,這應該蠻好想像的,就是C ...
- Android 悬浮窗 WindowManager WindowManager.LayoutParamas
前方高清大图~~~~ 绘制的图片资源: // draw bitmap BitmapDrawable bmpDraw = (BitmapDrawable) getResources().getDrawa ...
- 计算 sql查询语句所花时间
--1:下面这种是SQL Server中比较简单的查询SQL语句执行时间方法,通过查询前的时间和查询后的时间差来计算的: declare @begin_date datetimedeclare @en ...