Show / Hide Table of Contents

Walkthrough: Creating your first code Generator

Walkthrough

First click the "Generators" tab in the menu bar, followed by the add button.

Add

This will great a new Generator in the "Generators" window.

NewGen

You can rename the generator in the properties window

Rename

Rename it to "MyGen" and press enter

Then press the "Add" button in the Template menu to add a new template file. This file will allow you to create you first code generator

AddTempate

In the Code Generator Explorer double click the "new1.razor" file to edit the Code Generator Template

NewTemplateFile

Enter this code to create you first generator

@using SilverDawn.SilverModel.UML.Extensions;
@using SilverDawn.SilverModel.UML.Interfaces.Classes;
@using SilverDawn.SilverModel.UML.Classes;


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SilverDawn.SilverModel.UML.Interfaces.Classes;

namespace @Model.Model.QualifiedName
{
    public class @Model.Class.Name
    {
        @foreach (var att in Model.Class.OwnedAttributes)
        { 
        <text>public @Model.RazorSupport.LookupDataType("C#",att.DataType)</text> @att.Name <text> { get; set; } </text>
        }
    }
}

You now have a generator you can assign to a model and generate code.

Explantion of the code parts

The template are all written using the Razor templating engine. Razor Syntax

@using SilverDawn.SilverModel.UML.Extensions;
@using SilverDawn.SilverModel.UML.Interfaces.Classes;
@using SilverDawn.SilverModel.UML.Classes;

These references enable the Razor templating engine to access the UML models of SilverModel.

namespace @Model.Diagram.QualifiedName

The @Model is the Razor Engine Model. All information from SilverModel is passed to Razor using this Model.

Diagram is the current Class Diagram been passed to Razor.

QualifiedName get the UML Name for this diagram.

namespace @Model.Class.Name

Gets the name for the current Class been passed to Razor

@foreach (var att in Model.Class.AllAttributes())

This line creates a loop that iterates through all the Attributes owned by the class.

<text>public @att.Type.TypeName("C#")</text> @att.Name <text> { get; set; } </text>

This line outputs the datatype for the specified computer language and gets the name of the property.

Back to top Generated by DocFX