Razor Macros
Macros uses all the same tools as standard Razor Template. The primary difference between a Razor Template and a Macro is that the Macro will not be run during code generation.
Macros are useful if you need to run a task that will update the UML model via the API. For example: After importing a DLL you can run the "FindPrimaryKeys" macro in the "Entity Framework" code generator
Setting a Razor Template as a Macro
To set a Razor Template as a Macro, simple change the Output to "Macro".
Running the Macro
Press the "Preview" tab in the Razor Template editor and the macro will run. With any compile errors showing the Error window and any output showing in the "Preview" tab
Sample Macro
This is a code walk though of a macro for Finding Primary Keys Marco for the Entity Framework. The Code assumes the Primary Key Name is the same as the Class Name with "Id" appended to the end. e.g. The Primary Key for the "Person" Class is the property "PersonId"
First add the using statements to the top of the file
@using SilverDawn.SilverModel.UML.Extensions;
@using SilverDawn.SilverModel.UML.Interfaces.Classes;
@using SilverDawn.SilverModel.UML.Classes;
Next we loop through all the class and interfaces in the Model
@foreach (var classifier in Model.Diagram.Classifiers())
{
Then we loop through all the Properties of the classifier(class or interface)
foreach (var prop in classifier.AllProperties())
{
Then we check if the current Property is a Primary key.
if (prop.Name!=null && classifier.Name.ToUpper()==prop.Name.ToUpper().Replace("ID",""))
{
Then we output the found Primary Key. This will appear in the "Preview Tab"
@:PrimaryKey Set Classifier:@classifier.Name Property:@prop.Name
Then we update the Stereotype value for the Property
prop["Entity Framework","EF Primary Key"]="True";
Lastly is the closing braces
}
}
}
When the "Preview" tab in the Razor Editor is pressed the Macro will run.