Skip to content

Code Generator Template Structure

samatrhea edited this page Apr 29, 2024 · 3 revisions

The code-generator templates contain logic that determines what the rendered output looks like. The generator templates are located in the Templates folder. The following applies to the templates:

  • The templates extension shall be .hbs
  • The build action property of the file shall be set to None
  • The Copy to Output Directory property of the file shall be set to Copy Always.

The code-generator makes use of Handlebars.Net which is a .NET implementation of Handlebars.js templates.

Handlebars uses a template and an input object to generate text formats (C#, SQL, HTML, etc.). Handlebars templates look like regular text with embedded Handlebars expressions. A handlebars expression is a {{, some contents, followed by a }}. When the template is executed, these expressions are replaced with values from an input object. Checkout the handlebarsjs.com for the complete documentation.

The following provides a typical structure for a generated C# soure file:

Copyright Header

SysML2.NET is distributed with the APACHE 2.0 license, each file needs to contain the following copyright header:

// -------------------------------------------------------------------------------------------------
// <copyright file="{{this.Name}}.cs" company="Starion Group S.A.">
//
//   Copyright 2022-2024 Starion Group S.A.
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//        http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
//
// </copyright>
// ------------------------------------------------------------------------------------------------

Generated Code warning

In order to make it clear that a source file is generated it needs to contain the following notice at the beginning (after the copyright header) and at the end of the file:

// ------------------------------------------------------------------------------------------------
// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!--------
// ------------------------------------------------------------------------------------------------

Partial Classes

C# supports the concept of partial classes and methods. It is possible to split the definition of a class, a struct, an interface or a method over two or more source files. Each source file contains a section of the type or method definition, and all parts are combined when the application is compiled.

To split a class definition the partial keyword modifier is used, see example below:

... copyright header ...

... autagen notice ...

namespace SysML2.NET.Core.DTO
{
   public partial class LiteralString : ILiteralString
   {
      ... generated code
   }
}

... autagen notice ...