T4模板建立多個方法

2021-10-03 20:14:49 字數 4499 閱讀 1321

最近在學sping.net,用到了t4模板,由於技術發展原因,學習的是舊架構版本,新架構有些問題。經過多次嘗試修改後,得以解決,分享出來,供網友參考。另外,還行大佬們多多批評指正,感激不盡。

舊架構**

<#@ 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, 1);

metadatatools ef = new metadatatools(this);

string inputfile = @"..\\ec.oa.model\\datamodel.edmx";

edmitemcollection itemcollection = loader.createedmitemcollection(inputfile);

string namespacename = code.vsnamespacesuggestion();

entityframeworktemplatefilemanager filemanager = entityframeworktemplatefilemanager.create(this);

#>

using ecjtu.oa.model;

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.threading.tasks;

namespace ec.oa.idal

<#}#>

}

新架構**

<#@ template language="c#" debug="false" hostspecific="true"#>

<#@ include file="ef6.utility.cs.ttinclude"#><#@

output extension=".cs"#><#

const string inputfile = @"..\\ec.oa.model\\datamodel.edmx";

var texttransform = dynamictexttransformation.create(this);

var code = new codegenerationtools(this);

var ef = new metadatatools(this);

var filemanager = entityframeworktemplatefilemanager.create(this);

var itemcollection = new edmmetadataloader(texttransform.host, texttransform.errors).createedmitemcollection(inputfile);

#>

using system;

using system.collections.generic;

using system.linq;

using system.linq.expressions;

using system.text;

using system.threading.tasks;

using ec.oa.model;

<# beginnamespace(code);

#>

<#

<#}#>

<#

endnamespace(code);

#>

<#+

public void beginnamespace(codegenerationtools code)

}public void endnamespace(codegenerationtools code)

<#+

}}public class codestringgenerator

public string accessibilityandvirtual(string accessibility)

}public static string fixnamespaces(string typename)

public string gettypename(typeusage typeusage)

public string gettypename(edmtype edmtype)

public string gettypename(typeusage typeusage, string modelnamespace)

public string gettypename(edmtype edmtype, string modelnamespace)

public string gettypename(edmtype edmtype, bool? isnullable, string modelnamespace)

var collectiontype = edmtype as collectiontype;

if (collectiontype != null)

>", gettypename(collectiontype.typeusage, modelnamespace));

}var typename = _code.escape(edmtype.metadataproperties

.where(p => p.name == externaltypenameattributename)

.select(p => (string)p.value)

.firstordefault())

?? (modelnamespace != null && edmtype.namespacename != modelnamespace ?

_code.createfullname(_code.escapenamespace(edmtype.namespacename), _code.escape(edmtype)) :

_code.escape(edmtype));

if (edmtype is structuraltype)

if (edmtype is ******type)

typename = fixnamespaces(typename);

return clrtype.isvaluetype && isnullable == true ?

string.format(cultureinfo.invariantculture, "nullable<>", typename) :

typename;

}throw new argumentexception("edmtype");

}public type underlyingclrtype(edmtype edmtype)

if (isenumtype(edmtype))

return typeof(object);

}public bool enumisflags(edmtype enumtype)

public bool isenumtype(globalitem edmtype)

public primitivetype getenumunderlyingtype(edmtype enumtype)

public string createliteral(object value)

return string.format(cultureinfo.invariantculture, "new timespan()", ((timespan)value).ticks);

}public bool verifycaseinsensitivetypeuniqueness(ienumerabletypes, string sourcefile)

return true;

}public ienumerablegetitemstogenerate(ienumerableitemcollection) where t: edmtype

public ienumerablegetallglobalitems(ienumerableitemcollection)

public string getglobalitemname(globalitem item)

else

}}public static void argumentnotnull(t arg, string name) where t : class

}#>

T4模板語法

t4,即4個t開頭的英文本母組合 text template transformation toolkit。t4文字模板,即一種自定義規則的 生成器。根據業務模型可生成任何形式的文字檔案或供程式呼叫的字串。模型以適合於應用程式域的形式包含資訊,並且可以在應用程式的生存期更改 template deb...

T4模板 入門

t4模板作為vs自帶的一套 生成器,功能有多強大我也不知道,最近查詢了一些資料學習一下,做個筆記 更詳細的資料參見 msdn 你必須懂的t4模板 深入淺出 t4模板是沒有顏色高亮提示的,我們可以安裝vs支援的外掛程式,我安裝的是 t4 editor,當然還有其他比如 visual t4等,t4模板分...

CodeFirst時使用T4模板

我們都知道t4模板用於生成相似 在dbfirst和modelfirst條件下我們很容易從.edmx下獲取所有實體類和其名稱,並且通過我們定義的模板和某些遍歷工作為我們生成所需要的相似 但是codefirst模式下,我們沒有edmx,從哪獲取所有類名呢?難道要我們乙個個把entity實體層類名寫進陣列...