T4 Template Generation for C# Poco Files
You have to develop a single page application and you would like to use typescript. These days most WebApplications receive their data from a restful api from the server. Hence you will get plain json content. If you work with C# on the server you typically have Pocos (Plain old CLR Objects), that you use as Dtos (Data transfer Objects). These Pocos are well typed of course, but on the way to the client you loose this typing. In case of using simple javascript you will not be worried about that. But if you use typescript on the client it would be nice to have well generated typescript interfaces for accessing your plain json objects. You could implement these interface on your own, but this is a lot of work and error prone.
With TeaForPoco you can automatically generate typescript model and enum classes and other useful files for your purpose.
- T4 Templates are fully integrated in Visual Studio.
- Easy installation, as you just need to copy some files into your project.
- It does not pollute your own project.
- You can modify or extend the templates for your own purposes.
- Available Visual Studio Plugins for T4 Template development (e.g. T4 Editor from Devart, Resharper Plugin TeaFor )
- Automatically regenarate on build
TeaForPoco consists of two parts:
- The core file (\TeaForPoco.Core.ttinclude) with plain C# code to read your Poco Files
- The T4 template files (\Typescript\enums.tt,\Typescript\models.tt, ...) that generates the output.
- Clone the project
- Open the Visual Studio Solution file (TeaForPoco.sln)
- Restore nuget packages for the project (right click on project and select Manage NuGet Packages...)
- Under Properties for the core file, set Build Action to none (otherwise you may get a generated output file)
- Run T4 Template generation (e.g. from VS menu: Build->Transform All T4 Templates)
- Check the generated files for the Sample Poco Files in SampleDto and SampleEnum folder. (\Typescript\enums.tt.generated.ts, \Typescript\models.tt.generated.ts)
- Copy the core file and T4 template files to your project wherever you need it.
- Optionally rename the T4 template files for your needs.
- Add missing NuGet Packages. (see Dependencies section)
- Check include path for NuGet Packages in core file.
- Set the reference path to the core file in your T4 template files. (see configuration section)
- Adapt configuration parameters in T4 template files. (see configuration section)
- Optionally add *.generated.ts to your .gitignore file.
TeaForPoco needs the following NuGet Packages
- Microsoft.CodeAnalysis
- Microsoft.CodeAnalysis.CSharp
The core file needs to include these files from the nuget package folder
<#@ assembly name="$(SolutionDir)\packages\Microsoft.CodeAnalysis.Common.1.3.2\lib\net45\Microsoft.CodeAnalysis.dll" #>
<#@ assembly name="$(SolutionDir)\packages\Microsoft.CodeAnalysis.CSharp.1.3.2\lib\net45\Microsoft.CodeAnalysis.CSharp.dll" #>
The first line in the T4 Template references the core file.
<#@ include file="TeaForPoco.Core.ttinclude" #>
Adapt the path correctly, for example
<#@ include file="$(SolutionDir)\MyProject\Resources\TeaForPoco.Core.ttinclude" #>
Each T4 Template file has a settings section with parameters.
- EnumPath -> Path to your enumeration files.
- EnumModuleName -> Name of the typescript module name.
- EnumPath -> Path to your enumeration files.
- EnumModuleName -> Name of the typescript module name for enums
(should match with the one in enums.tt) - PocoClassPath -> Path to your poco files.
- PocoClassFilenameSuffix -> Suffix of your Poco Files
(if you have one, otherwise all files in source folder are regarded as Poco files) - PocoClassSuffix -> Suffix of your Poco Classes
(if you have one and likes to remove it) - RemoveClassSuffix -> true/false, removes Class Suffix
- InterfaceModuleName -> Name of the typescript module name for typescript interfaces
- CustomPlurals -> Dictionary for Custom pluralisation
(only necessary if RemoveClassSuffix is true)
You can adapt the mapping in models.tt within the Static Declarations section
C# | Typescript |
---|---|
string | text |
int | number |
double | number |
bool | boolean |
DateTime | Date |
Please add more if you need it!
Reference Types will be provided by your Poco Classes or Enums you provide.
If the template could not resolve a type, you will get a warning.
Array Types in C# are mapped to corresponding Array Types in Typescript.
Generic Types can be nested.
Generic Collection Types in C# are mapped to Arrays in Typescript.
These types are configured in models.tt in the Static Declarations section and are the following:
- Collection
- List
- Array
- Enumerable
Please add more if you need it!
Generic Dictionary Types in C# are mapped to nested Arrays in Typescript.
These types are configured in models.tt in the Static Declarations section and are the following:
- Dictionary
Please add more if you need it!
Nullable types in C# are directly mapped to a non-nullable typescript type, because all types in typescript are nullable.
(It is not mapped to optional types, marked with "?")
You can also write your own T4 templates and generate individual output, by using the TeaForPoco.Core.ttinclude file.
The ReadEnums Method tries to read all your C# files in the configured folder.
It returns a list of EnumItem classes described below (see Output Class Diagram section).
The ReadPocoClasses Method tries to read all your C# files in the configured folder.
It omits interfaces within this folder and returns a list of PocoClass classes described below (see Output Class Diagram section).
- Name -> Name of the enumeration.
- EnumMemberItems -> List of EnumMemberItem objects.
- Name -> Name of the enumeration item.
- Value -> Value of the enumeration item . (if it has a value assigned)
- Name -> Name of the poco class.
- BaseClasses -> List of base classes the poco inherits.
- BaseInterfaces -> List of interfaces the poco implements.
- PocoProperties -> List of PocoProperty objects.
- Name -> Name of the property
- TypeSyntax -> TypeSyntax Object (used from Roselyn)
MSDN (T4)
Wikipedia
T4 Template Tutorials and Solutions
Pluralsight Course (T4)