Generating Code For Movie Table
Serenity Code Generator
After making sure that our table exists in the database, we will use Serenity Code Generator (sergen.exe) to generate initial editing interface.
As ASP.NET Core has cross-platform support, .NET Core version of Sergen also needs to run in OSX / Linux / Windows. Thus, its UI is currently console based.
We first need to open a command prompt at project folder. Right click MovieTutorial.Web project and click Open Folder in File Explorer.
Click File menu in file explorer, and click Open Windows Powershell or Open Command Prompt.
If you are using Visual Studio 2022, right click on project and select "Open in Terminal".
Make sure you are at
dotnet sergen g to open Sergen code generation UI (console).
If you receive an error, type
dotnet tool restorebefore running sergen.
Sergen will list connections in appsettings.json file.
You can use up-down arrow keys to navigate. Select Default connection and press Enter.
After pressing Enter you'll get a list of tables in that database:
Navigate using arrow keys and select mov.Movie table by pressing Space then press Enter to accept.
Next, Sergen will ask for a module name, enter MovieDB.
In Serenity terms, a module is a logical group of pages, sharing a common purpose.
For example, in Serene template, all pages related to Northwind sample belongs to Northwind module.
Pages that are related to general management of site, like users, roles etc. belongs to Administration module.
A module usually corresponds to a database schema, or a single database but there is nothing that prevents you from using multiple modules in a single database / schema, or the opposite, multiple databases in one module.
For this tutorial, we will use MovieDB (analogous to IMDB) for all pages.
Module name is used in determining namespace and url of generated pages.
For example, our new page will be under MovieTutorial.MovieDB namespace and will use /MovieDB relative url.
Module names must be in Pascal case, e.g. something that starts with a CAPITAL letter.
aModuleare invalid module names.
When prompted, enter Movie as class identifier.
This usually corresponds to the table name but sometimes table names might have underscores or other invalid characters, so you decide what to name your entity in generated code (a valid identifier name).
Our table name is Movie so it is also a valid and fine C# identifier, so let's leave Movie as the entity identifier. Our entity class will be named MovieRow.
This name is also used in other class names. For example our page controller will be named MoviePage. It also determines the page url, in this sample our editing page will be at URL /MovieDB/Movie.
Identifier must always be in Pascal case, e.g. something that starts with a CAPITAL letter.
aTableare invalid identifier names.
Leave permission as Administration:General and press enter again.
In Serenity, access control to resources (pages, services etc.) are controlled by permission keys which are simple strings. Users or roles are granted these permissions.
Our Movie page will be only used by administrative users (or maybe later content moderators) so let's leave it as Administration:General for now. By default, in Serene template, only the admin user has this permission
Next, Sergen will ask you which files to generate, select All option (e.g. Row, Repository & Service, User Interface and Custom) and press ENTER last time.
If you don't need UI for some entity, just select Row and Repository & Service.
If you just want an entity class, and not request handlers / service access, select only Row.
Now you can quit command prompt, and return back to Visual Studio (or Notepad :)
After Generating Code
If Visual Studio asks if you want to reload changes, click Reload All.
REBUILD the Solution and then press F5 to launch application.
Use admin as username, and serenity as password to login.
When you are greeted with Dashboard page, you will notice that there is a new section, MovieDB on the bottom of left navigation.
Click to expand it and click Movie to open our first generated page.
Now try adding a new movie, than try updating and deleting it.
Sergen generated code for our table, and it just works without writing a single line of code.
This doesn't mean i don't like writing code. In contrast, i love it. Actually i'm not a fan of most designers and code generators. The code they produce is usually unmanagable mess.
Sergen just helped us here for initial setup which is required for layered architecture and platform standards. We would have to create about 13 files for entity, request handlers, page, endpoint, grid, form etc. Also we needed to do some setup in a few other places.
Even if we did copy paste and replace code from some other page, it would be error prone and take about 5-10 mins.
The code files Sergen generates has minimum code with the absolute basics. This is thanks to the base classes in Serenity that handles the most logic. Once we generate code for some table, we'll probably never use Sergen again (for this table), and modify this generated code to our needs. We'll see how.