DEV Community

Cover image for The Same Definitions on Blazor Server: What Has to Change?
Jeff Tsai
Jeff Tsai

Posted on Originally published at hackmd.io

The Same Definitions on Blazor Server: What Has to Change?

A guide to a 7-minute video: one project, layout changes without redeploying, from wide screens to phones

The video has English narration, with English and Traditional Chinese subtitles on YouTube. It was recorded with Polhem 1.11.0.

Note: Polhem now has five project templates: one for Avalonia, plus Blazor Server and Blazor WebAssembly templates, each in a MudBlazor and a Fluent UI version. The template in the video, polhem-blazor-server, is now the Blazor Server template with MudBlazor.

The previous post, Can a Form Be Described Only Once?, was a guide to the Avalonia version: one server plus four front ends for the desktop, the web, iOS and Android. This time it is Blazor Server, with the same Northwind sample and the same six Excel workbooks, and the screens are now rendered by the website on the server.

The Excel import, the division of labor among the definition files and the three databases are the same in both videos. The previous post covers them, so they only get a brief mention here. The rest of this post is about what is different once you switch to Blazor Server.


1️⃣ One project: the website is the server

In VS Code, create a new .NET project. Polhem has two templates, and this time the choice is "Polhem App (Blazor Server)". The generated solution has a single project, the Blazor Server website, next to a Define folder for the definition files and an Excel folder for the workbooks.

The Avalonia template generates a server plus desktop, web, iOS and Android front ends, and the front ends call the server over JSON-RPC. The Blazor Server template has only one project because the back end runs inside the website. Program.cs has three calls: one hosts the back end in the website, one registers the Blazor form screens, and one sets up the shell (the sign-in screen and the menu). When a page calls the back end, the call stays in the same process and never crosses the network.

Calls within the same process are treated as trusted local calls. Forms still check the signed-in user's permissions, but some system operations open only to local callers (reading any definition, for example) are reachable from pages. The template's Program.cs has a comment about this: in pages you add yourself, do not pass values sent by the browser straight into these calls.

If you later want Avalonia front ends or JavaScript clients to connect as well, add a JSON-RPC endpoint to the website. Calls from those clients are checked under the rules for remote clients.


2️⃣ Database and import: same as the previous post

The next two steps are the same as in the Avalonia version, and the video spends only a little over a minute on them:

  • Run the VS Code task "Polhem: Initialize database" to create the three databases, common, company and log, along with the framework tables and an administrator account.
  • In DefineEditor (Polhem's definition file editor), import the six workbooks in a batch. The database is upgraded after each one, and the business tables are created along the way.

Why there are three databases, what the workbooks describe and how the definition files divide the work after import are explained in sections 2 and 3 of the previous post.


3️⃣ Running: every screen is derived from the definitions

Press F5, and the website starts and opens a browser. Sign in with the administrator created earlier, and the menu has two groups of forms, master data and transactions.

The video enters categories, suppliers, customers, shippers and products in turn, 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 by a rule, and after picking a customer the save succeeds. The flow is the same as in the Avalonia version. The difference is that every screen this time is drawn by Blazor components from the definitions. The project has no Razor page for any of its forms.

The sign-in state belongs to the browser tab: reloading or opening a new tab goes back to the sign-in screen. The interface language is chosen on the sign-in screen and applied after signing in.


4️⃣ Changing the layout: no sign-out, no restart

This is the main point of the video. After the system has been in use for a while, users report that the order's notes field is a single line and too short.

In DefineEditor, open the order's layout (FormLayout), select the notes field and click the globe icon on the toolbar, and a preview opens in the browser. The preview uses the same Blazor screens as the website, so what the preview shows is what users will see. The video puts DefineEditor and the preview side by side and makes three changes:

  • The control changes from a single-line text box to a multi-line memo box
  • The field spans two columns and is three rows tall
  • The notes field is dragged to the end of the form

The preview follows each change immediately, before anything is saved. Once it looks right, press ⌘S to save.

Back on the website, in the same tab, there is no sign-out and no restart. Open the customer form first, then go back to orders and click New: the notes field is now the last row, a multi-line input across the full width.

This works because the definitions are cached, and saving invalidates the matching cache entry. The website reads the layout when a form is opened, so it gets the new one. The word to watch is "opened": an order form that is already open does not refresh itself, which is why the video switches to another form and back. In the earlier Avalonia video, changing the definitions meant signing out and back in. This time even that step is gone.


5️⃣ One layout, from wide screens to phones

The last part is RWD (responsive web design, where the layout adapts to the window width). The layout definition only says how the fields are arranged and how many columns each one spans. There is no separate version for phones.

The video narrows the browser step by step:

  • It starts with two columns, and the fields shrink as the window gets narrower
  • At phone width, the form becomes a single column and the left-hand menu collapses into a button
  • Details are no longer edited directly in a grid. A button opens a form that edits one line at a time
  • The menu slides in from the side and closes automatically once a form is chosen

At the end the browser is at phone width, and the same website still works. On narrow screens the list grid runs past the right edge and scrolls horizontally.


6️⃣ When to choose Blazor Server

Both templates read the same definitions, so the choice comes down to deployment and how the system is used.

Blazor Server means one project and one website to deploy. Users only need a browser, and layout changes need no redeploying. The cost is that every user keeps a connection to the server while working and the screen state lives on the server, so an unstable network hurts the experience, and there is no offline use.

If you need native mobile apps or desktop programs, or want the front end deployed separately from the server, the Avalonia template fits better. The two can also coexist: add a JSON-RPC endpoint to the Blazor Server website, and Avalonia front ends can connect to the same back end.

What the video does not show is the same as last time. Logic the definitions cannot express still goes into business objects, reports and batch jobs do not go through the definitions, and the framework has permissions, auditing and transport encryption that the video does not demonstrate.


✅ Wrapping up

This time only the front-end technology changed, and not a single line of the definitions did: the same workbooks, once imported, run on Blazor Server. The definitions only describe which fields a form has and how they are arranged, without tying themselves to how the screens are drawn. Whatever technology the front end uses, it draws its screens from the definitions.

The Blazor Server version adds one convenience: after a layout change, users see it the next time they open the form, with no redeploying and no signing in again. If your system is used mainly in the browser and has many forms that change often, this combination is worth a try. If you need offline use or native apps, the Avalonia version fits better.

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 templates with dotnet new install Polhem.Templates, then create a project with dotnet new polhem-blazor-server. The documentation and samples are on GitHub.


📘 Original article on HackMD, in Traditional Chinese:
👉 https://hackmd.io/@jeff377/polhem-blazor-server

📢 You are welcome to republish this, please credit the source.
📬 Follow for more engineering notes:
Facebook | HackMD | GitHub | NuGet

Top comments (0)