This is a sample to create a table Entity class using T4.
In Japanese, see also .NET Core+VS CodeでもT4 テンプレートエンジンでコード生成したい!
-
.NET Core 3.0 SDK (Preview 7)
-
Visual Studio Code
- Required: C# Extension
- Recommended: T4 Support to highlight T4 Documents.
T4 means "Text Template Transformation Toolkit". It is bundled to Visual Studio since 2008.
It is often used for automatic code generation such as table definition classes.
This sample uses Mono.TextTemplating which is a T4 implemented by Mono Project.
> dotnet new console
- Set
LangVersion
to 8.0.
<PropertyGroup>
+ <LangVersion>8.0</LangVersion>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
The program code generated by T4 file depends on this package.
> dotnet add package Mono.TextTemplating --version 2.0.5
Add DotNetCliToolReference
in csproj to use dotnet-t4-project-tool.
<ItemGroup>
+ <DotNetCliToolReference Include="dotnet-t4-project-tool" Version="2.0.5" />
<PackageReference Include="Mono.TextTemplating" Version="2.0.5" />
</ItemGroup>
Define TextTemplate
and Genarated
using Glob.
<ItemGroup>
<DotNetCliToolReference Include="dotnet-t4-project-tool" Version="2.0.5" />
<PackageReference Include="Mono.TextTemplating" Version="2.0.5" />
+ <TextTemplate Include="**\*.tt" />
+ <Generated Include="**\*.Generated.cs" />
</ItemGroup>
Define Task to run dotnet t4
.
%(TextTemplate.Identity)
- T4 File Paths such as foo/Template.tt.
$(RootNameSpace)
- Default Namespace. If you changed the T4 class's namespace, set it exactly.
%(TextTemplate.Filename)
- T4 File Name.
</ItemGroup>
+ <Target Name="TextTemplateTransform" BeforeTargets="BeforeBuild">
+ <Exec WorkingDirectory="$(ProjectDir)" Command="dotnet t4 %(TextTemplate.Identity) -c $(RootNameSpace).%(TextTemplate.Filename) -o %(TextTemplate.Filename).Generated.cs" />
+ </Target>
Define Task to delete *.Generated.cs
.
</Target>
+ <Target Name="TextTemplateClean" AfterTargets="Clean">
+ <Delete Files="@(Generated)" />
+ </Target>
TransFormText()
, Called by Program to use T4 Classes are defined in *.Generated.cs.
But *.Generated.cs is generated just before the build.
So calling TransFormText()
will cause a compile error because there is no definition at before build time.
This sample avoids this by using the "default implementation of interfaces".
using System;
namespace T4Sample
{
public interface ITextTemplate
{
string TransformText() => throw new NotImplementedException();
}
}
Note that T4 class should be defined with partial
class.
Note that variable type should be an interface. Don't use var
or TableEntityTemplate
.
> dotnet build
TableEntityTemplate.Generated.cs
is generated and the program is built including it.
dotnet run
does not cause Beforebuild-Event, you should execute dotnet build
first.
> dotnet run
using System;
using System.Collections.Generic;
namespace MyNameSpace
{
/// <summery>
/// Stores employee information.
/// </summery>
public class StaffMaster
{
public int StaffId { get; }
public string StaffName { get; set; }
/// <summery>
/// Home Address.
/// </summery>
public string Address { get; set; }
public DateTime CreatedDate { get; set; }
public StaffMaster(
int staffId
)
{
StaffId = staffId;
}
public StaffMaster(
int staffId,
string staffName,
string address,
DateTime createdDate
)
{
StaffId = staffId;
StaffName = staffName;
Address = address;
CreatedDate = createdDate;
}
}
}