Introduction Last updated: 12-20-2020

CodeGenHero™ is a developer productivity tool that can shave weeks off of a project schedule, reduce the cost of change, and improve code quality.

The concepts of source code generation, generative programming, and metaprogramming have been around for a very long time. The central concept is to develop programs that use input parameters, metadata, or both to output code themselves.

CodeGenHero™ takes a small input, such as a database connection string, and uses it to output a large number of coordinated class files that serve a targeted purpose. In this documentation you will see how this tool can reverse-engineer information about the tables, fields, and data types in a database. It uses that schema metadata to automatically generate a fully-functioning REST API along with a class that makes it easy to retrieve or update data from a client application. As the database schema changes in response to new requirements, keeping the Web API up to date is as simple as re-running the code generator. This process is one way you, the developer, can shorten the time it takes to iterate changes and improve the feedback loop that is critical to project success.

The CodeGenHero™ Visual Studio extension reverse engineers your database schema and stores that metadata for use by code generation templates. Templates use this metadata to output any number of useful class files. In fact, while the templates demonstrated in this chapter output C# code, there is no reason why templates you use or write yourself cannot output F#, Blazor/Razor syntax, XAML, Visual Basic, React, Angular, JavaScript, PHP, or anything else.

Registration

Registration is easy, simply sign up Here for a new account. Once registered, use the same user name and password to sign in to the tool.

Login to the extension with the same credentials for the account you create on the CodeGenHero™ website.

CodeGenHero™ Windows

Once you have installed the CodeGenHero™ Visual Studio Extension, you can open the CodeGenHero™ Main Window by accessing the Extensions > CodeGenHero > CodeGenHero Main Window menu item.

Open the CodeGenHero™ Main Window from the Extensions top menu item in Visual Studio 2019.

Much of the work with CodeGenHero™ is done within the Main Window. The CodeGenHero™ Notification Center is a vertical window that reports the results of code generation events on a template-by-template level.

Like all windows inside of Visual Studio, the CodeGenHero™ Main Window and Notification Center Window can be snapped and docked anywhere. We suggest you set up your workspace in the following way, but obviously, you can do whatever makes you most happy.

CodeGenHero™ Main Window docked in the center, and Notification Center Window docked just below the solution explorer in Visual Studio.

CodeGenHero™ Projects

If you have the CodeGenHero™ main window open, but do not have a Visual Studio solution open, you will see an area on the welcome tab asking you to create a new or open an existing solution.

Open a solution or create a new one.

CodeGenHero™ needs to store data such as the blueprint and metadata in your Visual Studio project. You will need to designate a specific project in your solution to store these configuration files. Highlight the project in your solution that you would like to store the CodeGenHero™ config files and click the 'Set CodeGenHero Project' button. This action will create a folder called 'CodeGenHero' in the selected project. This folder will store the Blueprint and Metadata required to generate code. You will only need to set the CodeGenHero™ Project once for your solution.


The CodeGenHero™ project folder

At this point, it is good to understand that CodeGenHero™ stores metadata and template input values, aka blueprints, in a folder named CodeGenHero. This folder gets located in whatever project is set using the Set CodeGenHero™ Project button in the image below. The reason CodeGenHero™ stores information in files within the solution is to facilitate source control and enable the sharing of settings between developers.

Existing CodeGenHero Project in Solution

If you are opening a solution that already has a CodeGenHero™ Project in it, the extension should recognize that immediately. If for some reason, it does not, or if you would like to change it, you can hightlight the proper project and set that project by clicking th ered button pictured below.

Existing CodeGenHero™ Project

Metadata

Metadata is information that describes other data (data about data). In CodeGenHero™, the output of the metadata source extraction process are stored in files with a .cghm file extension.

Currently, the metadata used by the CodeGenHero™ templates focus on information about relational database tables, fields, and relationships between entities. This makes CodeGenHero™ especially useful for projects in which a legacy database exists and a new application is required to interact with that legacy database. In future releases, CodeGenHero™ will support Metadata Sources that are not databases, like a code-first workflow.

CodeGenHero™ Metadata Tab and default location of metadata file.

Creating a New Metadata Source

Creating a new metadata source involves connecting to an existing database and setting the class name and namespace for your Entity Framework Core DbContext class. One very cool option is to have CodeGenHero™ also generate and output model classes for your EF DbContext (that is, POCO’s and DbContext itself). This option reverse engineers the database, outputs code first model classes, and avoids the drudgery associated with manually writing all that code!

Create a New Metadata Source

Edit or Refresh Metadata Sources

