I recently created a Code Generation tool that targets a set of entity items in a .NET project – to generate an entire .NET stack that includes a Service, Business and Data Access Layers. Because Entity Framework has database migration tools I can leverage my Entity Framework DbContext (that is generated) to also create a database based on the specified DbContext. I can do this in less than 10 minutes. I think that is productive, right?
The application leverages the Vergosity Framework and a light-weight enterprise architecture. I felt that once I had the architecture in place and realized the patterns were repeatable, I was ready for code generation. It has been a few years since I have worked with a code generator. The first consideration of code generation is to define the source that will be used to generate code. The second consideration are the templates. Then you bind the source with the template to create the output. Sounds pretty straight forward. You have some binding options when you are using .NET (i.e., T4Templates, Razor Engine). I chose the Razor Engine because this allowed me to use Razor syntax in Visual Studio to create my templates (.cshtml) files – this turned out to be the easiest part of creating the code generator. I’ve worked with T4Templates in the past. I do not have anything against them, just wanted to try something different.
Try it out for yourself…You can get the installer on GitHub (https://github.com/buildmotion/CodeBuilder-Install) or you can download the actual source code and see how it works.
Download the BuildMotion.CodeBuilder
You will need to get the source code which is contained in (2) projects on GitHub. The CodeBuilder requires a reference to Vergosity.Services and to the latest version of the Vergosity Framework (available on NuGet).
- CodeBuilder: https://github.com/buildmotion/CodeBuilder
- Vergosity.Services. https://github.com/buildmotion/Vergosity.Services
- Vergosity Framework at NuGet: Install-Package Vergosity.Framework or https://www.nuget.org/packages/Vergosity.Framework
Here is a screen shot of the main window for the Code Builder. It was built using WPF on top of the Vergosity Framework for handling all of the business actions. The application doesn’t use a database, although I could see some future feature that saves the configuration.
Recipe for the Application
I would recommend starting out with a new or existing C# .NET Class Library project. You will want to make some NuGet package references.
1. Reference the Vergosity Framework & Vergosity.Services
You can reference the Vergosity framework with one of (2) ways using NuGet.
Package Manager Console: Install-Package Vergosity.Framework
The Vergosity.Services source code and project is located on GitHub. You will need to reference this project for both the BuildMotion.CodeBuilder and your target application.
2. Create or Modify Existing Entity
You will want to create or modify your entity classes. Make sure your entity classes inherit from Vergosity.Entity.IEntity or some other distinct interface. You might need to create one for you entity classes. This will be used by the code generator to target all classes in your project that implement or inherit from the specified type.
Ex: public class Customer : Vergosity.Entity.IEntity
3. Add Identifier Properties to your Entities
Make sure your entity have identifier properties. If you are using the Vergosity.Entity.IEntity interface – you will need to implement the Id property as a System.Guid.
4. Add reference to the DataAnnotations namespace
We are going to use some annotations to provide information to Entity Framework Migrations when we generate the database from the code. Pretty cool, right. Now, Entity Framework will know which property is our identity column.
5. Compile the Target Assembly
You will now want to compile the target assembly project before you generate your code. You will select the actual compiled assembly when you use the BuildMotion.CodeBuilder tool.
6. Open/Run the BuildMotion.CodeBuilder application.
- Select the target assembly. It should be compiled with your Entity definitions.
- Enter the default or core namespace for the application. (i.e., BuildMotion.Reference)
- Enter the Application Name (i.e., ReferenceApp).
7. Build Some Code
This will provide the CodeBuilder enough information to create the Service and Entity Framework code. It will also create a few other necessary files for the application. For example, I use Autofac as the dependency injection container, and there is a bootstrap class to do the initial wire up.
- Click on the Build Service Code button.
- Click on the Build Entity Framework Code button.
8. Create Entity Code: Service, Business, Rules, Validation, and Data Access Repositories.
- Click the Retrieve Entity Items button.
- Select one ore more entity items to build your code.
- Click the Build Code button to finish generating all of the code.
If you are all good, you will the following.
9. Include the code into your Target Project.
You will now want to include all of the generated code into your project and compile when you are ready.