Code Generation Logic & Concept
This shows the concept and logic behind the way CodeStencil uses string replacement to generate your code. The numbered elements on the images below have been explained below with the same numbers.
(1)PROJECT_NAME: In the Expanders panel, you can see that the expansion string is "RazorApp". In the CodeTree panel, the nodes that have that expander label will generate the elements (in this case, folder, file) with the appropriate expansion string.
For example, the first node (identified with the dotted red arrow) will create a folder called RazorApp
The other node ( also identified with the dotted red arrow) will create a Visual Studio project file called RazorApp.csproj
(2)CS_TABLE_LIST: The CS_TABLE_LIST
code nanite will enumerate the list of tables listed in the Database Schema. Right now, the tables listed are: Album, Artist, Customer, Employee, Genre, Invoice, MediaType, Track. Now, let us analyze what happens within the Code Tree and the Code Editor.
Code Tree
The first node - [%CS_TABLE_LIST%]EntityConfiguration.cs
is a file node. Therefore CodeStencil will generate files with names based on a combination of enumerated tables plus the string - "EntityConfiguration". So, the files that will be created will be:
- AlbumEntityConfiguration.cs ArtistEntityConfiguration.cs
- ArtistEntityConfiguration.cs
- CustomerEntityConfiguration.cs
- EmployeeEntityConfiguration.cs
- GenreEntityConfiguration.cs
- InvoiceEntityConfiguration.cs
- MediaTypeEntityConfiguration.cs
- TrackEntityConfiguration.cs
The second node - [%CS_TABLE_LIST%].cs
is also a file node. CodeStencil will generate files with names based on the tables. So, the files that will be created will be:
- Album.cs
- Artist.cs
- Customer.cs
- Employee.cs
- Genre.cs
- Invoice.cs
- MediaType.cs
- Track.cs
Code Editor
The line :
public virtual DbSet<[%CS_TABLE_LIST%]> [%CS_TABLE_LIST%] { get; set; }
will generate:
public virtual DbSet<Album> Album { get; set; }
public virtual DbSet<Artist> Artist { get; set; }
public virtual DbSet<Customer> Customer { get; set; }
public virtual DbSet<Employee> Employee { get; set; }
public virtual DbSet<Genre> Genre { get; set; }
public virtual DbSet<Invoice> Invoice { get; set; }
public virtual DbSet<InvoiceLine> InvoiceLine { get; set; }
public virtual DbSet<MediaType> MediaType { get; set; }
public virtual DbSet<Playlist> Playlist { get; set; }
public virtual DbSet<PlaylistTrack> PlaylistTrack { get; set; }
public virtual DbSet<Track> Track { get; set; }
The line :
new [%CS_TABLE_LIST%]Configuration(modelBuilder.Entity<[%CS_TABLE_LIST%]>());
will generate:
new AlbumConfiguration(modelBuilder.Entity<Album>());
new ArtistConfiguration(modelBuilder.Entity<Artist>());
new CustomerConfiguration(modelBuilder.Entity<Customer>());
new EmployeeConfiguration(modelBuilder.Entity<Employee>());
new GenreConfiguration(modelBuilder.Entity<Genre>());
new InvoiceConfiguration(modelBuilder.Entity<Invoice>());
new InvoiceLineConfiguration(modelBuilder.Entity<InvoiceLine>());
new MediaTypeConfiguration(modelBuilder.Entity<MediaType>());
new PlaylistConfiguration(modelBuilder.Entity<Playlist>());
new PlaylistTrackConfiguration(modelBuilder.Entity<PlaylistTrack>());
new TrackConfiguration(modelBuilder.Entity<Track>());
(3)DB_CONTEXT: The expansion label - [%DB_CONTEXT%]
expands to "ApplicationDbContext" which is the expansion string defined in the Expander panel.
So, the first DB_CONTEXT
expander;
public class [%DB_CONTEXT%] : IdentityDbContext
Expands to:
public class ApplicationDbContext : IdentityDbContext
The other DB_CONTEXT
expanders;
public [%DB_CONTEXT%](DbContextOptions<[%DB_CONTEXT%]> options) : base(options)
Expands to:
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
(4)NAMESPACE: The NAMESPACE expansion label is a combination of 2 other expanders - [%ORGANIZATION_LABEL%]
and [%PROJECT_NAME%]
[%ORGANIZATION_LABEL%]
expands to "DolaSoft"
[%PROJECT_NAME%]
expands to "RazorApp"
Hence, [%NAMESPACE%]
expands to "DolaSoft.RazorApp"
This is the final code generated by CodeStencil:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace DolaSoft.RazorApp.Models
{
public class ApplicationDbContext : IdentityDbContext
{
public virtual DbSet<Album> Album { get; set; }
public virtual DbSet<Artist> Artist { get; set; }
public virtual DbSet<Customer> Customer { get; set; }
public virtual DbSet<Employee> Employee { get; set; }
public virtual DbSet<Genre> Genre { get; set; }
public virtual DbSet<Invoice> Invoice { get; set; }
public virtual DbSet<InvoiceLine> InvoiceLine { get; set; }
public virtual DbSet<MediaType> MediaType { get; set; }
public virtual DbSet<Playlist> Playlist { get; set; }
public virtual DbSet<PlaylistTrack> PlaylistTrack { get; set; }
public virtual DbSet<Track> Track { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
new AlbumConfiguration(modelBuilder.Entity<Album>());
new ArtistConfiguration(modelBuilder.Entity<Artist>());
new CustomerConfiguration(modelBuilder.Entity<Customer>());
new EmployeeConfiguration(modelBuilder.Entity<Employee>());
new GenreConfiguration(modelBuilder.Entity<Genre>());
new InvoiceConfiguration(modelBuilder.Entity<Invoice>());
new InvoiceLineConfiguration(modelBuilder.Entity<InvoiceLine>());
new MediaTypeConfiguration(modelBuilder.Entity<MediaType>());
new PlaylistConfiguration(modelBuilder.Entity<Playlist>());
new PlaylistTrackConfiguration(modelBuilder.Entity<PlaylistTrack>());
new TrackConfiguration(modelBuilder.Entity<Track>());
}
}
}