{Say something here about refreshing metadata sources}

Exclude Navigation Properties

{Say something here about excluding navigation properties}

Blueprints

In CodeGenHero™, the input parameters required for code generation are stored in blueprints, using files with a .cghb file extension. These are the values that drive template output. As a convention, they are stored in the blueprint folder in a CodeGenHero™ project.

CodeGenHero™ Blueprint Tab

Note that it is possible to have multiple blueprint files and have each file contain different configuration settings. Thus, we could have made a second file to keep the configuration for client-side concerns separate from server-side concerns. This can be useful on teams where, say, mobile developers are separate from the API developers.

Creating a New Blueprint

{Say something here about creating blueprints}

Edit or Refresh a Blueprint

{Say something here about refreshing blueprints}

Templates

CodeGenHero™ Tempates allow developers to generate the architecture that they need. Because templates often complement each other, we offer groups of temples that we affectionately call Template Bundles. Individual templates become exponentially more powerful when used in synergy with other templates to create coordinated template bundles.

You can generate code with our templates or use them to make your own.

Loading CodeGenHero™ Templates

To upload template bundles, navigate to the TEMPLATES tab, then the Template Bundles inner tab. Template bundles use the .cghpkg extension.

CodeGenHero™ Templates Tab
CodeGenHero™ Blueprint Tab

Open Source CodeGenHero™ Templates

CodeGenHero™ templates, NuGet packages, and sample projects are open source. If you do create your own templates or want to contribute to the many NuGet packages available at github.com/MSCTek/CodeGenHero, then pull requests are welcome!

Need some template bundles? Visit the CodeGenHero™ Download Page.

Configure

Template parameters are configured using global and template specific variables. This is how the output from template bundles, as a whole, work together.

Global Variables

Global variables represent parameters that are used by more than one of the templates that you have selected.

CodeGenHero Templates Tab
CodeGenHero™ Global Template Variables

Each Template Bundles will have their own specific set of variables. The following are global variables commonly found in the templates created by the CodeGenHero™ team. If you are building your own templates, feel free to use these template variables.

BaseNamespace and NamespacePostfix

DTONamespace

DBContextName

PrependSchemaNameIndicator (boolean)

RegexInclude and RegexExclude

RepositoryNamespace, RepositoryEntitiesNamespace and RepositoryInterfaceNamespace

UseAuthorizedBaseController (boolean)

WebApiControllerNamespace

WebApiDataServiceInterfaceNamespace

Template Variables

Template variables represent parameters that are specific to a single template being generated.

CodeGenHero Templates Tab
CodeGenHero™ Template Variables
Hidden Template Variables

Can I convert a template variable to a global variable?

System Variables

{Insert info here}

TableName

TablePluralName

Map

The Map tab in CodeGenHero™ lists all the templates previously selected in the Templates tab. Next to each template, use the dropdown to choose the desired output project as well as a directory within that project to place the generated file(s). Templates can be designed to output more than one file. For example, a separate Web API controller or data transfer object class (DTO) might be generated for each table in the database.

CodeGenHero Map Tab
Map Templates

Merge

The CodeGenHero™ Merge Tab provides a table-based comparison of generated output with the corresponding code in your solution. Based on this, a recommended action is offered on a case-by-case basis.

CodeGenHero™ Merge Tab
Merge Tab
Filtering

You can filter the table on the merge tab by Action or by Template name by using the combo boxes and the Clear Filters Button.

Select All Button

Check or uncheck all of the records in the Merge Table.

Update Selections Button

Process Selected Files Button

Execute the selected action for files represented by selected records in the table. Use this button if you are done identifing code that needs to be added, merged, deleted or updated in your solution.

Re-Analyze Actions Button

Re-evaluate the suggested action for selected records. Use this button if you are actively changing files in your solution and would like to identify those files that have changed.

Generate All Code Button

Regenerates all code. All code is automatically generated when you activate this tab.

Save Generated Code Button

Creates a zip file of all generated code.

Settings

Clicking on the gear icon in the upper right corner of the CodeGenHero Main Window will popup a Settings dialog with the following options:

Auto Generate Code Toggle

When enabled, code will be re-generated when templates change and when you naviate to the Merge tab. The default setting is "On".

Output Diagnostic Messages Toggle

When enabled, diagnostic messages will be displayed in the CodeGenHero™ Notification Center Window. The default setting is "Off".

Telemetry Level Combo Box

In an effort to provide a product that accurately works, CodeHenHero™ collects telemetry data. The default telemetry level is "WARN".