Skip to content

rogerwcpt/code_generation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 

Repository files navigation

Code Generators in .NET Core

This project attempts to explore and compare the code generation options available in .NET Core, specifically those that are template based and generate code from a given model.

The templates that are included so far are

  1. Razor Templates (reference docs)
  2. T4 Templates (reference docs)
  3. XSL Templates (reference docs)
  4. Scriban or Liquid Templates (reference docs)
  5. Mustache Templates (reference docs)

Razor Templates

Razor Templates have clean syntax and a pleasure to use, but strangely there is no Razor Engine in .NET 5.0 so one has to rely on a project on a GitHub project https://github.com/adoconnection/RazorEngineCore

Advantages

  • Clean minimal razor syntax
  • Supports embedded C# methods
  • Most IDEs have great intellisense ,despite being a standalone template

Disadvantages

  • Not part of the official .NET Core offering
  • Much slower than other generators

Sample

<h3>FullName: @Model.FullName</h3>
<h3>Skills:</h3>
<ul>
@foreach (var skill in Model.Skills)
{
    <li>@skill</li>
}
</ul>

T4 Templates

T4 templates have been around a long time, but similar to the Razor Templates there is no offical support in NET 5.0 so one has to rely on a GitHub project https://github.com/mono/t4

Advantages

  • Can embed C# helper methods to aid in code generation
  • Faster because the template has a pre-compiled code-behind generated by the IDE

Disadvantages

  • T4 Syntax Feels outdated syntax compared to razor
  • Relies on IDE to generate a precompiled C# code behind
  • Not part of the official .NET Core offering

Sample

<#@ template language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>

<h3>FullName: <#= Model.FullName #></h3>
<h3>Skills:</h3>
<ul>
<# foreach (var skill in Model.Skills)  { #>
    <li><#=skill#></li>
<# } #>
</ul>

Xsl Template

Xsl templates have been around for decades and a W3C standard and has been supported since .NET 1.0

Advantages

  • Is a an official W3 standard.
  • Supported in from .NET 1.0 to latest .NET and .NET Core
  • Most IDEs have great intellisense ,despite being a standalone template

Disadvantages

  • Geared more for xml-type (includingHTML) output
  • Syntax not as simple as Razor
  • Have to deal with xml namespaces and verbose syntax
  • Cannot embed C# helper methods to aid in code generation
  • .NET Core still only suports XSLT 1.0

Sample

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:template match="/PersonModel">
        <h3>FirstName: <xsl:value-of select="FullName"/></h3>
        <h3>Skills:</h3>
        <ul>
            <xsl:for-each select="Skills/string">
                <li><xsl:value-of select="."/></li>
            </xsl:for-each>
        </ul>
    </xsl:template>
</xsl:stylesheet> 

Scriban/Liquid Templates

Liquid Templates, like Razor, have clean and minimal syntax. Liquid is an open-source template language created by Shopify and written in Ruby. Scriban project (which supports Liquid templates as well) can be found here: https://github.com/scriban/scriban

Advantages

  • Very fast

Disadvantages

  • Syntax might get confusing if you're used to Razor syntax
  • Can't embed C# helper methods

Sample

<h3>FullName: {{ full_name }}</h3>
<h3>Skills:</h3>
<ul>
    {% for skill in skills %}
    <li> {{ skill }} </li>
    {% endfor %}
</ul>

Mustache Templates

Mustache Templates, are very widely used in html but also for general code generation. They are logic-less templates in that the contain no for-loops or logic A fairly active GitHub repo (at the time of this commit) thta supports Mustache Templates is https://github.com/stubbleorg/stubble

Older Ports
Package Version Downloads LastUpdated
Stubble 1.9.3 445,197 23/05/2020
Mustachio 2.1.0 49,492 24/06/2019
Nustash 1.16.0.10 238,28 21/04/2019
Mustache-Sharp 1.0.0 194,509 15/07/2018
*Stats taken from nuget.org on 13 June 2021

Advantages

  • Very fast

Disadvantages

  • No logic can be embedded in the templates

Sample

<h3>FullName: {{FullName}}</h3>
<h3>Skills:</h3>
<ul>
{{#Skills}}
    <li> {{.}} </li>
{{/Skills}}
</ul>

Performance

Engine Time (ms)
T4 3
Mustache 91
Liquid 127
Xsl 303
Razor 2392

Nuget/Repo Stats as of 8 June 2021

Package Repo Stars Most downloads for a version in the last year
Mono.TextTemplating (for T4) 215 6,788
Stubble.Core (for Mustache) 258 427,328
Scriban (for Liquid) 1,500 86,091
Xsl n/a n/a (part of .NET Core)
RazorEngineCore 205 28,379

Template Output

About

Comparison Of Code Generation techniques available in .NET Core

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published