New Feature: Preserving User Code Changes during Code Generation with Razor
This new feature will revolutionise how code generation is used by helping to preserve any custom code changes made to the output code generated files.
When using code generation traditionally, one big issue is that if any changes were made to the output file, these are overwritten during the next code generation run. This is not an ideal situation!
Fortunately, SilverModel has several protections built into it to preserve user code changes in the output files which are:
- Checksum – to detect changes made to the output files and stops the file been overwritten.
- User Code Blocks – predefined locations were custom code from a programmer can be created in the code generated file. This code will be preserved and keep during code generation!
The rest of this article will focus on the User Code Blocks, which is a new feature in SilverModel. For more information on how the checksum works, please refer to the online documentation
User Code Blocks
User Code Blocks are predefined sections within a Razor Template. They define where in file output by Razor that custom code can be inserted.
User Code Blocks have had a major effect on how I use code generation. Now I can update and insert code into the generated files and not have to worry about losing the changes! With a lot of the code I have generated in the past, it has got me 95% to 98% of the way there. The last few percent of the code has always been custom coded. The biggest issue being that if I updated a code generated file, I could no longer use the latest version of this generated file, as any custom changes made would block the file been overwritten (or I would lose my changes). Now with User Code Blocks, I can make the custom changes as required, and SilverModel will keep these changes. So now I get the best of both worlds. Code Generation to create the bulk of my code and then use custom code when required.
During code generation, before Razor overwrites the file. SilverModel examines the existing file, for any custom code in the User Code Blocks to save. These saved User Code Blocks are then merged into the latest code generated file and saved to disk. Thus, preserving any custom code changes made, this is a major improvement in code generation!!
The User Code Blocks currently works with C style languages, e.g. C, C++, C#, Java, JavaScript, Kotlin, Scala and Typescript. Support for other languages like HTML and XML will be coming.
How to use User Code Blocks
To define the start of User Code Block in a Razor template use //UserCodeBlockStart- then after the ‘-’ give the User Code Block a name, e.g. //UserCodeBlockStart-PreSave.
To end the User Code Block use //UserCodeBlockEnd- and give it the same name as the starting User Code Block. e.g. //UserCodeBlockEnd-PreSave
Example of a User Code Block in a Razor Template
A User Code Block is simple and quick to define in the Razor Template. The code for this example is also on GitHub.

Example of User Code Block in the outputted file.
Whenever Razor does code generation all code within the User Code Block will be preserved. The code for this example is also on GitHub.

Ideas on where to use User Code Blocks
Here are some quick ideas on places where User Code Blocks can be used.
- Before saving a database record.
- Add other values
- Custom validation code
- Creating a data view
- Insert information from a third-party service
- Override values
- User Screens
- Add extra controls
- When a button is pressed, execute other actions.
- etc ,etc
User Code Blocks can be placed anywhere in a code generated file, so the possibilities are endless.
Closing
User Code Blocks are an exciting and powerful addition to SilverModel that enables custom code to be persevered during code generation.
User Code Blocks will open a lot of new possibilities on how and where code generated files can be used, as now code generated file can be safely modified as required.
If you have any questions please ask, and we will be happy to help.