Custom Templates
You can write Custom Templates if the Built-in Templates do not support your code generation needs.
How it works
Dash parses, validates and processes a Model File to build an Internal Model. This Internal Model is passed to the Template Engine and then interpreted by the Template to actually generate the (C#) code.
The process visualized:
[ insert image ]
Internal Model
Warning
To write Custom Templates, you should have a good understanding of the Internal Model.
To explain the Internal Model, let's take a look at a JSON representation of an Internal Model:
{
"Namespace": "Foo",
"ModelName": "MyModel",
"Entities": [{
"Name": "User",
"CodeAttributes": [{
"Name": "Id",
"DataType": "int",
"IsNullable": false,
"DefaultValue": null
}],
"SingleReferences": [],
"CollectionReferences": [],
"SeedData": []
}]
}
Internal Model
Namespaceis the namespace of the application.ModelNameis the name of the Model, which is inferred from the filename.Entitiesis an array of objects:Namecontains the value that represents the .NET class name.CodeAttributes: is an array of objects:Namecontains the value that represents the membername of a class.DataTypeis the C# data type name, e.g.bool,int.IsNullableindicates that the member is (not) nullable.DefaultValuecontains the default value of the member (nullif there is no default value)
SingleReferencesis an array of objects:ReferenceNamerepresents a property name that references another class.EntityModelcontains the classname that is being referenced.IsNullableindicates that the property is (not) nullable.
CollectionReferencesis an array of objects:ReferenceNamerepresents a property name that holds a collection of objectsEntityModelcontains the classname that is being referenced.IsNullableindicates that the property is (not) nullable.
Warning
The values of all Internal Model properties should be respected by the Template and used as-is. Any special logic (e.g. pluralizing nouns) should be left to the Dash processing pipeline.
If what you want to achieve is not supported, then please visit the Dash Github Repo to open an issue or see how you can contribute to the codebase.
Template Engine
Dash uses Scriban as its template engine. The Scriban language is similar to Liquid. To learn more about the Scriban syntax, please visit the documentation on the Scriban templating language.
Hello World
The following is a simple Custom Template that iterates through all classes inside the Internal Model:
namespace {{ namespace }}
{
{{ for e in entities }}
public class {{ e.name }}
{
}
{{ end }}
}
When the above Internal Model is used as the input, the generated code will be:
namespace Foo
{
public class User
{
}
}