Skip to content

Serenity Code Generator

Serenity has code generator what generates row, form, grid, page, services, client typings and server typings. It's already installed in Serene and StartSharp templates.

Sergen on Nuget.org

Installation:


$ dotnet tool install sergen

Usage


Ensure your console working directory is in your web project.

$ cd /projects/Serene/Serene.Core/
$ dotnet sergen
Serenity Code Generator 5.0.7.0

Usage: sergen [command]

Commands:
    g[enerate]        Launches table code generator
    r[estore]         Restores content, e.g. scripts, fonts and css from .nupkg

    c[lienttypes]     Imports editor, formatter types from TypeScript to CS
    m[vc]             Generates intellisense helpers for view locations
    s[ervertypings]   Imports row, form, service types from CS to TypeScript
    t[ransform]       Runs clienttypes, mvc and servertypings commands at once

Restoring contents for serenity


Serenity distribute contents from nuget packages, but it's needs restore operation for effect your project. For example, lets say Serenity.Scripts has new content and bug fixes in typescript code, when you update Serenity.Scripts nuget package on your project, you still seeing the previous contents. After sergen restore operation your contents are updated and you can access latest content.

$ dotnet sergen restore
  Determining projects to restore...
  Restored /projects/Serene/Serene.Core/Serene.Core.csproj (in 39.92 sec).
Processing: Mapster
Processing: StackExchange.Exceptional.AspNetCore
Processing: Serenity.FluentMigrator.Runner
Processing: Serenity.Scripts
Processing: Serenity.Web
Processing: Serenity.Web.Assets
Restoring: wwwroot/fonts/glyphicons-halflings-regular.svg
Processing: StackExchange.Exceptional.Shared
Processing: Serenity.Core
Processing: Serenity.Data
Processing: Serenity.Data.Entity
Processing: Serenity.Services
Processing: jQuery
Processing: jQuery.UI.Combined
Processing: jQuery.Validation
Processing: bootstrap
Restoring: wwwroot/fonts/glyphicons-halflings-regular.svg
Processing: toastr
Processing: Select2.js

Generating ClientTypes


Client Types are attributes what used in server side as definition, like editors, formatters etc. For example, when you create a custom editor, it's must registered as editor with @Serenity.Decorators.registerEditor() . Then sergen is collects that "editors" from your .ts files. It's creates attribute .cs files. This attributes are used on properties in SampleForm.cs or SampleRow.cs files mostly.

$ dotnet sergen clienttypes
Transforming ClientTypes at: /projects/Serene/Serene.Core/Imports/ClientTypes

Generating ServerTypings


Server Types are codes what generated on client side. It's will include grid columns, forms, rows, services, enums, permissions, request objects and response objects etc. For example, when you create a enum at c# and then add ScriptInclude attribute on it, sergen generates this enum for typescript.

$ dotnet sergen servertypings
Transforming ServerTypings at: /projects/Serene/Serene.Core/Imports/ServerTypings

Generates intellisense helpers for view locations


Sergen can create helpers for "view file" paths. Serenity uses *.cshtml files under module directory. So when you want to use it from controller, you need to write full path instead of mvc convension. This helpers are for make easy to access your cshtml path with intellisense.

$ dotnet sergen mvc
Transforming MVC at: /projects/Serene/Serene.Core/Imports/MVC

Transform


Transform is shortcut for run all typings commands at once.

$ dotnet sergen transform
Transforming MVC at: /projects/Serene/Serene.Core/Imports/MVC
Transforming ClientTypes at: /projects/Serene/Serene.Core/Imports/ClientTypes
Transforming ServerTypings at: /projects/Serene/Serene.Core/Imports/ServerTypings

Generate


This command is generate code from selected database table. Follow requests.

$ dotnet sergen generate
=== Table Code Generation ===

Available Connections:
Default
Northwind

Enter a Connection: ('!' to abort)
Northwind

Available Tables:
...
dbo.Categories
dbo.Customers
dbo.Orders
dbo.Products
dbo.Region
dbo.Shippers
dbo.Suppliers
dbo.Territories
dbo.VersionInfo

Enter a Table: ('!' to abort)
dbo.Region

Enter a Module name for table: ('!' to abort)
Northwind

Enter a class Identifier for table: ('!' to abort)
RegionSample

Enter a Permission Key for table: ('!' to abort)
Northwind:RegionSampleRole

Choose What to Generate (R:Row, S:Repo+Svc, U=UI, C=Custom)
RSUC

Module and identifier names are validated. Module names must be valid namespace names (letters, digits, _ and dots), and dots are treated as namespace separators: they are converted to / for URL and file path generation (e.g. a module named Northwind.Samples generates files under Modules/Northwind/Samples with the route Northwind/Samples).

Generated files are listed below:

/projects/Serene/Serene.Core/Modules/Northwind/NorthwindNavigation.cs
/projects/Serene/Serene.Core/Modules/Northwind/RegionSample/RegionSampleColumns.cs
/projects/Serene/Serene.Core/Modules/Northwind/RegionSample/RegionSampleDialog.ts
/projects/Serene/Serene.Core/Modules/Northwind/RegionSample/RegionSampleEndpoint.cs
/projects/Serene/Serene.Core/Modules/Northwind/RegionSample/RegionSampleForm.cs
/projects/Serene/Serene.Core/Modules/Northwind/RegionSample/RegionSampleGrid.ts
/projects/Serene/Serene.Core/Modules/Northwind/RegionSample/RegionSampleIndex.cshtml
/projects/Serene/Serene.Core/Modules/Northwind/RegionSample/RegionSamplePage.cs
/projects/Serene/Serene.Core/Modules/Northwind/RegionSample/RegionSampleRepository.cs
/projects/Serene/Serene.Core/Modules/Northwind/RegionSample/RegionSampleRow.cs

Non-Interactive Generate (CI)


sergen generate can also run without any prompts, so it can be used in CI or in scripts. Add --no-interactive (aliases --ni, --noninteractive):

  • Values with a default fall back to it.
  • Values without one become required: --table, and --connection-key when the project defines several connections. A missing switch fails with a message naming it, instead of waiting for input.
$ dotnet sergen generate --no-interactive --table dbo.Region --module Northwind --identifier RegionSample --what RSUC

Existing files whose content would change are handled by the --overwrite[:mode] switch:

Mode Behavior
yes (default for a bare switch) Replace the file, keeping a *.bak copy
no Fail with exit code 13 (OverwriteRefused)
skip Leave the file as it is and continue
force Replace the file, without a backup

Aliases: y / yes / true / backup for yes, n / no / false for no, s / skip for skip, f / force / overwrite for force.

Without the switch sergen stays interactive. In non-interactive mode it defaults to failing rather than silently discarding hand edits:

$ dotnet sergen generate --ni --table dbo.Region --overwrite:skip

A refused overwrite returns exit code 13, so a script can tell it apart from other failures. --help can be combined with a command, and dotnet sergen generate --help documents all the switches for the generator.