A guide to a 10-minute video: from an empty project and Excel workbooks to four front ends
- Full version (about 10 minutes)
- Short version (about 3 minutes)
The video has English narration, with English and Traditional Chinese subtitles on YouTube.
This year I wrote a thirty-post series for the iThome Ironman contest, ERP Architect's Notes: Designing a Definition-Driven Framework. It covers the mechanisms and trade-offs of definition-driven design, and the framework at the time was Bee.NET. After the series ended, Polhem took over from Bee.NET, and the packages moved from Bee.* to Polhem.*. The definition-driven mechanisms carried over, the framework itself went through a major refactoring, and it gained a project template and Excel import. Bee.NET is no longer updated.
Polhem is an open-source .NET enterprise application framework under the MIT license. The source is on GitHub and the packages are on NuGet. The front end uses Avalonia, so the same UI code runs on the desktop, in the browser, and on iOS and Android.
Note: the video was recorded with Polhem 1.9.0. Polhem has since added Blazor front ends (MudBlazor and Fluent UI), so the same definitions can also run as a Blazor web app. The video shows the Avalonia front end.
The thirty posts are almost all text. This time I recorded a video that walks through the process: using Northwind as the example, it starts from an empty project, imports a few Excel workbooks, and the system runs. When the requirements change, you change the definitions, and the system follows. No CRUD code and no SQL are written along the way. The sections below follow the order of the video and explain the design behind each part.
1️⃣ Opening: why is one field written so many times?
In a business system, a field is only a few facts: its name, its type, whether it is required. Yet those facts are restated in the entity, the migration, the DTOs, the validation, the API, the form, the list and the translations. Add a field, and every one of those places has to change, and has to change consistently.
Better tools make each place quicker to write, but they do not reduce the number of places. So the question I care about is where the source lives: in the code, or in the definitions?
Polhem chooses the definitions. Each form is described once, as data, and the tables, the CRUD SQL, the validation rules and the screens are derived from that description. The same description runs on SQL Server, PostgreSQL, MySQL, Oracle and SQLite, and switching databases is a matter of configuration. The front end reads the definitions at run time, so changing a definition needs no recompiling and no redeploying.
The description does not have to be written as code either. The video uses six Excel workbooks prepared in advance: categories, suppliers, customers, shippers, products and orders. Each row is one field.
2️⃣ Creating the project and preparing the database
In VS Code, create a new .NET project and pick "Polhem App (Avalonia)" from the template list, with the default SQL Server database. The generated project already has a server, a desktop front end and a Define folder for the definition files, and the Polhem packages and the SQL Server driver are already referenced.
Next, run the VS Code task "Polhem: Initialize database". It creates three databases, common, company and log, along with the tables the framework needs and an administrator account. The video looks at SQL Server before and after, so the difference is easy to see.
The three databases keep the framework's own data (sign-in state, cache notifications) apart from each company's business data, with the audit log stored separately again. When one application serves several companies, each company's business data stays isolated while the framework data is shared.
3️⃣ Importing Excel: the workbook is the specification
This is the longest part of the video. Before importing, it opens the orders workbook to show what it describes:
- The field name and data type; the type decides the column type in the database
- The maximum length of text fields
- The input control, for example a drop-down list for the status
- The related form: the customer field points to the Customer form, so after import the customer code and name are filled in automatically and the customer can be picked on screen
- The amount formula: quantity times unit price, less the discount
- A separate sheet that lists the rules checked before saving
Then DefineEditor (Polhem's definition file editor) imports the six workbooks in a batch, upgrading the database after each one. When it finishes, each form has its own set of definition files, and the business tables have been created.
Excel is only the input. The definition files produced by the import are no different from ones written by hand. Here is what each of them does:
| Definition file | What it describes | Where it is used |
|---|---|---|
| FormSchema | The form's fields, relationships, computed fields and rules | The other definitions are derived from it, and the CRUD SQL is built from it |
| TableSchema | The structure of the physical tables | Derived from FormSchema, used to create and upgrade the tables |
| FormLayout | How the fields are arranged | Derived from FormSchema, each front end draws its screens from it |
| Language files | Field captions in each language | The text shown on screen |
FormSchema describes a form, not a table. An order has a master and details, and the customer field relates to another form. All of that is written at the form level, and the framework generates the SQL from it.
4️⃣ Running: a system with no form code
Press F5, and the server and the desktop front end start together. Sign in with the administrator created earlier, and the menu already has two groups of forms, master data and transactions, arranged exactly like the program tree in DefineEditor.
The video enters a few master records, then opens an order: pick a product on a detail line, enter a quantity and a unit price, and the amount is calculated. Saving without a customer is blocked, and the rule that blocks it is the one from the workbook's rules sheet. Pick a customer, the code and name come along, and the second save succeeds.
The calculation and the save check both work, yet no code was written for this form. Data moves between the layers as a DataSet, so no form needs its own type or DTO. The computed fields and rules are declared in the definitions, and the framework executes them.
5️⃣ Iterating: when requirements change, change the definitions
This is the part I most want people to see. Once a system is live, requirements change: sales wants to grade customers, and orders need a quantity limit.
This time the change is made directly in DefineEditor. The customer form gets a "grade" field: text, ten characters long, B as the default, with choices A, B and C in a drop-down list. After saving, syncing the form adds the field to the TableSchema, the layout and the list. Upgrading only this form's tables gives the customer table a grade column. The order form gets one more rule: a detail line cannot exceed one hundred units.
Back in the front end, sign out and sign back in, with no restart needed. The customer list has a grade column, and existing customers have the default value B. The edit screen has the drop-down list. An order with a quantity of two hundred is blocked by the new rule when saved.
Two mechanisms make this work. The database upgrade compares the definitions with the existing tables and adds only what is missing, leaving existing data alone. The definitions are cached, and a change invalidates the cache, so the front end reads the new definitions the next time.
6️⃣ Visual fine-tuning and multiple front ends
The video ends with two short parts.
The first adjusts details in DefineEditor: the contact name becomes required, the layout changes from two columns to three, and "grade" gets a Chinese caption, with the preview updating immediately. After signing in again, the three-column layout and the required check both take effect, and users with the Chinese interface see the new caption.
The second goes back to the folders created with the project. Besides the desktop, the same project has web, iOS and Android front ends, and one task starts the server and all four front ends together. All four show the same menu and the same orders, because they read the same definitions and talk to the same server.
FormLayout only describes which fields there are and how they are arranged, without tying itself to a UI technology, and each front end draws its screens from it. Front-end technology turns over every few years, and the definitions can stay in use.
7️⃣ What the video does not show
The demo picks a flow that the definitions can express completely. So that nobody concludes every form can be finished this way, a few notes:
- Logic the definitions cannot express still needs code. Excel does not describe code. In the framework's complete Northwind sample, the order form is bound to a custom business object that handles the order total and the detail checks; the video shows the definitions-only version. The definitions handle what is common, and special business logic overrides the business object's extension points.
- Reports and batch jobs do not go through the definitions. CRUD is driven by the definitions, while reports and batch jobs are implemented by the business objects themselves.
- The framework has permissions, auditing and transport encryption, but the video does not demonstrate them.
✅ Conclusion
The flow in the video comes down to one sentence: describe the form, and leave the rest to the framework. It works because the definitions are the single source of truth. The database, the SQL, the screens and the rules are all derived from them, so when a definition changes, every layer follows.
This approach fits systems with many forms of a regular shape that need long-term maintenance and customization, such as ERP. If a system has few forms and every screen is heavily customized, writing the code directly may be simpler. You do not need Polhem either: you can take the idea of making the definitions the source and build it with the tools you already know.
Try it without installing anything: Blazor Server or Avalonia WebAssembly. Sign in with demo / demo, or demo-ro / demo for the read-only account. It runs on a free host, so the first visit after a quiet spell can take about a minute, and the data resets when the service restarts.
To try it yourself, install the template with dotnet new install Polhem.Templates. The documentation and samples are on GitHub.
📘 Original article on HackMD, in Traditional Chinese:
👉 https://hackmd.io/@jeff377/polhem-video-guide
📢 You are welcome to republish this, please credit the source.
📬 Follow for more engineering notes:
Facebook | HackMD | GitHub | NuGet