Code Generation - STARIONGROUP/Kalliope GitHub Wiki

Code Generation

The purpose of Kalliope is to support convention-based and template-based code generation. Multiple templating engines are available in the .NET ecosystem such as DotLiquid and Handlebars.NET. More temaplating engines exist and of course they can be used as well, the reason to use one is basicaly determined by "what you like best".

A part of the Kalliope library has been code-generated using DotLiquid, read more about it here.

Codegeneration can be used to generate anything that is relevant to your project, it can include generation of the following kinds of concepts:

  • HTML
  • C# code such as classes, interfaces, enumerations, structs, etc.
  • XML Schema
  • SQL Schema, SQL queries, etc
  • Graph Schema and Graph queries such as Cypher, SPARQL, etc.
  • Object Relational Mappings (not to be confused with Object Role Modelling)

In order to use Kalliope it needs to be installed in your C# project: dotnet add package Kalliope.Xml

The first step in code generation is to read an ORM model and to have it available as an object graph in memory. This is done using the OrmFileReader. The following code-snippet demonstrates how this is done:

    var ormXmlReader = new OrmXmlReader();
    var fileReader = new OrmFileReader
    {
        OrmXmlReader = ormXmlReader
    };

    var path = "path-to-an-orm-model";

    fileReader.Read(path);

    var cache = this.fileReader.Assembler.Cache;
    Lazy<Kalliope.Core.ModelThing> lazyPoco;
    cache.TryGetValue("root:unique-identifier-of-the-root-model-entity", out lazyPoco);
    var ormRoot = (Kalliope.OrmRoot)lazyPoco.Value;

The ormRoot variable in the above code-snippet is the ORM Model class which provides access to the content of the model such all the EntityTypes, ValueTypes, etc.

DotLquid

A DotLiquid template is rendered in two steps: Parse and Render. A template must follow the DotLquid syntax.

Template template = Template.Parse("{{name}} is awesome!");  // Parses and compiles the template
template.Render(Hash.FromAnonymousObject(new { name = "Kalliope" })); // Renders the output => "Kalliope is awesome"

The Parse step creates a fully compiled template which can be re-used. You can store it in memory or in a cache for faster rendering later. All parameters you want to use in your templates have to be passed as parameters to the Render method.

The complete documentation is available here.

Handlebars.NET

A handlebars template is rederened in two setps: Compile and Render. A template must follow the Handlebars syntax.

var template = Handlebars.Compile(@"{{ this.Name }} is awesome!"); // Parses and compiles the template
var result = template(new { Name = "Kalliope" }); // Renders the output => "Kalliope is awesome"

The Parse step creates a fully compiled template which can be re-used. The object you want to use in your templates are passed to the template object.

The complete documentation is available here