t4模板的认识

1
输出类
<#=codeStringGenerator.EntityClassOpening(entity)#>

public string EntityClassOpening(EntityType entity)
{
return string.Format(
CultureInfo.InvariantCulture,
"{0} {1}partial class {2}View{3}",// public partial class sysFunction
Accessibility.ForType(entity),
_code.SpaceAfter(_code.AbstractOption(entity)),
_code.Escape(entity),
_code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)));
}

 

2 构造函数

public <#=code.Escape(entity)#>()
{
<#
foreach (var edmProperty in propertiesWithDefaultValues)
{
#>
this.<#=code.Escape(edmProperty)#> = <#=typeMapper.CreateLiteral(edmProperty.DefaultValue)#>;
<#
}

foreach (var navigationProperty in collectionNavigationProperties)
{
#>
this.<#=code.Escape(navigationProperty)#> = new HashSet<<#=typeMapper.GetTypeName
(navigationProperty.ToEndMember.GetEntityType())#>>();
<#
}

foreach (var complexProperty in complexProperties)
{
#>
this.<#=code.Escape(complexProperty)#> = new <#=typeMapper.GetTypeName(complexProperty.TypeUsage)#>();
<#
}
#>
}

 

3 命名空间
<#=codeStringGenerator.UsingDirectives(inHeader: false)#>

 

using Model;


/// <summary>
/// 数据层
/// 成东
/// </summary>

<#=codeStringGenerator.EntityClassOpening(entity)#>


4生成属性

<#=codeStringGenerator.EntityClassOpening(entity)#>
{

#region 此表的特殊操作

#endregion


<#
var propertiesWithDefaultValues = typeMapper.GetPropertiesWithDefaultValues(entity);
var collectionNavigationProperties = typeMapper.GetCollectionNavigationProperties(entity);
var complexProperties = typeMapper.GetComplexProperties(entity);

if (propertiesWithDefaultValues.Any() || collectionNavigationProperties.Any() || complexProperties.Any())
{
#>

<#
}

var simpleProperties = typeMapper.GetSimpleProperties(entity);
if (simpleProperties.Any())
{
foreach (var edmProperty in simpleProperties)
{
#>
<#=codeStringGenerator.Property(edmProperty)#>
<#
}
}

if (complexProperties.Any())
{
#>

<#
foreach(var complexProperty in complexProperties)
{
#>
<#=codeStringGenerator.Property(complexProperty)#>
<#
}
}

var navigationProperties = typeMapper.GetNavigationProperties(entity);
if (navigationProperties.Any())
{
#>

<#
foreach (var navigationProperty in navigationProperties)
{
#>
<#=codeStringGenerator.NavigationProperty(navigationProperty)#>
<#
}
}
#>


}


4 生成引用
<#=codeStringGenerator.UsingDirectives(inHeader: false)#>
下面我们可以自己加别的引用
using System;

 

posted @ 2015-04-29 22:17  陌念  阅读(207)  评论(0编辑  收藏  举报