Part 3 - Using CodeGenHero

In this part of the tutorial, we will cover how to use the CodeGenHero extension to dynamically generate code for our Solution.

Prerequisites

This part of the tutorial assumes that you have:

Getting started with CodeGenHero

Now that you have the custom project templates installed, let's put them to use by scaffolding out a multi-tiered solution. We will use this solution in the next part of the tutorial to generate code.

  1. Make sure you have your ArtistSite solution open.
  2. Now that we have the CodeGenHero extension installed, we want to open up its main window.
    Click on the Extensions | CodeGenHero | CodeGenHero Main Window in the menu.
  3. In the CodeGenHero window, you will be prompted to log in under “Already a Hero?”
    If you have not done so already, makes sure that you confirm your email using a link sent to you after registration. Then, enter the credentials you used when registering on the CodeGenHero.
  4. Single-click on the MSC.ArtistSite.CodeGenHero project to select it so that it is highlighted.


    Then, click the Set CodeGenHero Project button.

    This action instructions the CodeGenHero extension to store the configuration files it uses, such as metadata and blueprint files, in this project.
    Click Yes to confirm that you do, indeed, want to store the configuration files in this project.
  5. Once this is done, the Metadata tab will activate.
    Click the Next Step: Metadata button in the bottom-right of the CodeGenHero window to continue.
Metadata

Generating code relies upon something called metadata. In the case of CodeGenHero, that metadata is derived from a relational database. In this section, we will walk through how to create the metadata source that works with CodeGenHero templates.

  1. Click the Create New button near the top of the Metadata tab
  2. Click the New button in the Create Metadata Source window that opens.
  3. In the Connection Properties window that opens, enter your server name, authentication details, and the name of the database on the server you wish to use.

    Your server name will be (localdb)\MSSQLLocalDB”, if you are following along from the previous part of this tutorial.
    Likewise, we can use Windows Authentication and the database name is ArtistSite. Click the Test Connection button to confirm everything works and then click the OK button when you are done.
  4. In the Create Metadata Source window, review the generated default settings.
    Here you can make changes to the namespace that will be used for Entity Framework classes as well as the name for the DbContext class itself. You can also give this metadata source a name and description to differentiate it from other metadata sources you may have in a solution.
    We will only have a single metadata source in this tutorial, so the default values are probably fine.

    Make a note of the DbContext namespace and Class Name you chose. We will need these values later in the tutorial.
  5. Tick the checkbox for Generate Model Classes
    Click the Choose Output Folder button and navigate to set a value of the Entities folder of the Repository project. Depending on the folder location where your solution is located, you should end up with a value that is something like "C:\Users\<YourUserName>\source\repos\MSC.ArtistSite\MSC.ArtistSite\MSC.ArtistSite.Repository\Entities"


    Click the OK button at the bottom of the Create Metadata Source window.
    Wait a few seconds while the tool analyzes the database and generates a DbContext class and entity classes that are compatible with Entity Framework. After some time, a new entry should appear in the My Metadata Sources grid.
  6. Now that we have metadata, the Blueprint and Templates tabs should become enabled.
    Click the Next Step: Metadata button to continue.

Blueprint

Next, we will change the name of the Blueprint file that is generated by default. For context, Blueprint files are used by the CodeGenHero extension to store settings such as which of the available templates are selected for code generation as well as parameter values for those templates.

Out of the box, CodeGenHero will create a default Blueprint named “Sample Blueprint”. Even though our tutorial will only have a single blueprint, we want to rename this file to something that makes more sense. In more complex solutions, this practice can help us keep things organized, as there can be multiple blueprint and metadata file combinations.
  1. Click on the row containing the Sample Blueprint item in the My Blueprints table.
  2. Edit the Name and Description fields in the Edit or Create Blueprint section to be "ArtistSite Blueprint" and "Blueprint for ArtistSite code generation", respectively.


    Click the Save button.
  3. Click on Next Step: Templates to navigate to the next tab where we will load and select templates to generate.


MSC Technology Consulting #MSCTek


Copyright © MSC Technology Consulting 2021.3.2

An error has occurred. This application may no longer respond until reloaded. Reload 🗙