<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jeff Tsai</title>
    <description>The latest articles on DEV Community by Jeff Tsai (@jeff377).</description>
    <link>https://dev.to/jeff377</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4181868%2F2f539f0e-aeec-4ef8-ae80-a4110d0cec24.jpg</url>
      <title>DEV Community: Jeff Tsai</title>
      <link>https://dev.to/jeff377</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jeff377"/>
    <language>en</language>
    <item>
      <title>The Same Definitions on Blazor Server: What Has to Change?</title>
      <dc:creator>Jeff Tsai</dc:creator>
      <pubDate>Thu, 26 Nov 2026 03:53:38 +0000</pubDate>
      <link>https://dev.to/jeff377/the-same-definitions-on-blazor-server-what-has-to-change-2no</link>
      <guid>https://dev.to/jeff377/the-same-definitions-on-blazor-server-what-has-to-change-2no</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;A guide to a 7-minute video: one project, layout changes without redeploying, from wide screens to phones&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;  &lt;iframe src="https://www.youtube.com/embed/HwQXMhrWtKM" width="710" height="399"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://youtu.be/HwQXMhrWtKM" rel="noopener noreferrer"&gt;Full version&lt;/a&gt; (about 7 minutes)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://youtu.be/ek5rZvSzMfI" rel="noopener noreferrer"&gt;Short version&lt;/a&gt; (about 2.5 minutes)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The video has English narration, with English and Traditional Chinese subtitles on YouTube. It was recorded with Polhem 1.11.0.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;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, &lt;code&gt;polhem-blazor-server&lt;/code&gt;, is now the Blazor Server template with MudBlazor.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The previous post, &lt;a href="https://dev.to/jeff377/can-a-form-be-described-only-once-27ha"&gt;Can a Form Be Described Only Once?&lt;/a&gt;, 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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;




&lt;h2&gt;
  
  
  1️⃣ One project: the website is the server
&lt;/h2&gt;

&lt;p&gt;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 &lt;code&gt;Define&lt;/code&gt; folder for the definition files and an &lt;code&gt;Excel&lt;/code&gt; folder for the workbooks.&lt;/p&gt;

&lt;p&gt;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. &lt;code&gt;Program.cs&lt;/code&gt; 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.&lt;/p&gt;

&lt;p&gt;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 &lt;code&gt;Program.cs&lt;/code&gt; has a comment about this: in pages you add yourself, do not pass values sent by the browser straight into these calls.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;




&lt;h2&gt;
  
  
  2️⃣ Database and import: same as the previous post
&lt;/h2&gt;

&lt;p&gt;The next two steps are the same as in the Avalonia version, and the video spends only a little over a minute on them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;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.&lt;/p&gt;




&lt;h2&gt;
  
  
  3️⃣ Running: every screen is derived from the definitions
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;




&lt;h2&gt;
  
  
  4️⃣ Changing the layout: no sign-out, no restart
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The control changes from a single-line text box to a multi-line memo box&lt;/li&gt;
&lt;li&gt;The field spans two columns and is three rows tall&lt;/li&gt;
&lt;li&gt;The notes field is dragged to the end of the form&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The preview follows each change immediately, before anything is saved. Once it looks right, press ⌘S to save.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;




&lt;h2&gt;
  
  
  5️⃣ One layout, from wide screens to phones
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;The video narrows the browser step by step:&lt;/p&gt;

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

&lt;p&gt;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.&lt;/p&gt;




&lt;h2&gt;
  
  
  6️⃣ When to choose Blazor Server
&lt;/h2&gt;

&lt;p&gt;Both templates read the same definitions, so the choice comes down to deployment and how the system is used.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;




&lt;h2&gt;
  
  
  ✅ Wrapping up
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Try it without installing anything: &lt;a href="https://polhem-northwind.onrender.com/" rel="noopener noreferrer"&gt;Blazor Server&lt;/a&gt; or &lt;a href="https://polhem-northwind.onrender.com/web/" rel="noopener noreferrer"&gt;Avalonia WebAssembly&lt;/a&gt;. Sign in with &lt;code&gt;demo&lt;/code&gt; / &lt;code&gt;demo&lt;/code&gt;, or &lt;code&gt;demo-ro&lt;/code&gt; / &lt;code&gt;demo&lt;/code&gt; 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.&lt;/p&gt;

&lt;p&gt;To try it yourself, install the templates with &lt;code&gt;dotnet new install Polhem.Templates&lt;/code&gt;, then create a project with &lt;code&gt;dotnet new polhem-blazor-server&lt;/code&gt;. The documentation and samples are on &lt;a href="https://github.com/polhem-dev/polhem" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;📘 Original article on HackMD, in Traditional Chinese:&lt;br&gt;
👉 &lt;a href="https://hackmd.io/@jeff377/polhem-blazor-server" rel="noopener noreferrer"&gt;https://hackmd.io/@jeff377/polhem-blazor-server&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📢 You are welcome to republish this, please credit the source.&lt;br&gt;
📬 Follow for more engineering notes:&lt;br&gt;
&lt;a href="https://www.facebook.com/profile.php?id=61574839666569" rel="noopener noreferrer"&gt;Facebook&lt;/a&gt; ｜ &lt;a href="https://hackmd.io/@jeff377" rel="noopener noreferrer"&gt;HackMD&lt;/a&gt; ｜ &lt;a href="https://github.com/jeff377" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; ｜ &lt;a href="https://www.nuget.org/profiles/jeff377" rel="noopener noreferrer"&gt;NuGet&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>blazor</category>
      <category>erp</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Polhem.Markdown: A Markdown Viewer and Editor for Avalonia and Blazor</title>
      <dc:creator>Jeff Tsai</dc:creator>
      <pubDate>Thu, 26 Nov 2026 01:07:17 +0000</pubDate>
      <link>https://dev.to/jeff377/polhemmarkdown-a-markdown-viewer-and-editor-for-avalonia-and-blazor-4hb0</link>
      <guid>https://dev.to/jeff377/polhemmarkdown-a-markdown-viewer-and-editor-for-avalonia-and-blazor-4hb0</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;The same text renders the same way in both front ends, with no extra syntax packages to install&lt;/p&gt;

&lt;p&gt;This post is based on version 0.3.1. The packages are still 0.x, so a minor release may still change the API; any change will be noted in the &lt;a href="https://github.com/polhem-dev/polhem-markdown/blob/main/CHANGELOG.md" rel="noopener noreferrer"&gt;CHANGELOG&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Polhem.Markdown is a set of Markdown packages for .NET. It provides two components each for Avalonia and Blazor:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;MarkdownViewer&lt;/code&gt;: displays Markdown, with support for GitHub Flavored Markdown, syntax highlighting, Mermaid diagrams and math.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;MarkdownEditor&lt;/code&gt;: edits the Markdown source, with a &lt;code&gt;MarkdownViewer&lt;/code&gt; next to it for a live preview.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;My project has Avalonia desktop and mobile apps as well as a Blazor web front end, and both need to display Markdown replies from AI. My first plan was to pick an existing package for each side. But two packages parse on their own and decide on their own how things look, so the same text ends up looking different in the two front ends, and the security rules have to be written twice. So I decided to build my own set.&lt;/p&gt;

&lt;p&gt;The source is on &lt;a href="https://github.com/polhem-dev/polhem-markdown" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; under the MIT license. It does not depend on the &lt;a href="https://github.com/polhem-dev/polhem" rel="noopener noreferrer"&gt;Polhem framework&lt;/a&gt;, so other projects can use it directly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjm8vtxtthjeiq7f3t1b3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjm8vtxtthjeiq7f3t1b3.png" alt="The same reply in the Blazor and Avalonia viewers" width="800" height="365"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  1️⃣ The packages
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Package&lt;/th&gt;
&lt;th&gt;Contents&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://www.nuget.org/packages/Polhem.Markdown" rel="noopener noreferrer"&gt;Polhem.Markdown&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Parses with &lt;a href="https://github.com/xoofx/markdig" rel="noopener noreferrer"&gt;Markdig&lt;/a&gt;, converts the result into its own document model, and applies the security policy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://www.nuget.org/packages/Polhem.Markdown.Avalonia" rel="noopener noreferrer"&gt;Polhem.Markdown.Avalonia&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;The &lt;code&gt;MarkdownViewer&lt;/code&gt; and &lt;code&gt;MarkdownEditor&lt;/code&gt; controls for Avalonia&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://www.nuget.org/packages/Polhem.Markdown.Blazor" rel="noopener noreferrer"&gt;Polhem.Markdown.Blazor&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;The &lt;code&gt;MarkdownViewer&lt;/code&gt; and &lt;code&gt;MarkdownEditor&lt;/code&gt; components for Blazor&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;An application references only the package for its own front end, and &lt;code&gt;Polhem.Markdown&lt;/code&gt; comes along with it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet add package Polhem.Markdown.Avalonia
dotnet add package Polhem.Markdown.Blazor
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The target framework is &lt;code&gt;net10.0&lt;/code&gt;, and the Avalonia side uses Avalonia 12.&lt;/p&gt;




&lt;h2&gt;
  
  
  2️⃣ How it differs from similar packages
&lt;/h2&gt;

&lt;p&gt;In November 2026, I looked through the open-source Markdown packages for Avalonia and Blazor on NuGet and read the source of a few of the more downloaded ones. There are plenty of viewers that support a single front end. Here is what this set deliberately does differently.&lt;/p&gt;

&lt;h3&gt;
  
  
  Both front ends draw the same model
&lt;/h3&gt;

&lt;p&gt;I did not find an open-source package that supports both Avalonia and Blazor. With one package on each side, the parser, the supported syntax and the handling of unrecognized syntax may all differ.&lt;/p&gt;

&lt;p&gt;The core package of Polhem.Markdown converts Markdig's syntax tree into its own model, and both viewers draw only that model. Tests guard this too: each side takes the same sample text, which covers every node, and checks that every kind of node is drawn (including the shapes of Mermaid diagrams and the parts of math formulas). If the model gains a node and one viewer does not keep up, its tests fail, so new syntax cannot show up on only one side.&lt;/p&gt;

&lt;p&gt;Also, Markdig's types do not appear in the public API, which is also checked by a test, so switching parsers later would not affect consumers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Syntax is built into the main package, and Blazor loads no third-party scripts
&lt;/h3&gt;

&lt;p&gt;A common approach is for the main package to handle only the basic syntax, with Mermaid, math and syntax highlighting in separate extension packages. On the Blazor side, scripts such as mermaid.js and highlight.js are usually loaded in the browser.&lt;/p&gt;

&lt;p&gt;This set puts all of it in the main package. The viewers lay out and draw Mermaid flowcharts and sequence diagrams themselves. Math is parsed once in the core; Blazor outputs MathML, which browsers support natively, and Avalonia does its own layout (the reasons are in &lt;a href="https://github.com/polhem-dev/polhem-markdown/blob/main/maintainers/adr/adr-002-math-one-structure-two-layouts.md" rel="noopener noreferrer"&gt;ADR-002&lt;/a&gt;). Syntax highlighting is also done in C#.&lt;/p&gt;

&lt;p&gt;So the Blazor viewer does not need to load any third-party scripts, and there is no CDN that might be unreachable. Only browser operations such as scrolling to a heading or copying code go through Blazor's JS interop.&lt;/p&gt;

&lt;h3&gt;
  
  
  A built-in security policy
&lt;/h3&gt;

&lt;p&gt;What the viewer displays is mostly replies from AI models, and a model's output can be steered by a third party (for example, through instructions hidden in a web page it read). So the links, images and HTML in the text have to be treated as untrusted input.&lt;/p&gt;

&lt;p&gt;A common approach on the Blazor side is to put the HTML produced by Markdig straight into the page. Raw HTML embedded in the Markdown is output as well, and links starting with &lt;code&gt;javascript:&lt;/code&gt; can still be clicked. This set puts the rules in the core package, and both viewers apply the same ones:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Raw HTML is limited to a small allowlist. &lt;code&gt;&amp;lt;div align&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;details&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;sup&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;kbd&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;picture&amp;gt;&lt;/code&gt;, which READMEs commonly use, are converted into nodes in the model. All other HTML is dropped, and &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; is removed together with its content.&lt;/li&gt;
&lt;li&gt;Link schemes have an allowlist. By default, only &lt;code&gt;http&lt;/code&gt;, &lt;code&gt;https&lt;/code&gt; and &lt;code&gt;mailto&lt;/code&gt; links can be clicked.&lt;/li&gt;
&lt;li&gt;Image schemes have an allowlist too. By default, only &lt;code&gt;https&lt;/code&gt; images are loaded.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The allowlists and the base for relative URLs are set in &lt;code&gt;MarkdownOptions&lt;/code&gt;. For example, to resolve relative links against the document's own URL and allow local images:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;viewer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MarkdownOptions&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;BaseUri&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Uri&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"https://example.com/docs/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;AllowedImageSchemes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s2"&gt;"file"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;SVG images are the one place where the two sides deliberately differ. Blazor hands them to the browser, where scripts inside an SVG do not run. Avalonia has no browser, so an SVG has to be decoded inside the application's own process. A review found that a crafted SVG could overflow the renderer's stack and terminate the app outright, without even an exception to catch. So the Avalonia version only draws SVGs from trusted badge services (the badges commonly seen on READMEs); the reasons are in &lt;a href="https://github.com/polhem-dev/polhem-markdown/blob/main/maintainers/adr/adr-001-svg-from-trusted-hosts.md" rel="noopener noreferrer"&gt;ADR-001&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  The editor and the viewer share the same parse
&lt;/h3&gt;

&lt;p&gt;Markdown editors are not rare, but the widely downloaded open-source packages mostly do either the viewer or the editor. Those with an editor usually highlight the source with a separate set of rules, which does not always agree with how the preview parses it.&lt;/p&gt;

&lt;p&gt;The editor in this set highlights according to the viewer's interpretation: whatever the editor marks as a heading is a heading in the preview. Every node in the model records its position in the source, so the editor and the preview can scroll together, and Ctrl-clicking (Command on macOS) in the preview jumps back to the source.&lt;/p&gt;

&lt;p&gt;The Blazor editor does not wrap a JavaScript editor. It uses a native &lt;code&gt;&amp;lt;textarea&amp;gt;&lt;/code&gt; for input and draws the colors behind it, so IME input (for Chinese, for example) and screen readers behave the same as in any other input box on the page.&lt;/p&gt;

&lt;h3&gt;
  
  
  Streaming and publishing
&lt;/h3&gt;

&lt;p&gt;AI replies arrive in chunks. Each time a chunk arrives, just set the &lt;code&gt;Markdown&lt;/code&gt; property again. The viewer compares the source and rebuilds only the blocks that changed, so the display does not flash as a whole, and no separate Append API is needed.&lt;/p&gt;

&lt;p&gt;On the publishing side, CI publishes and runs the core package's test program with Native AOT, and also publishes the Blazor WebAssembly sample with trimming (removing unused code at publish time). If a change stops working after trimming, the build fails. The Avalonia sample also has an iOS version that runs on the simulator.&lt;/p&gt;




&lt;h2&gt;
  
  
  3️⃣ Supported Markdown syntax
&lt;/h2&gt;

&lt;p&gt;The goal is to match how GitHub displays &lt;code&gt;.md&lt;/code&gt; files. Both viewers support every item in the table below:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Syntax&lt;/th&gt;
&lt;th&gt;How it is written or what it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Headings, emphasis, blockquotes, lists, thematic breaks&lt;/td&gt;
&lt;td&gt;CommonMark&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Strikethrough&lt;/td&gt;
&lt;td&gt;&lt;code&gt;~~text~~&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Task lists&lt;/td&gt;
&lt;td&gt;&lt;code&gt;- [x] done&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Tables with column alignment&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;:--&lt;/code&gt;, &lt;code&gt;:-:&lt;/code&gt;, &lt;code&gt;--:&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Footnotes&lt;/td&gt;
&lt;td&gt;&lt;code&gt;text[^note]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GitHub alerts&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;&amp;gt; [!NOTE]&lt;/code&gt;, &lt;code&gt;[!TIP]&lt;/code&gt;, &lt;code&gt;[!IMPORTANT]&lt;/code&gt;, &lt;code&gt;[!WARNING]&lt;/code&gt;, &lt;code&gt;[!CAUTION]&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Emoji shortcodes&lt;/td&gt;
&lt;td&gt;&lt;code&gt;:wave:&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;YAML front matter&lt;/td&gt;
&lt;td&gt;Hidden, not displayed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Links to headings&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;[setup](#setup)&lt;/code&gt; scrolls to that heading&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Code blocks&lt;/td&gt;
&lt;td&gt;Show the language and a copy button, with syntax highlighting for common languages such as C#, JSON, SQL, XML, JavaScript and Python&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Mermaid diagrams&lt;/td&gt;
&lt;td&gt;Flowcharts and sequence diagrams; other types are shown as code&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Math&lt;/td&gt;
&lt;td&gt;Inline &lt;code&gt;$…$&lt;/code&gt;, and display &lt;code&gt;$$…$$&lt;/code&gt; or fenced code blocks tagged &lt;code&gt;math&lt;/code&gt;; amounts of money in the text (such as &lt;code&gt;$100&lt;/code&gt;) stay as text&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HTML commonly used in READMEs&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;&amp;lt;div align&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;details&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;sup&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;kbd&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;picture&amp;gt;&lt;/code&gt;; other HTML is dropped&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Images&lt;/td&gt;
&lt;td&gt;Only &lt;code&gt;https&lt;/code&gt; by default; on Avalonia, SVG is limited to trusted badge hosts&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Code blocks are readable in both the light and dark themes:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftp9gx5roaqy4fqfauvkg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftp9gx5roaqy4fqfauvkg.png" alt="A table and syntax highlighting in the dark theme" width="800" height="251"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Mermaid labels can be in Chinese:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffs8xw960a58lmzlk2byq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffs8xw960a58lmzlk2byq.png" alt="A flowchart and a sequence diagram with Chinese labels" width="800" height="590"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Math supports fractions, sums, piecewise functions and matrices:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4z2wfzvm259fjvz644ok.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4z2wfzvm259fjvz644ok.png" alt="Inline math, fractions, sums, piecewise functions and matrices" width="797" height="121"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;HTML commonly used in READMEs is turned into content, while &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; is removed:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4f069ky8lon248om8sxg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4f069ky8lon248om8sxg.png" alt="Centered text, an expandable section, a removed script and footnotes" width="799" height="226"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  4️⃣ MarkdownViewer
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Avalonia
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;Window&lt;/span&gt; &lt;span class="na"&gt;xmlns:md&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"using:Polhem.Markdown.Avalonia"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;md:MarkdownViewer&lt;/span&gt; &lt;span class="na"&gt;Markdown&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"{Binding Reply}"&lt;/span&gt; &lt;span class="na"&gt;LinkActivated&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"OnLinkActivated"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/Window&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;OnLinkActivated&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;object&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;MarkdownLinkActivatedEventArgs&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;OpenInApp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Uri&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Handled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When a link is clicked, the viewer raises the &lt;code&gt;LinkActivated&lt;/code&gt; event and the application decides what to do, for example open a form inside the app. If the event is not handled (&lt;code&gt;Handled&lt;/code&gt; is not &lt;code&gt;true&lt;/code&gt;), the link is opened by the operating system.&lt;/p&gt;

&lt;p&gt;If the text contains Chinese, set &lt;code&gt;CjkFontFamily&lt;/code&gt;. When Chinese and English are mixed, Avalonia may render bold Chinese at regular weight (&lt;a href="https://github.com/AvaloniaUI/Avalonia/issues/18199" rel="noopener noreferrer"&gt;AvaloniaUI/Avalonia#18199&lt;/a&gt;). Specifying a Chinese font keeps it bold:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;md:MarkdownViewer&lt;/span&gt; &lt;span class="na"&gt;Markdown&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"{Binding Reply}"&lt;/span&gt;
                   &lt;span class="na"&gt;CjkFontFamily&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"PingFang TC, Microsoft JhengHei UI, Noto Sans CJK TC"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The default colors work in both the light and dark themes. To match your own theme, set brush properties such as &lt;code&gt;LinkForeground&lt;/code&gt; and &lt;code&gt;CodeBackground&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Blazor
&lt;/h3&gt;

&lt;p&gt;Add the stylesheet to the host page:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"_content/Polhem.Markdown.Blazor/polhem-markdown.css"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@using Polhem.Markdown.Blazor

&amp;lt;MarkdownViewer Markdown="@reply" /&amp;gt;

@code {
    private string reply = "# Hello";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without &lt;code&gt;OnLinkActivated&lt;/code&gt;, links open in a new tab. Once it is set, clicks are handed to your handler.&lt;/p&gt;

&lt;p&gt;The font and text color follow the surrounding page. The other colors and fonts are &lt;code&gt;--pmd-*&lt;/code&gt; CSS variables; the full list and defaults are at the top of &lt;code&gt;polhem-markdown.css&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.pmd&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nv"&gt;--pmd-link&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#0969da&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nv"&gt;--pmd-code-font&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"JetBrains Mono"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;monospace&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Polhem framework's Blazor front end comes in a MudBlazor version and a Fluent UI version. Each version's stylesheet maps these variables to its theme's colors, so the same component fits into both looks.&lt;/p&gt;




&lt;h2&gt;
  
  
  5️⃣ MarkdownEditor
&lt;/h2&gt;

&lt;p&gt;The editor edits the source; it is not WYSIWYG. Place it side by side with a &lt;code&gt;MarkdownViewer&lt;/code&gt; and you have an editing screen with a live preview. The editors on the two front ends behave the same:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Toolbar: bold, italic, strikethrough, inline code, link, image, heading, list, task list, blockquote, code block, table and thematic break, plus undo and redo. The labels are in English by default; you can replace them, for example with Chinese, or turn the toolbar off and put your own buttons there.&lt;/li&gt;
&lt;li&gt;Shortcuts and list keys: Ctrl (Command on macOS) with B, I, K or E toggles inline formatting; Enter continues a list, and Tab and Shift+Tab change an item's level.&lt;/li&gt;
&lt;li&gt;Line numbers: shown by default.&lt;/li&gt;
&lt;li&gt;Sync with the viewer: they scroll together, and you can jump from the preview back to the source; see the next section for how.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Avalonia
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ff2dqdbvr6oupbtvmmvke.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ff2dqdbvr6oupbtvmmvke.png" alt="The sample text in the editor, next to the viewer that renders it" width="800" height="375"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;Grid&lt;/span&gt; &lt;span class="na"&gt;ColumnDefinitions&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"*,*"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;md:MarkdownEditor&lt;/span&gt; &lt;span class="na"&gt;x:Name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Editor"&lt;/span&gt; &lt;span class="na"&gt;Markdown&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"{Binding Text}"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;md:MarkdownViewer&lt;/span&gt; &lt;span class="na"&gt;Grid.Column&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"1"&lt;/span&gt; &lt;span class="na"&gt;Markdown&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"{Binding Text}"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/Grid&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Formatting commands can also be called from code, for example from your own menu:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;Editor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ApplyFormat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MarkdownFormat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Bold&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// toggles bold on the selection, same as Ctrl+B&lt;/span&gt;
&lt;span class="nc"&gt;Editor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CycleHeading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                     &lt;span class="c1"&gt;// plain text → # → ## → ### → plain text&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replacing the toolbar labels with Chinese:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;Editor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ToolbarLabels&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;switch&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;MarkdownToolbarItem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Bold&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"粗體"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;MarkdownToolbarItem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Italic&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"斜體"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;   &lt;span class="c1"&gt;// null keeps the default label&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Avalonia version is built on &lt;a href="https://github.com/AvaloniaUI/AvaloniaEdit" rel="noopener noreferrer"&gt;AvaloniaEdit&lt;/a&gt;. The editor brings the styles it needs, so it works with any theme.&lt;/p&gt;

&lt;h3&gt;
  
  
  Blazor
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fskxm88lntwbfv6vd3d5u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fskxm88lntwbfv6vd3d5u.png" alt="The sample text in the Blazor editor in a browser, next to the viewer" width="800" height="375"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div style="display: grid; grid-template-columns: 1fr 1fr; height: 30rem"&amp;gt;
    &amp;lt;MarkdownEditor @ref="editor" @bind-Markdown="text" aria-label="Markdown source" /&amp;gt;
    &amp;lt;MarkdownViewer Markdown="@text" /&amp;gt;
&amp;lt;/div&amp;gt;

@code {
    private MarkdownEditor editor = default!;
    private string text = "# Hello";

    // Toggles bold on the selection, same as Ctrl+B
    private Task Bold() =&amp;gt; editor.ApplyFormatAsync(MarkdownFormat.Bold);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Blazor formatting commands are asynchronous (&lt;code&gt;ApplyFormatAsync&lt;/code&gt;, &lt;code&gt;CycleHeadingAsync&lt;/code&gt;), and the toolbar labels are replaced through &lt;code&gt;ToolbarLabels&lt;/code&gt; in the same way.&lt;/p&gt;

&lt;p&gt;Because the colors are drawn behind the &lt;code&gt;&amp;lt;textarea&amp;gt;&lt;/code&gt; and must line up with its text, &lt;code&gt;--pmd-code-font&lt;/code&gt; has to stay a monospace font. The editor fills the height its container gives it and works in all three render modes: interactive server, WebAssembly and auto. There is no search panel yet.&lt;/p&gt;




&lt;h2&gt;
  
  
  6️⃣ Connecting the editor and the viewer
&lt;/h2&gt;

&lt;p&gt;The editor and the viewer are two independent components, and the application wires them together. These are the things to handle:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Updating the preview: hand the editor's text to the viewer. Updating on every keystroke works, but if you wait until typing pauses, the viewer does not have to rebuild on every key.&lt;/li&gt;
&lt;li&gt;Synchronized scrolling: both sides report which position in the source (a character offset) is at the top of the view, and both can scroll to a given source position. When one side scrolls, pass its position to the other.&lt;/li&gt;
&lt;li&gt;Jumping back to the source: when you Ctrl-click (Command on macOS) in the preview, the viewer raises an event carrying the source position of that content, and the application moves the editor's caret there.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Synchronized scrolling has one catch: if A's scrolling moves B, B's scrolling reports a position again, and both sides accept everything, they end up pulling each other back and forth. The samples let only the side under the mouse pointer, or the side with keyboard focus, drive the other.&lt;/p&gt;

&lt;h3&gt;
  
  
  Avalonia
&lt;/h3&gt;

&lt;p&gt;The Avalonia viewer does not scroll by itself; the enclosing &lt;code&gt;ScrollViewer&lt;/code&gt; does. So the viewer provides conversions: &lt;code&gt;GetVerticalOffset&lt;/code&gt; turns a source position into a Y coordinate inside the viewer, and &lt;code&gt;GetSourceOffset&lt;/code&gt; does the reverse.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;viewerScroll&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ScrollViewer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;Content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;viewer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Editor scrolls → the viewer follows&lt;/span&gt;
&lt;span class="n"&gt;editor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PropertyChanged&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Property&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nc"&gt;MarkdownEditor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FirstVisibleSourceOffsetProperty&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nc"&gt;EditorLeads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;viewer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetVerticalOffset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;editor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FirstVisibleSourceOffset&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;viewerScroll&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Offset&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Vector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;viewerScroll&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Offset&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;X&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Viewer scrolls → the editor follows&lt;/span&gt;
&lt;span class="n"&gt;viewerScroll&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ScrollChanged&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nc"&gt;EditorLeads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;viewer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetSourceOffset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;viewerScroll&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Offset&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;editor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ScrollToSourceOffset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Ctrl- or Command-click in the preview → the editor moves to that source&lt;/span&gt;
&lt;span class="n"&gt;viewer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SourceRequested&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;editor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CaretOffset&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SourceOffset&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;editor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ScrollToSourceOffset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SourceOffset&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;editor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Focus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Handled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nc"&gt;EditorLeads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;viewerScroll&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IsPointerOver&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;editor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IsPointerOver&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;editor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IsKeyboardFocusWithin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This assumes the viewer sits directly inside the &lt;code&gt;ScrollViewer&lt;/code&gt;. If a container with padding, such as a &lt;code&gt;Border&lt;/code&gt;, sits in between, convert the coordinates with &lt;code&gt;TranslatePoint&lt;/code&gt; as well; the full version is in &lt;code&gt;samples/MarkdownGallery/SourceSync.cs&lt;/code&gt; in the repo.&lt;/p&gt;

&lt;h3&gt;
  
  
  Blazor
&lt;/h3&gt;

&lt;p&gt;The Blazor viewer finds the nearest scrolling area around it by itself (or uses the whole page if there is none). Both components report their position through &lt;code&gt;FirstVisibleSourceOffsetChanged&lt;/code&gt; and scroll to a given position with &lt;code&gt;ScrollToSourceOffsetAsync&lt;/code&gt;, so the wiring is symmetric:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div @onpointerenter="() =&amp;gt; leader = Pane.Editor" @onfocusin="() =&amp;gt; leader = Pane.Editor"&amp;gt;
    &amp;lt;MarkdownEditor @ref="editor" @bind-Markdown="text" aria-label="Markdown source"
                    FirstVisibleSourceOffsetChanged="EditorScrolled" /&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div @onpointerenter="() =&amp;gt; leader = Pane.Viewer" @onfocusin="() =&amp;gt; leader = Pane.Viewer"&amp;gt;
    &amp;lt;MarkdownViewer @ref="viewer" Markdown="@text"
                    FirstVisibleSourceOffsetChanged="ViewerScrolled" OnSourceRequested="GoToSource" /&amp;gt;
&amp;lt;/div&amp;gt;

@code {
    private enum Pane { Editor, Viewer }

    private MarkdownEditor? editor;
    private MarkdownViewer? viewer;
    private Pane leader = Pane.Editor;
    private string text = "# Hello";

    private async Task EditorScrolled(int offset)
    {
        if (leader == Pane.Editor &amp;amp;&amp;amp; viewer is not null)
            await viewer.ScrollToSourceOffsetAsync(offset);
    }

    private async Task ViewerScrolled(int offset)
    {
        if (leader == Pane.Viewer &amp;amp;&amp;amp; editor is not null)
            await editor.ScrollToSourceOffsetAsync(offset);
    }

    private async Task GoToSource(MarkdownSourceRequestedEventArgs e)
    {
        if (editor is null) return;
        leader = Pane.Editor;
        await editor.SetCaretOffsetAsync(e.SourceOffset);
        await editor.ScrollToSourceOffsetAsync(e.SourceOffset);
        await editor.FocusAsync();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To keep the example short, it uses &lt;code&gt;@bind-Markdown&lt;/code&gt;, so the preview updates on every keystroke. For the version that waits until typing has paused for 150 milliseconds, see &lt;code&gt;samples/MarkdownGallery.Blazor/Components/GalleryPage.razor&lt;/code&gt; in the repo.&lt;/p&gt;




&lt;h2&gt;
  
  
  7️⃣ Samples
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;samples/&lt;/code&gt; folder in the repo has three versions of the same Gallery: Avalonia desktop, Blazor Server and Blazor WebAssembly. The Gallery lists every kind of node, can switch between light and dark themes, and can "replay": it types the text out in chunks to simulate a streaming reply. Turn on the Edit switch and the editor appears next to the viewer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/polhem-dev/polhem-markdown.git
&lt;span class="nb"&gt;cd&lt;/span&gt; polhem-markdown
dotnet run --project samples/MarkdownGallery.Desktop
dotnet run --project samples/MarkdownGallery.Blazor
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  ✅ Conclusion
&lt;/h2&gt;

&lt;p&gt;If your application has both Avalonia and Blazor front ends, or needs to display Markdown from untrusted sources (such as AI replies) without loading Mermaid or MathJax scripts into the page, give this set a try.&lt;/p&gt;

&lt;p&gt;It is not the right fit for every project, though. The known limitations right now:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Only the syntax and HTML listed above are supported; all other HTML is dropped. For documents that need full HTML, displaying them in a browser is a better fit.&lt;/li&gt;
&lt;li&gt;Mermaid supports only flowcharts and sequence diagrams.&lt;/li&gt;
&lt;li&gt;The Avalonia viewer cannot yet select text across blocks (&lt;a href="https://github.com/polhem-dev/polhem-markdown/issues/1" rel="noopener noreferrer"&gt;#1&lt;/a&gt;). For now each block is selectable on its own, and code blocks have a copy button.&lt;/li&gt;
&lt;li&gt;The editor has no WYSIWYG mode and no export feature, and the Blazor editor does not have a search panel yet.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The packages are still 0.x, and the API may still change. Feel free to try them, and if you have any questions or suggestions, open an issue on GitHub.&lt;/p&gt;

&lt;h3&gt;
  
  
  Further reading
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com/xoofx/markdig" rel="noopener noreferrer"&gt;Markdig&lt;/a&gt;: the Markdown parser for .NET that this package builds on&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.github.com/gfm/" rel="noopener noreferrer"&gt;GitHub Flavored Markdown Spec&lt;/a&gt;: the GFM syntax specification&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://mermaid.js.org/" rel="noopener noreferrer"&gt;Mermaid&lt;/a&gt;: a syntax for describing diagrams in text&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/docs/Web/MathML" rel="noopener noreferrer"&gt;MathML&lt;/a&gt;: the browser's native markup for math&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;📘 Original article on HackMD, in Traditional Chinese:&lt;br&gt;
👉 &lt;a href="https://hackmd.io/@jeff377/polhem-markdown" rel="noopener noreferrer"&gt;https://hackmd.io/@jeff377/polhem-markdown&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📢 You are welcome to republish this, please credit the source.&lt;br&gt;
📬 Follow for more engineering notes:&lt;br&gt;
&lt;a href="https://www.facebook.com/profile.php?id=61574839666569" rel="noopener noreferrer"&gt;Facebook&lt;/a&gt; ｜ &lt;a href="https://hackmd.io/@jeff377" rel="noopener noreferrer"&gt;HackMD&lt;/a&gt; ｜ &lt;a href="https://github.com/jeff377" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; ｜ &lt;a href="https://www.nuget.org/profiles/jeff377" rel="noopener noreferrer"&gt;NuGet&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>markdown</category>
      <category>blazor</category>
      <category>avalonia</category>
    </item>
    <item>
      <title>Can a Form Be Described Only Once?</title>
      <dc:creator>Jeff Tsai</dc:creator>
      <pubDate>Wed, 25 Nov 2026 02:56:29 +0000</pubDate>
      <link>https://dev.to/jeff377/can-a-form-be-described-only-once-27ha</link>
      <guid>https://dev.to/jeff377/can-a-form-be-described-only-once-27ha</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;A guide to a 10-minute video: from an empty project and Excel workbooks to four front ends&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;  &lt;iframe src="https://www.youtube.com/embed/5WjUTwoPyGw" width="710" height="399"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://youtu.be/5WjUTwoPyGw" rel="noopener noreferrer"&gt;Full version&lt;/a&gt; (about 10 minutes)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://youtu.be/OAlAgAuGdKE" rel="noopener noreferrer"&gt;Short version&lt;/a&gt; (about 3 minutes)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The video has English narration, with English and Traditional Chinese subtitles on YouTube.&lt;/p&gt;

&lt;p&gt;This year I wrote a thirty-post series for the iThome Ironman contest, &lt;a href="https://dev.to/jeff377/erp-architects-notes-designing-a-definition-driven-framework-series-index-4ln7"&gt;ERP Architect's Notes: Designing a Definition-Driven Framework&lt;/a&gt;. It covers the mechanisms and trade-offs of definition-driven design, and the framework at the time was &lt;a href="https://github.com/jeff377/bee-library" rel="noopener noreferrer"&gt;Bee.NET&lt;/a&gt;. After the series ended, Polhem took over from Bee.NET, and the packages moved from &lt;code&gt;Bee.*&lt;/code&gt; to &lt;code&gt;Polhem.*&lt;/code&gt;. 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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/polhem-dev/polhem" rel="noopener noreferrer"&gt;Polhem&lt;/a&gt; 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 &lt;a href="https://avaloniaui.net/" rel="noopener noreferrer"&gt;Avalonia&lt;/a&gt;, so the same UI code runs on the desktop, in the browser, and on iOS and Android.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;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.&lt;/p&gt;




&lt;h2&gt;
  
  
  1️⃣ Opening: why is one field written so many times?
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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?&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;




&lt;h2&gt;
  
  
  2️⃣ Creating the project and preparing the database
&lt;/h2&gt;

&lt;p&gt;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 &lt;code&gt;Define&lt;/code&gt; folder for the definition files, and the Polhem packages and the SQL Server driver are already referenced.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;




&lt;h2&gt;
  
  
  3️⃣ Importing Excel: the workbook is the specification
&lt;/h2&gt;

&lt;p&gt;This is the longest part of the video. Before importing, it opens the orders workbook to show what it describes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The field name and data type; the type decides the column type in the database&lt;/li&gt;
&lt;li&gt;The maximum length of text fields&lt;/li&gt;
&lt;li&gt;The input control, for example a drop-down list for the status&lt;/li&gt;
&lt;li&gt;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&lt;/li&gt;
&lt;li&gt;The amount formula: quantity times unit price, less the discount&lt;/li&gt;
&lt;li&gt;A separate sheet that lists the rules checked before saving&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Definition file&lt;/th&gt;
&lt;th&gt;What it describes&lt;/th&gt;
&lt;th&gt;Where it is used&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;FormSchema&lt;/td&gt;
&lt;td&gt;The form's fields, relationships, computed fields and rules&lt;/td&gt;
&lt;td&gt;The other definitions are derived from it, and the CRUD SQL is built from it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;TableSchema&lt;/td&gt;
&lt;td&gt;The structure of the physical tables&lt;/td&gt;
&lt;td&gt;Derived from FormSchema, used to create and upgrade the tables&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;FormLayout&lt;/td&gt;
&lt;td&gt;How the fields are arranged&lt;/td&gt;
&lt;td&gt;Derived from FormSchema, each front end draws its screens from it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Language files&lt;/td&gt;
&lt;td&gt;Field captions in each language&lt;/td&gt;
&lt;td&gt;The text shown on screen&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;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.&lt;/p&gt;




&lt;h2&gt;
  
  
  4️⃣ Running: a system with no form code
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;




&lt;h2&gt;
  
  
  5️⃣ Iterating: when requirements change, change the definitions
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;




&lt;h2&gt;
  
  
  6️⃣ Visual fine-tuning and multiple front ends
&lt;/h2&gt;

&lt;p&gt;The video ends with two short parts.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;




&lt;h2&gt;
  
  
  7️⃣ What the video does not show
&lt;/h2&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;li&gt;The framework has permissions, auditing and transport encryption, but the video does not demonstrate them.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ✅ Conclusion
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Try it without installing anything: &lt;a href="https://polhem-northwind.onrender.com/" rel="noopener noreferrer"&gt;Blazor Server&lt;/a&gt; or &lt;a href="https://polhem-northwind.onrender.com/web/" rel="noopener noreferrer"&gt;Avalonia WebAssembly&lt;/a&gt;. Sign in with &lt;code&gt;demo&lt;/code&gt; / &lt;code&gt;demo&lt;/code&gt;, or &lt;code&gt;demo-ro&lt;/code&gt; / &lt;code&gt;demo&lt;/code&gt; 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.&lt;/p&gt;

&lt;p&gt;To try it yourself, install the template with &lt;code&gt;dotnet new install Polhem.Templates&lt;/code&gt;. The documentation and samples are on &lt;a href="https://github.com/polhem-dev/polhem" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;📘 Original article on HackMD, in Traditional Chinese:&lt;br&gt;
👉 &lt;a href="https://hackmd.io/@jeff377/polhem-video-guide" rel="noopener noreferrer"&gt;https://hackmd.io/@jeff377/polhem-video-guide&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📢 You are welcome to republish this, please credit the source.&lt;br&gt;
📬 Follow for more engineering notes:&lt;br&gt;
&lt;a href="https://www.facebook.com/profile.php?id=61574839666569" rel="noopener noreferrer"&gt;Facebook&lt;/a&gt; ｜ &lt;a href="https://hackmd.io/@jeff377" rel="noopener noreferrer"&gt;HackMD&lt;/a&gt; ｜ &lt;a href="https://github.com/jeff377" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; ｜ &lt;a href="https://www.nuget.org/profiles/jeff377" rel="noopener noreferrer"&gt;NuGet&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>avalonia</category>
      <category>erp</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Day 30: Three Depths of Development and the Big Picture of the Framework</title>
      <dc:creator>Jeff Tsai</dc:creator>
      <pubDate>Mon, 23 Nov 2026 00:32:11 +0000</pubDate>
      <link>https://dev.to/jeff377/day-30-three-depths-of-development-and-the-big-picture-of-the-framework-n5n</link>
      <guid>https://dev.to/jeff377/day-30-three-depths-of-development-and-the-big-picture-of-the-framework-n5n</guid>
      <description>&lt;p&gt;The previous post, &lt;a href="https://dev.to/jeff377/day-29-which-part-definitions-the-framework-and-application-code-each-own-2a5m"&gt;Which Part Definitions, the Framework and Application Code Each Own&lt;/a&gt;, ended by saying that this one would gather the thirty posts into a single big picture.&lt;/p&gt;

&lt;p&gt;This post covers:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The three depths the framework offers along the axis of NoCode, LowCode and AnyCode&lt;/li&gt;
&lt;li&gt;The big picture: what grows out of a single &lt;code&gt;FormSchema&lt;/code&gt;, and what every call passes through&lt;/li&gt;
&lt;li&gt;Which systems this design suits and does not suit, and where the long term return lands&lt;/li&gt;
&lt;li&gt;The road not yet finished, and what is deliberately not done&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  1. Three depths
&lt;/h2&gt;

&lt;p&gt;The three depths are not mutually exclusive, and one form can use all three:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Depth&lt;/th&gt;
&lt;th&gt;What you write&lt;/th&gt;
&lt;th&gt;What changing it costs&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;NoCode&lt;/td&gt;
&lt;td&gt;Definitions, being &lt;code&gt;FormSchema&lt;/code&gt;, &lt;code&gt;TableSchema&lt;/code&gt;, &lt;code&gt;FormLayout&lt;/code&gt;, and the &lt;code&gt;FormRule&lt;/code&gt; and &lt;code&gt;ValueExpression&lt;/code&gt; entries on fields&lt;/td&gt;
&lt;td&gt;Swapping in another definition&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;LowCode&lt;/td&gt;
&lt;td&gt;Overridden business object steps and business plugins&lt;/td&gt;
&lt;td&gt;Compiling, packaging and deploying&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;AnyCode&lt;/td&gt;
&lt;td&gt;A custom front end, business object or repository, or an &lt;code&gt;ExecFunc&lt;/code&gt; method&lt;/td&gt;
&lt;td&gt;Compiling, packaging and deploying&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;What can be done by changing a definition is NoCode. Adding a piece of code at a step or stage the framework has already cut out is LowCode. Writing a whole stretch yourself because the framework's flow cannot hold it is AnyCode.&lt;/p&gt;

&lt;p&gt;Logic that every company does the same way moves step by step from LowCode into definitions and the framework, following the process in the previous post's section on converging from the application into the framework.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. The big picture
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Path&lt;/th&gt;
&lt;th&gt;Question it answers&lt;/th&gt;
&lt;th&gt;As forms are added&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;The vertical path&lt;/td&gt;
&lt;td&gt;What this form looks like&lt;/td&gt;
&lt;td&gt;Grows linearly&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The horizontal path&lt;/td&gt;
&lt;td&gt;What happened on this call&lt;/td&gt;
&lt;td&gt;Does not change&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The vertical path:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;             Definitions (single source of truth)
                   FormSchema at the center
                               │
   ┌───────────────────────────┼───────────────────────────┐
   │ generated                 │ generated                 │ evaluated
   ↓                           ↓                           ↓
 Layouts                     TableSchema                 FormRule
 → record, list, lookup      → upgraded after comparing  → at save and delete
 CRUD statements             DataSet structure           ValueExpression
 → assembled at run time     → no entity classes needed  → when a related field changes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The horizontal path:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; Front end → Connector → serialize → compress → encrypt
                                                   ↓
             Endpoint → access control → session → BO (extension points and plugins)
                                                   ↓
                                              Repository → data access → database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Mechanisms that cut across both paths:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Mechanism&lt;/th&gt;
&lt;th&gt;Post&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Definition caching and cross-node invalidation&lt;/td&gt;
&lt;td&gt;&lt;a href="https://dev.to/jeff377/day-12-cross-node-invalidation-for-definition-and-data-caches-17gf"&gt;Cross-Node Invalidation for Definition and Data Caches&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The error contract&lt;/td&gt;
&lt;td&gt;&lt;a href="https://dev.to/jeff377/day-18-the-error-contract-and-user-visible-messages-1d0n"&gt;The Error Contract and User Visible Messages&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Customization overlays and multiple languages&lt;/td&gt;
&lt;td&gt;
&lt;a href="https://dev.to/jeff377/day-19-the-scope-of-the-customization-layer-and-overlay-granularity-3mfg"&gt;The Scope of the Customization Layer and Overlay Granularity&lt;/a&gt; and &lt;a href="https://dev.to/jeff377/day-20-multilingual-customization-and-resource-structure-262p"&gt;Multilingual Customization and Resource Structure&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Deciding and applying permissions&lt;/td&gt;
&lt;td&gt;&lt;a href="https://dev.to/jeff377/day-24-the-three-axes-of-permissions-action-row-and-column-15fo"&gt;The Three Axes of Permissions: Action, Row and Column&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Auditing&lt;/td&gt;
&lt;td&gt;&lt;a href="https://dev.to/jeff377/day-25-the-audit-trail-and-the-before-and-after-values-of-a-change-1j5k"&gt;The Audit Trail and the Before and After Values of a Change&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Numeric rounding, the semantics of time, and time zone conversion&lt;/td&gt;
&lt;td&gt;
&lt;a href="https://dev.to/jeff377/day-26-numeric-semantics-multiple-currencies-and-units-of-measure-2n2p"&gt;Numeric Semantics, Multiple Currencies and Units of Measure&lt;/a&gt;, &lt;a href="https://dev.to/jeff377/day-27-the-semantic-boundaries-between-date-datetime-and-time-24i8"&gt;The Semantic Boundaries Between Date, DateTime and Time&lt;/a&gt; and &lt;a href="https://dev.to/jeff377/day-28-time-zone-conversion-of-datetime-3ona"&gt;Time Zone Conversion of DateTime&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Mechanisms all over the picture keep using the same few principles:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Principle&lt;/th&gt;
&lt;th&gt;Examples&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Position decides meaning&lt;/td&gt;
&lt;td&gt;Which side of the transaction an extension point is on, in &lt;a href="https://dev.to/jeff377/day-14-extension-points-on-a-business-object-and-when-to-override-them-43lm"&gt;Extension Points on a Business Object and When to Override Them&lt;/a&gt;. Whether a value is kept per process, per session or per call, in &lt;a href="https://dev.to/jeff377/day-17-the-connector-and-the-convergence-of-client-responsibilities-5d9k"&gt;The Connector and the Convergence of Client Responsibilities&lt;/a&gt;. Which layer stands in when a translation cannot be found, in the post on multilingual customization&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Failure has to be visible&lt;/td&gt;
&lt;td&gt;Whether you can tell that a cache has gone stale, in the post on cross-node invalidation. Whether a &lt;code&gt;ProgId&lt;/code&gt; bound to the wrong type fails on the spot or looks like it is still running, in &lt;a href="https://dev.to/jeff377/day-15-api-contract-layering-and-the-type-registry-12mm"&gt;API Contract Layering and the Type Registry&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Everything the caller sends is a claim, not a fact&lt;/td&gt;
&lt;td&gt;A type name has to pass the allowlist first, in &lt;a href="https://dev.to/jeff377/day-22-the-division-of-labor-between-xml-json-and-messagepack-51h4"&gt;The Division of Labor Between XML, JSON and MessagePack&lt;/a&gt;. &lt;code&gt;PayloadFormat&lt;/code&gt; has to match the level the method declares, in &lt;a href="https://dev.to/jeff377/day-23-the-api-payload-security-pipeline-order-protection-levels-and-keys-ona"&gt;The API Payload Security Pipeline: Order, Protection Levels and Keys&lt;/a&gt;. The department column is looked up in the database once more, in the post on permissions&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The paths in the picture are offered, not enforced. Open your own connection, assemble your own statements, or go around the entry point, and none of the cross-cutting mechanisms take effect.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Which systems this suits and does not suit
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://dev.to/jeff377/day-2-an-overview-of-definition-driven-architecture-31hi"&gt;An Overview of Definition-Driven Architecture&lt;/a&gt; asked three questions before you commit. Each gets one more sentence here, and two more questions are added:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Question&lt;/th&gt;
&lt;th&gt;The answer before&lt;/th&gt;
&lt;th&gt;What going through the series adds&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Are the screens standardized forms?&lt;/td&gt;
&lt;td&gt;Layouts converge into a master block plus a detail block, and dashboards and scheduling are not in the pattern&lt;/td&gt;
&lt;td&gt;Screens outside the pattern use manual layout, and the part that binds data is shared with forms, as &lt;a href="https://dev.to/jeff377/day-7-formlayout-and-screen-generation-for-multiple-front-ends-1edf"&gt;FormLayout and Screen Generation for Multiple Front Ends&lt;/a&gt; showed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Is a request a single document?&lt;/td&gt;
&lt;td&gt;The &lt;code&gt;DataSet&lt;/code&gt; is self describing, and every response carries its structure along&lt;/td&gt;
&lt;td&gt;Definitions have to be cached, and changes reach each node through notifications, as the post on cross-node invalidation showed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;How long does the system have to live?&lt;/td&gt;
&lt;td&gt;The cost is concentrated up front, and the return takes years to show&lt;/td&gt;
&lt;td&gt;Where the return lands is laid out below&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Is it connecting to an existing database?&lt;/td&gt;
&lt;td&gt;The overview post did not ask&lt;/td&gt;
&lt;td&gt;Existing tables first have to be changed to carry the framework's system fields, as &lt;a href="https://dev.to/jeff377/day-3-the-northwind-case-study-eight-forms-and-a-custom-business-object-3i33"&gt;The Northwind Case Study: Eight Forms and a Custom Business Object&lt;/a&gt; noted&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Will a second one appear?&lt;/td&gt;
&lt;td&gt;The overview post did not ask this either&lt;/td&gt;
&lt;td&gt;A second company, a second language, a second role, a second currency. If none will appear, mechanisms such as the customization layer, multiple languages, permission models and the currency master are pure cost, as the previous post concluded&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href="https://dev.to/jeff377/day-1-why-erp-needs-a-definition-driven-framework-1c2b"&gt;Why ERP Needs a Definition-Driven Framework&lt;/a&gt; said that in the short term Code-First wins almost every time. The return on definition-driven design is long term:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Return&lt;/th&gt;
&lt;th&gt;Where it lands&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Development process&lt;/td&gt;
&lt;td&gt;There is only one way to do the same thing, and nothing to compile, package or deploy between changing a definition and it taking effect&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Maintenance cost&lt;/td&gt;
&lt;td&gt;Thousands of structurally identical &lt;code&gt;FormSchema&lt;/code&gt; files can be checked by one script, for example whether every &lt;code&gt;Currency&lt;/code&gt; field is marked with &lt;code&gt;NumberKind&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Education cost&lt;/td&gt;
&lt;td&gt;Understand one form and you understand them all, as the overview post said. Extension points are listed, and which step you override and which stage you hook into each have a fixed name&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;User training&lt;/td&gt;
&lt;td&gt;The layout vocabulary is small, so training cost does not grow in proportion to the number of forms, as the post on &lt;code&gt;FormLayout&lt;/code&gt; said&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  4. The road not yet finished
&lt;/h2&gt;

&lt;p&gt;The previous post separated boundaries from backlog: what deliberately does not go through definitions is a boundary, and what the framework has not finished is backlog. The table below sorts what is not yet complete by why it is not complete:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Why it is not yet complete&lt;/th&gt;
&lt;th&gt;Examples&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Should converge, and has not yet&lt;/td&gt;
&lt;td&gt;The document numbering rule, a prefix plus date plus a sequence, which every company currently writes itself&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The declarative side has not caught it yet&lt;/td&gt;
&lt;td&gt;Cross row aggregation, such as at least one detail row and the master total; and the query condition screen for reports, where &lt;code&gt;FilterNode&lt;/code&gt; already exists but a layout generator is missing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The mechanism is there, but only half done&lt;/td&gt;
&lt;td&gt;Rounding a value to the column's capacity before a write has a rule and no implementation, as the post on numeric semantics noted; there is no extension point inside the transaction yet; the front end has no logout flow; the service container for local calls is not yet DI based; JSON-RPC batch requests are not implemented&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The definitions are enough, and the tools are not built yet&lt;/td&gt;
&lt;td&gt;Exporting import formats and test data from definitions, as in &lt;a href="https://dev.to/jeff377/day-9-data-and-definitions-split-apart-so-forms-need-no-types-of-their-own-505g"&gt;Data and Definitions Split Apart, So Forms Need No Types of Their Own&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Not unfinished, but not done by design&lt;/td&gt;
&lt;td&gt;Genuine business judgment, SQL for reports and batch jobs, and deep screen customization, as the previous post laid out; and &lt;code&gt;ValueExpression&lt;/code&gt; does not query the database&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The declarative side not having caught something yet is progress: add aggregate functions and evaluation ordering, and it can become a declaration. Not done by design is a design decision. If &lt;code&gt;ValueExpression&lt;/code&gt; could query the database, for example, the front end and the back end could not compute the same answer. Filing either one under the wrong heading costs something different. Treat progress as a boundary, and things that could have converged stay in every company's code for good. Treat a design decision as backlog, and the framework grows into one that can do anything, which means it has made no decisions for you, as the overview post put it.&lt;/p&gt;

&lt;p&gt;The half done kind is the easiest to mistake for finished when reading the documentation: a rule written with no implementation, a route missing one stretch, something built with no caller. Take rounding to capacity before a write. If the &lt;code&gt;discount&lt;/code&gt; column from the previous post, declared with no decimal places, is someday filled with a decimal, whether it gets rounded and how is up to whichever database it happens to be.&lt;/p&gt;




&lt;h2&gt;
  
  
  In closing
&lt;/h2&gt;

&lt;p&gt;The first post said the enemy of ERP is restatement. What definition-driven design eliminates is not restatement but copying. The facts about one field still appear in &lt;code&gt;FormLayout&lt;/code&gt;, &lt;code&gt;TableSchema&lt;/code&gt;, the statements, the &lt;code&gt;DataSet&lt;/code&gt;, &lt;code&gt;FormRule&lt;/code&gt;, auditing, permissions and time zone conversion. The difference is that all of these places read the same &lt;code&gt;FormSchema&lt;/code&gt;, rather than each keeping a copy of its own.&lt;/p&gt;

&lt;p&gt;With everything reading the same one, that one has to be:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Requirement&lt;/th&gt;
&lt;th&gt;Post&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Wide enough to carry meaning beyond structure&lt;/td&gt;
&lt;td&gt;The overview post&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Kept current by something that guarantees every node gets the latest version, or they stay on old values&lt;/td&gt;
&lt;td&gt;The post on cross-node invalidation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Clear about who can change it, and down to which layer&lt;/td&gt;
&lt;td&gt;The post on the customization layer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Built so that a missing declaration is visible, because once something is left out the remaining values are still valid&lt;/td&gt;
&lt;td&gt;The post on numeric semantics&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A definition is structured data, and &lt;a href="https://dev.to/jeff377/day-5-how-formschema-drives-sql-4b4f"&gt;How FormSchema Drives SQL&lt;/a&gt; listed batch scripts, tools that produce definitions, and AI as all able to work with it. Now that AI can write code, what it produces can be a definition: build time diagnostics check it, the framework expands it, and structurally identical forms can be made one after another by the same procedure. The work of an ERP engineer moves accordingly to the framework, the mechanisms and this procedure, and what has to be understood is how the whole architecture works.&lt;/p&gt;

&lt;p&gt;The thirty posts end here. As the first post said, the framework's only role in this series is to show that these ideas are not just talk on paper. You can skip it entirely, take away only the idea of letting definitions be the source, and build that idea in the languages and tools you know.&lt;/p&gt;




&lt;p&gt;📚 Part of the thirty-post series &lt;a href="https://dev.to/jeff377/erp-architects-notes-designing-a-definition-driven-framework-series-index-4ln7"&gt;ERP Architect's Notes: Designing a Definition-Driven Framework&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📘 Original article on HackMD, in Traditional Chinese:&lt;br&gt;
👉 &lt;a href="https://hackmd.io/@jeff377/day-30-depths-and-map" rel="noopener noreferrer"&gt;https://hackmd.io/@jeff377/day-30-depths-and-map&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📢 You are welcome to republish this, please credit the source.&lt;br&gt;
📬 Follow for more engineering notes:&lt;br&gt;
&lt;a href="https://www.facebook.com/profile.php?id=61574839666569" rel="noopener noreferrer"&gt;Facebook&lt;/a&gt; ｜ &lt;a href="https://hackmd.io/@jeff377" rel="noopener noreferrer"&gt;HackMD&lt;/a&gt; ｜ &lt;a href="https://github.com/jeff377" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; ｜ &lt;a href="https://www.nuget.org/profiles/jeff377" rel="noopener noreferrer"&gt;NuGet&lt;/a&gt;&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>dotnet</category>
      <category>erp</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Day 29: Which Part Definitions, the Framework and Application Code Each Own</title>
      <dc:creator>Jeff Tsai</dc:creator>
      <pubDate>Sun, 22 Nov 2026 13:25:02 +0000</pubDate>
      <link>https://dev.to/jeff377/day-29-which-part-definitions-the-framework-and-application-code-each-own-2a5m</link>
      <guid>https://dev.to/jeff377/day-29-which-part-definitions-the-framework-and-application-code-each-own-2a5m</guid>
      <description>&lt;p&gt;The previous post, &lt;a href="https://dev.to/jeff377/day-28-time-zone-conversion-of-datetime-3ona"&gt;Time Zone Conversion of DateTime&lt;/a&gt;, ended by saying that this one would go back to the case study and put those mechanisms in their places one by one.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/jeff377/day-1-why-erp-needs-a-definition-driven-framework-1c2b"&gt;Why ERP Needs a Definition-Driven Framework&lt;/a&gt; said that of Northwind's eight forms only one needs code written for it, and promised to lay the whole application out and reconcile it item by item before the series closes. What is inside that one form, &lt;a href="https://dev.to/jeff377/day-3-the-northwind-case-study-eight-forms-and-a-custom-business-object-3i33"&gt;The Northwind Case Study: Eight Forms and a Custom Business Object&lt;/a&gt; already covered. This post takes every behavior apart and looks at who is responsible for each part.&lt;/p&gt;

&lt;p&gt;This post covers:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The three levels of system behavior, and the test for each&lt;/li&gt;
&lt;li&gt;What appears at each level from a single form&lt;/li&gt;
&lt;li&gt;What runs on every call, and what the application paid for each&lt;/li&gt;
&lt;li&gt;What hand written code remains, how it converges into the framework, and where definitions are deliberately not used&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  1. System behavior falls into three levels
&lt;/h2&gt;

&lt;p&gt;Take any behavior in the system apart, and each part lands in one of these three levels:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Level&lt;/th&gt;
&lt;th&gt;Test&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Definition&lt;/td&gt;
&lt;td&gt;The behavior is decided by a definition file, and changing that file changes it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Built into the framework&lt;/td&gt;
&lt;td&gt;This part is the framework's code running, and the application wrote no code for it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Application code&lt;/td&gt;
&lt;td&gt;Someone wrote C# by hand, and switching to another ERP means writing it again&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  One behavior can span several levels
&lt;/h3&gt;

&lt;p&gt;Take the detail amount. Taken apart, it belongs to all three levels:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;This part&lt;/th&gt;
&lt;th&gt;Level&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;The formula for the amount&lt;/td&gt;
&lt;td&gt;Definition, written in the field's &lt;code&gt;ValueExpression&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;How many places to round to&lt;/td&gt;
&lt;td&gt;Definition, looked up in the currency master&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;When to recalculate and when to round&lt;/td&gt;
&lt;td&gt;Built into the framework&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Adding up each row's amount and writing it back to the master&lt;/td&gt;
&lt;td&gt;Application code&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;So a few rows in the next section's table read definition plus built into the framework.&lt;/p&gt;

&lt;h3&gt;
  
  
  The tables record the current state
&lt;/h3&gt;

&lt;p&gt;The tables that follow record the case study as it is now, not necessarily where each thing belongs. Where it belongs is judged with the test from &lt;a href="https://dev.to/jeff377/day-2-an-overview-of-definition-driven-architecture-31hi"&gt;An Overview of Definition-Driven Architecture&lt;/a&gt;: would every ERP in the world do this the same way? What is the same is done by the framework, and what differs is left to the application. The totaling above is an example: it is the same everywhere, yet today it is still in application code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Not all definitions are written by the application
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;TableSchema&lt;/code&gt; files for the &lt;code&gt;common&lt;/code&gt; and &lt;code&gt;log&lt;/code&gt; categories use the framework's defaults as they are. The ones for departments, employees, audit rules and permissions were copied from the framework's defaults the way &lt;a href="https://dev.to/jeff377/day-4-the-purpose-and-classification-of-definition-files-3i6n"&gt;The Purpose and Classification of Definition Files&lt;/a&gt; describes, and employee has two extra fields added.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. What grows out of a single form
&lt;/h2&gt;

&lt;p&gt;The overview post listed what is derived from definitions: most of it is generated, and the other kind is evaluating declared rules and expressions. The behaviors that actually appear along with a form number a few more than that list:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Behavior&lt;/th&gt;
&lt;th&gt;Level&lt;/th&gt;
&lt;th&gt;In the case study&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Fields, types, whether NULL is allowed, uniqueness&lt;/td&gt;
&lt;td&gt;Definition&lt;/td&gt;
&lt;td&gt;Declared completely in one &lt;code&gt;FormSchema&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Creating tables and upgrading them later&lt;/td&gt;
&lt;td&gt;Definition plus built into the framework&lt;/td&gt;
&lt;td&gt;The definition part is one &lt;code&gt;TableSchema&lt;/code&gt; per physical table plus a row in &lt;code&gt;DbCategorySettings&lt;/code&gt;; comparing against the current state and issuing DDL is the framework's&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Layouts for the single record form, the list and the lookup window&lt;/td&gt;
&lt;td&gt;Definition plus built into the framework&lt;/td&gt;
&lt;td&gt;The single record form reads the eight &lt;code&gt;FormLayout&lt;/code&gt; files the application wrote, and the list and lookup window are arranged on the spot from the field set of the &lt;code&gt;FormSchema&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CRUD statements&lt;/td&gt;
&lt;td&gt;Built into the framework&lt;/td&gt;
&lt;td&gt;A query is assembled on the spot from the fields this particular call uses, cross table JOINs included; inserts and updates carry every column of the table&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cross table relationships&lt;/td&gt;
&lt;td&gt;Definition plus built into the framework&lt;/td&gt;
&lt;td&gt;One declaration decides the editor, what is filled back after a pick, the JOIN and the value that gets stored&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The cross layer data container&lt;/td&gt;
&lt;td&gt;Built into the framework&lt;/td&gt;
&lt;td&gt;No entity classes, no DTOs, no mapping configuration&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Validation rules and computed fields&lt;/td&gt;
&lt;td&gt;Definition&lt;/td&gt;
&lt;td&gt;Across the whole application, only the order declares &lt;code&gt;FormRule&lt;/code&gt; entries and one &lt;code&gt;ValueExpression&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Default values for a new row&lt;/td&gt;
&lt;td&gt;Built into the framework&lt;/td&gt;
&lt;td&gt;Filled in by field type, and a date field gets today in the user's time zone&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The actions exposed to callers&lt;/td&gt;
&lt;td&gt;Built into the framework&lt;/td&gt;
&lt;td&gt;All eight forms share the same set, differing only in ProgId&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Menu and program registration&lt;/td&gt;
&lt;td&gt;Definition&lt;/td&gt;
&lt;td&gt;One item in &lt;code&gt;MenuSettings&lt;/code&gt;, one row in &lt;code&gt;ProgramSettings&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Whether definitions agree with one another&lt;/td&gt;
&lt;td&gt;Built into the framework&lt;/td&gt;
&lt;td&gt;Build time diagnostics ship with the package, and the case study's server is checked when it builds&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Everything that adding a form touches falls in the definition level: one &lt;code&gt;FormSchema&lt;/code&gt;, one &lt;code&gt;TableSchema&lt;/code&gt; per physical table, one &lt;code&gt;FormLayout&lt;/code&gt;, a row in &lt;code&gt;DbCategorySettings&lt;/code&gt;, a row in &lt;code&gt;ProgramSettings&lt;/code&gt; and an item in &lt;code&gt;MenuSettings&lt;/code&gt;. The first three are new files, and the last three each gain one line. The table creation code does not change either; it creates or upgrades each table registered in &lt;code&gt;DbCategorySettings&lt;/code&gt; in turn.&lt;/p&gt;

&lt;p&gt;The definition level can go wrong too, and nobody reports it. The detail's &lt;code&gt;discount&lt;/code&gt; declares only a type and no decimal places, so it is created as a column with a decimal scale of zero. SQLite, which the case study uses, keeps decimals as they are, and every discount is zero anyway, so it has never been hit.&lt;/p&gt;

&lt;p&gt;As for the application code level: seven of the eight forms have zero lines. Those seven rows in &lt;code&gt;ProgramSettings&lt;/code&gt; give only a ProgId and a display name, which means the whole flow takes the defaults.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. What runs on every call
&lt;/h2&gt;

&lt;p&gt;The things below happen on every call. All of them are built into the framework, and the last column is what the application paid for each:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;What happens on a call&lt;/th&gt;
&lt;th&gt;Level&lt;/th&gt;
&lt;th&gt;What the application paid&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Endpoint, request parsing and method dispatch&lt;/td&gt;
&lt;td&gt;Built into the framework&lt;/td&gt;
&lt;td&gt;An empty controller subclass&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Serialization and binding of transport types&lt;/td&gt;
&lt;td&gt;Built into the framework&lt;/td&gt;
&lt;td&gt;Zero lines on the server; of the four front ends, all but the desktop one carry an entry in their build files granting an exception&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Compression, encryption and key derivation&lt;/td&gt;
&lt;td&gt;Built into the framework&lt;/td&gt;
&lt;td&gt;A few lines at startup, being a master key seed, the payload options and a flag allowing automatic creation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Access control, the API key check and the three axes of permissions&lt;/td&gt;
&lt;td&gt;Built into the framework&lt;/td&gt;
&lt;td&gt;Nothing declared separately&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Login, the session, and the user's time zone and language&lt;/td&gt;
&lt;td&gt;Built into the framework&lt;/td&gt;
&lt;td&gt;Zero lines of code, with the time zone and language read from the user's seeded row&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The error contract and the messages users can see&lt;/td&gt;
&lt;td&gt;Built into the framework&lt;/td&gt;
&lt;td&gt;The message text is written on &lt;code&gt;FormRule&lt;/code&gt; attributes, and the order's code puts it in the exceptions it throws&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Auditing&lt;/td&gt;
&lt;td&gt;Built into the framework&lt;/td&gt;
&lt;td&gt;A block of switches in &lt;code&gt;SystemSettings&lt;/code&gt; and a few rows in &lt;code&gt;DbCategorySettings&lt;/code&gt;, zero lines of code&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Time zone conversion&lt;/td&gt;
&lt;td&gt;Built into the framework&lt;/td&gt;
&lt;td&gt;Zero lines of code, since the application's own two time fields are both calendar dates&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Numeric rounding&lt;/td&gt;
&lt;td&gt;Built into the framework&lt;/td&gt;
&lt;td&gt;Two fields are marked with &lt;code&gt;NumberKind&lt;/code&gt;, and on top of that the currency master is deployed and the company's base currency is set to USD&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Definition caching and cross-node invalidation&lt;/td&gt;
&lt;td&gt;Built into the framework&lt;/td&gt;
&lt;td&gt;A row in &lt;code&gt;DbCategorySettings&lt;/code&gt; for the notification table&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;There is also a group of mechanisms that the case study uses only lightly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Not used: business plugins, permission models, the unit of measure master, and the maintenance screen for plugin settings&lt;/li&gt;
&lt;li&gt;Connected, but with only one of each: the customization layer, multiple languages and the currency master. The case study is still one company, one customization and one currency, and only the order has a translation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They pay off only once a second one appears: a second company, a second language, a second role, a second currency. So this group cannot be counted as what the framework saves the application. It is the more expensive part of this design, and until the second one appears, the cost paid out looks like pure waste.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. What hand written code remains
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The order's five things
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/jeff377/bee-northwind-avalonia/blob/main/Bee.Northwind.Server/BusinessObjects/OrderBO.cs" rel="noopener noreferrer"&gt;&lt;code&gt;OrderBO&lt;/code&gt;&lt;/a&gt; overrides only two of the steps the framework cuts out: getting a new record, and the step before saving. The latter looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;DoBeforeSave&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SaveContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;base&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DoBeforeSave&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// FormRule and ValueExpression finish running here first&lt;/span&gt;

    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;dataSet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DataSet&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;master&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OrderDataSet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;MasterRow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dataSet&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;OrderDataSet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;RequireAtLeastOneDetail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dataSet&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;EnforceStatusRules&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dataSet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;master&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;AssignOrderNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;master&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;OrderDataSet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ComputeTotal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dataSet&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is four things, and the fifth is in the step that gets a new record:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;This one&lt;/th&gt;
&lt;th&gt;Why it is still in code&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Whether a status transition is legal&lt;/td&gt;
&lt;td&gt;A genuine business judgment. Which states can be jumped to, and whether details can still change after confirmation, is set differently at every company&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Generating the order number&lt;/td&gt;
&lt;td&gt;Standard behavior, which the framework should absorb and has not yet&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;At least one detail row&lt;/td&gt;
&lt;td&gt;Cross row aggregation. A rule model that evaluates row by row can see the fields of one row, but not how many rows this document has&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Summing the details back into the master&lt;/td&gt;
&lt;td&gt;The same. The framework already decides when each row is rounded, and adding up the rounded values is still outside it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The order date defaulting to today&lt;/td&gt;
&lt;td&gt;The framework already does this, taking today in the user's time zone; this line takes the server's today&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Converging from the application into the framework
&lt;/h3&gt;

&lt;p&gt;The logic and customization specific to a form converge toward the framework along the same path:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Step&lt;/th&gt;
&lt;th&gt;What happens&lt;/th&gt;
&lt;th&gt;In the case study&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;The application writes it first&lt;/td&gt;
&lt;td&gt;The framework has no matching mechanism yet, so it goes into an overridden business object step or a business plugin&lt;/td&gt;
&lt;td&gt;The detail amount, the required field checks and the five things above all started out in &lt;code&gt;OrderBO&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Deciding whether to absorb it&lt;/td&gt;
&lt;td&gt;The test from the overview post, whether every company does it the same way&lt;/td&gt;
&lt;td&gt;Status transitions differ at every company, so they stay in the application&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The framework takes over&lt;/td&gt;
&lt;td&gt;What can be declared goes into definitions, and what needs no declaration is built into the framework&lt;/td&gt;
&lt;td&gt;The detail amount became a &lt;code&gt;ValueExpression&lt;/code&gt;, the required field checks became &lt;code&gt;FormRule&lt;/code&gt; entries, and defaulting the order date to today is built into the framework; the order number and cross row totals have no matching mechanism yet&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The application deletes the old code&lt;/td&gt;
&lt;td&gt;The part that has been taken over is removed from the business object&lt;/td&gt;
&lt;td&gt;The amount and the required field checks are already deleted, and the order date line is still there&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  The application shell
&lt;/h3&gt;

&lt;p&gt;On the server, apart from the order, all the code describes this deployment and has nothing to do with forms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple databases: the code registers only database drivers, and the case study uses SQLite. The common, company and log categories each have a connection in &lt;code&gt;DatabaseSettings&lt;/code&gt;, all currently pointing at the same file. Splitting them into different databases means changing only this settings file, switching to another database also means registering its driver, and no form definition has to change.&lt;/li&gt;
&lt;li&gt;The rest: host wiring, cross origin settings for development, the empty controller subclass, the demo account, and creating tables and loading sample data.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The front end side
&lt;/h3&gt;

&lt;p&gt;The front ends have nothing to do with forms either:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple front ends: desktop, browser, iOS and Android share one UI project, each with only a few lines of startup code, declaring remote only, the endpoint, and where the key is stored.&lt;/li&gt;
&lt;li&gt;Calling the back end through the Connector: every action a form uses has a matching method. A call carries the API key and the token obtained at login, and its content is compressed and then encrypted; the browser one cannot get the key used for encryption, so it goes only as far as compression.&lt;/li&gt;
&lt;li&gt;No file in the UI project is written for a particular form: the container that hosts a form takes a ProgId, and the navigation list grows out of &lt;code&gt;MenuSettings&lt;/code&gt;. Going from eight forms to eighty, this side does not change.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Where definitions are deliberately not used
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Genuine business judgment: in the case study, only the status transition&lt;/li&gt;
&lt;li&gt;SQL for reports and batch jobs: multi table JOINs, aggregation, dynamic conditions and performance tuning. Forcing definitions onto them only makes things more complicated, and what the framework offers is a route for writing your own repository&lt;/li&gt;
&lt;li&gt;Deep screen customization: &lt;code&gt;FormLayout&lt;/code&gt; describes how fields are arranged, not what a dashboard should look like&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are boundaries, not backlog items. The backlog is what the framework has not finished, such as the order number and cross row totals.&lt;/p&gt;




&lt;h2&gt;
  
  
  In closing
&lt;/h2&gt;

&lt;p&gt;Seven of the eight forms have zero lines, and neither the application shell nor the front ends depend on how many forms there are.&lt;/p&gt;

&lt;p&gt;The three levels relate to the number of forms in different ways. The definition level grows linearly, and it should, because every form has its own fields, its own relationships and its own idea of what to show. The level built into the framework is constant, since what one call has to do does not grow as forms are added. The application code level follows rules, not forms: every rule the framework has not taken over yet has to be written once in every form that uses it, and what should finally remain is only the few rules that differ from company to company.&lt;/p&gt;

&lt;p&gt;Of the order's five things, four can leave the application sooner or later: the order number once the framework absorbs it, at least one detail row and the detail total once definitions can compute across rows, and the order date line by simply being deleted. The status transition will always stay, because every company differs. The case study post closed on writing code only where a judgment is genuinely required, and in this case study that means the status transition.&lt;/p&gt;

&lt;p&gt;The next post, &lt;a href="https://dev.to/jeff377/day-30-three-depths-of-development-and-the-big-picture-of-the-framework-n5n"&gt;Three Depths of Development and the Big Picture of the Framework&lt;/a&gt;, is the last one, gathering the thirty posts into a single big picture.&lt;/p&gt;




&lt;p&gt;📚 Part of the thirty-post series &lt;a href="https://dev.to/jeff377/erp-architects-notes-designing-a-definition-driven-framework-series-index-4ln7"&gt;ERP Architect's Notes: Designing a Definition-Driven Framework&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📘 Original article on HackMD, in Traditional Chinese:&lt;br&gt;
👉 &lt;a href="https://hackmd.io/@jeff377/day-29-case-reconciliation" rel="noopener noreferrer"&gt;https://hackmd.io/@jeff377/day-29-case-reconciliation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📢 You are welcome to republish this, please credit the source.&lt;br&gt;
📬 Follow for more engineering notes:&lt;br&gt;
&lt;a href="https://www.facebook.com/profile.php?id=61574839666569" rel="noopener noreferrer"&gt;Facebook&lt;/a&gt; ｜ &lt;a href="https://hackmd.io/@jeff377" rel="noopener noreferrer"&gt;HackMD&lt;/a&gt; ｜ &lt;a href="https://github.com/jeff377" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; ｜ &lt;a href="https://www.nuget.org/profiles/jeff377" rel="noopener noreferrer"&gt;NuGet&lt;/a&gt;&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>dotnet</category>
      <category>erp</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Day 28: Time Zone Conversion of DateTime</title>
      <dc:creator>Jeff Tsai</dc:creator>
      <pubDate>Sat, 21 Nov 2026 01:04:48 +0000</pubDate>
      <link>https://dev.to/jeff377/day-28-time-zone-conversion-of-datetime-3ona</link>
      <guid>https://dev.to/jeff377/day-28-time-zone-conversion-of-datetime-3ona</guid>
      <description>&lt;p&gt;The previous post, &lt;a href="https://dev.to/jeff377/day-27-the-semantic-boundaries-between-date-datetime-and-time-24i8"&gt;The Semantic Boundaries Between Date, DateTime and Time&lt;/a&gt;, ended by saying that this one would take up the only type that ever gets changed.&lt;/p&gt;

&lt;p&gt;When a time value is written to the database, the column does not record which time zone it is based on. The same code run on a machine in Taipei and on a host whose time zone is set to UTC writes values 8 hours apart, and nothing in the table shows the difference. It is discovered only when the first user in another time zone shows up, and by then the historical data has already been written in bulk.&lt;/p&gt;

&lt;p&gt;The framework uses a single baseline in the database, and does the conversion in a single place.&lt;/p&gt;

&lt;p&gt;This post covers:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The database stores UTC, in columns without a time zone&lt;/li&gt;
&lt;li&gt;Conversion is concentrated in the Connector on the client, and the server decides the points in time that get saved&lt;/li&gt;
&lt;li&gt;Which values get changed, and what the decision is based on&lt;/li&gt;
&lt;li&gt;An undeclared calendar date column, and clients without a Connector&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  1. Which time zone a stored value follows
&lt;/h2&gt;

&lt;p&gt;Storing local time in the database shows no problem in a deployment with a single time zone. But "local" may mean the time zone of the application host or that of the database host. The two usually agree, and when they do not, nothing gives it away.&lt;/p&gt;

&lt;p&gt;The framework stores every point in time as UTC in a column without a time zone, which is the type listed for &lt;code&gt;DateTime&lt;/code&gt; in the previous post's comparison of the five databases. PostgreSQL's &lt;code&gt;timestamptz&lt;/code&gt; converts implicitly according to the server's time zone setting, so the framework does not use it; time zone conversion is not left to the database.&lt;/p&gt;

&lt;p&gt;When a SQL statement does not give a value for a column, what gets written is the column's default value, for example when an INSERT written by the caller leaves the column out, or when existing rows are backfilled after a column is added. So on every database the DEFAULT of a point in time column is also written in a form that returns UTC:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Provider&lt;/th&gt;
&lt;th&gt;DEFAULT of a point in time column&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;SQL Server&lt;/td&gt;
&lt;td&gt;&lt;code&gt;getutcdate()&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PostgreSQL&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(NOW() AT TIME ZONE 'UTC')&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;MySQL&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(UTC_TIMESTAMP(6))&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Oracle&lt;/td&gt;
&lt;td&gt;&lt;code&gt;SYS_EXTRACT_UTC(SYSTIMESTAMP)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SQLite&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;CURRENT_TIMESTAMP&lt;/code&gt; (already UTC)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The outer parentheses on the MySQL line are required syntax from 8.0.13 on: apart from &lt;code&gt;CURRENT_TIMESTAMP&lt;/code&gt;, any expression used as a default value has to be wrapped in parentheses.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Conversion happens in only one place
&lt;/h2&gt;

&lt;p&gt;If conversion is spread around, done once on the desktop, once in the browser and once more in report export, and one of those places converts once too often or not at all, the symptom is a wrong time on screen, not an exception.&lt;/p&gt;

&lt;p&gt;The framework concentrates conversion in the Connector on the client. The server does no conversion and reads and writes UTC directly. The Connector converts different things in each direction:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Direction&lt;/th&gt;
&lt;th&gt;What is converted&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;When a response arrives&lt;/td&gt;
&lt;td&gt;Points in time, from UTC to the user's time zone&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;When a request goes out&lt;/td&gt;
&lt;td&gt;Only query conditions, from the user's time zone to UTC; the &lt;code&gt;DataSet&lt;/code&gt; being saved is left untouched&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The server does not accept the points in time sent up with a save. A new row is filled with the UTC time of the save, and a field with a default value expression is recalculated by the server; a modified or deleted row gets its original value read back from the database. So reading a record and saving it back leaves its time values unchanged. When the time zone is set wrong, the values stored in the database are not affected; what is wrong is the display on screen and the query conditions.&lt;/p&gt;

&lt;p&gt;Another approach would be for the server to convert the points in time it receives back to UTC, using the time zone recorded on the session, and then save them. The framework did not take it. That would give the time zone two sources, the client for display and the server for writing back; when the two disagree, the user sees 09:00, leaves it alone, and a different point in time gets saved. For a form to edit a &lt;code&gt;DateTime&lt;/code&gt; field directly, the business object has to convert it to UTC on the server itself.&lt;/p&gt;

&lt;p&gt;The time zone used for conversion is the one on the session. It is read from the user's row at login, falls back to the deployment level default only when that is empty, and is not taken from the device: when a user travels with a laptop, the time shown for the same record, and the baseline for query conditions, should not change with the device.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Which values get changed
&lt;/h2&gt;

&lt;p&gt;All the Connector has is a payload, with no &lt;code&gt;FormSchema&lt;/code&gt; to look up, so the decision rests on the marker on the column from the previous post, which travels along with the data.&lt;/p&gt;

&lt;p&gt;What is decided is whether a value is a point in time. A time of day is a string in the &lt;code&gt;DataSet&lt;/code&gt;, so it is outside the decision.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Where the value sits&lt;/th&gt;
&lt;th&gt;What expresses its meaning&lt;/th&gt;
&lt;th&gt;Converted?&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;A cell of a &lt;code&gt;DataSet&lt;/code&gt; in a response&lt;/td&gt;
&lt;td&gt;The marker on the column&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;DateTime&lt;/code&gt; is converted, &lt;code&gt;Date&lt;/code&gt; is not&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The value of a query condition in a request&lt;/td&gt;
&lt;td&gt;The value's own CLR type&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;DateTime&lt;/code&gt; is converted, &lt;code&gt;DateOnly&lt;/code&gt; is not&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The parameter bag carried by every request&lt;/td&gt;
&lt;td&gt;Nothing&lt;/td&gt;
&lt;td&gt;Not converted&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A query condition has no column to hang a marker on, so its meaning comes from the value's own type (these two lines build a &lt;a href="https://github.com/jeff377/bee-library/blob/main/src/Bee.Definition/Filters/FilterCondition.cs" rel="noopener noreferrer"&gt;&lt;code&gt;FilterCondition&lt;/code&gt;&lt;/a&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;FilterCondition&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"invoice_date"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;someDateOnly&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// calendar date, left as is&lt;/span&gt;
&lt;span class="nc"&gt;FilterCondition&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"created_at"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;someDateTime&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// point in time, converted to UTC when sent&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Passing a &lt;code&gt;DateTime&lt;/code&gt; where a calendar date belongs raises no error; the condition value simply shifts along with it, and the rows found are wrong.&lt;/p&gt;

&lt;p&gt;The values in the parameter bag are untyped, so a point in time cannot be told from a calendar date there. Converting them all would corrupt UTC values the caller put in on purpose, so they are not converted.&lt;/p&gt;

&lt;p&gt;The message types to convert are listed one by one, and a newly added message type that carries data is not included automatically. So a separate test finds every message type that carries a &lt;code&gt;DataSet&lt;/code&gt;, a &lt;code&gt;DataTable&lt;/code&gt; or query conditions, runs each one, and checks that responses were converted to the user's time zone and query conditions to UTC; a type that was missed makes this test fail.&lt;/p&gt;

&lt;h3&gt;
  
  
  Whose today it is
&lt;/h3&gt;

&lt;p&gt;When a row is added, a date field defaults to today, and this today has a time zone problem too. If it takes the server's today and the server's time zone is set to UTC, a user in Taipei who opens a document before eight in the morning gets the previous day as the default date.&lt;/p&gt;

&lt;p&gt;The framework works out today in the user's time zone. The default value of a date field on a new row and today inside an expression use the same code, and the caller passes in the time zone on the session. This code runs on both the server and the client, and the two sides have to arrive at the same today; otherwise the same document has different dates on each side. Calendar dates are not converted, so no mechanism would make up that difference.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. How it breaks when something is missed
&lt;/h2&gt;

&lt;p&gt;When your own SQL leaves out the declaration of a calendar date column, that column is treated as a point in time and converted like any other when the response arrives. Measured with a column holding 2026-01-01 in the database:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Who reads it&lt;/th&gt;
&lt;th&gt;Value after conversion&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;A user in Taipei&lt;/td&gt;
&lt;td&gt;2026-01-01 08:00&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A user in New York&lt;/td&gt;
&lt;td&gt;2025-12-31 19:00&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Taipei is east of UTC, so conversion adds 8 hours: the date stays the same and only hours and minutes appear. New York is west of UTC, so conversion subtracts, and the date falls back to the day before. The same mistake therefore looks like nothing more than extra hours and minutes in a deployment with only users in Taiwan, and becomes a wrong date only once users west of UTC show up.&lt;/p&gt;

&lt;p&gt;A client without a Connector has to convert on its own: points in time it receives go to the user's time zone, and query conditions go to UTC. Column types arrive with the payload, and the &lt;code&gt;type&lt;/code&gt; of a calendar date column is &lt;code&gt;Date&lt;/code&gt;, so it can tell which columns to leave alone.&lt;/p&gt;




&lt;h2&gt;
  
  
  Back to Northwind
&lt;/h2&gt;

&lt;p&gt;The case study is a single time zone deployment, and the demo account's row has &lt;code&gt;Asia/Taipei&lt;/code&gt; as its time zone.&lt;/p&gt;

&lt;p&gt;The application's own two time fields are both calendar dates, so the Connector finds no field to convert on the application's forms. The only value that conversion touches and that is also visible on screen is the creation time on the audit rule form: the list does not show this column, the single record form does, read only and showing just the date. When a rule is added from the screen, the server fills this column in at save.&lt;/p&gt;




&lt;h2&gt;
  
  
  In closing
&lt;/h2&gt;

&lt;p&gt;Points in time in the database are all stored as UTC, and column default values are written in forms that return UTC as well. Conversion is done only in the Connector on the client: when a response arrives, it converts points in time to the time zone on the session according to the marker on the column; when sending, it converts only query conditions to UTC. The server does not accept points in time sent up with a save: a new row gets the UTC time of the save, and a modified row keeps the value from the database.&lt;/p&gt;

&lt;p&gt;When your own SQL leaves a calendar date column undeclared, users in Taipei see only extra hours and minutes, and only users west of UTC see the day before. So calendar date columns have to be declared by whoever writes the SQL. Whether conversion covers every message type is checked by a test.&lt;/p&gt;

&lt;p&gt;The three posts in this chapter ask the same question: what a value stands for cannot be seen from the value itself. The same decimal number can be a unit price or an amount, the same time value can be a calendar date or a point in time, and a point in time stored as UTC does not show which time zone it should be displayed in. The three posts also take the same approach: declare the meaning next to the value, and have every path that touches the value follow that declaration. The cost is the same too: when a declaration is missing, nobody reports an error, because the values left behind are still valid.&lt;/p&gt;

&lt;p&gt;The next post, &lt;a href="https://dev.to/jeff377/day-29-which-part-definitions-the-framework-and-application-code-each-own-2a5m"&gt;Which Part Definitions, the Framework and Application Code Each Own&lt;/a&gt;, goes back to the case study and puts these mechanisms in their places one by one: which of them sit in the definitions, which sit in the framework, and which the application has to write itself.&lt;/p&gt;




&lt;p&gt;📚 Part of the thirty-post series &lt;a href="https://dev.to/jeff377/erp-architects-notes-designing-a-definition-driven-framework-series-index-4ln7"&gt;ERP Architect's Notes: Designing a Definition-Driven Framework&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📘 Original article on HackMD, in Traditional Chinese:&lt;br&gt;
👉 &lt;a href="https://hackmd.io/@jeff377/day-28-timezone-conversion" rel="noopener noreferrer"&gt;https://hackmd.io/@jeff377/day-28-timezone-conversion&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📢 You are welcome to republish this, please credit the source.&lt;br&gt;
📬 Follow for more engineering notes:&lt;br&gt;
&lt;a href="https://www.facebook.com/profile.php?id=61574839666569" rel="noopener noreferrer"&gt;Facebook&lt;/a&gt; ｜ &lt;a href="https://hackmd.io/@jeff377" rel="noopener noreferrer"&gt;HackMD&lt;/a&gt; ｜ &lt;a href="https://github.com/jeff377" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; ｜ &lt;a href="https://www.nuget.org/profiles/jeff377" rel="noopener noreferrer"&gt;NuGet&lt;/a&gt;&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>dotnet</category>
      <category>erp</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Day 27: The Semantic Boundaries Between Date, DateTime and Time</title>
      <dc:creator>Jeff Tsai</dc:creator>
      <pubDate>Fri, 20 Nov 2026 03:06:33 +0000</pubDate>
      <link>https://dev.to/jeff377/day-27-the-semantic-boundaries-between-date-datetime-and-time-24i8</link>
      <guid>https://dev.to/jeff377/day-27-the-semantic-boundaries-between-date-datetime-and-time-24i8</guid>
      <description>&lt;p&gt;The order date on a document, the event time on an audit record and the start time on a shift schedule answer different questions. Once they are in a &lt;code&gt;DataSet&lt;/code&gt;, though, the first two are both &lt;code&gt;DateTime&lt;/code&gt; columns and the third is a string column, and the column type alone cannot tell them apart. So what a column is has to be stated separately, by the definition layer.&lt;/p&gt;

&lt;p&gt;The screen relies on it to pick an editing control, the database relies on it to pick a native type, and when a value travels between client and server, it decides whether a time zone conversion happens (that is the subject of &lt;a href="https://dev.to/jeff377/day-28-time-zone-conversion-of-datetime-3ona"&gt;Time Zone Conversion of DateTime&lt;/a&gt;). None of these can be decided from the fact that it is a &lt;code&gt;DateTime&lt;/code&gt; alone.&lt;/p&gt;

&lt;p&gt;This post covers:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How to tell the three kinds of time apart&lt;/li&gt;
&lt;li&gt;Calendar dates and points in time are both &lt;code&gt;DateTime&lt;/code&gt; in a &lt;code&gt;DataSet&lt;/code&gt;, so what keeps their meaning&lt;/li&gt;
&lt;li&gt;Why a time of day does not use the database's native time type&lt;/li&gt;
&lt;li&gt;Empty values and values out of range&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  1. Whether the value needs to know which day it is
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;08:00&lt;/code&gt; on a shift schedule and &lt;code&gt;08:00&lt;/code&gt; on a clock-in record look the same but mean different things: the first applies every day, while the second stands only for the one time it happened.&lt;/p&gt;

&lt;p&gt;The framework splits time fields into three kinds by meaning, and the test is a single question: does this value need to know which day it is?&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Kind&lt;/th&gt;
&lt;th&gt;Question it answers&lt;/th&gt;
&lt;th&gt;&lt;code&gt;FieldDbType&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;Type in code&lt;/th&gt;
&lt;th&gt;Time zone conversion&lt;/th&gt;
&lt;th&gt;Examples&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Calendar date&lt;/td&gt;
&lt;td&gt;Which day&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Date&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;DateOnly&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Not converted&lt;/td&gt;
&lt;td&gt;A birthday, an invoice date, an accounting period&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Point in time&lt;/td&gt;
&lt;td&gt;Which day and what time&lt;/td&gt;
&lt;td&gt;&lt;code&gt;DateTime&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;DateTime&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Converted&lt;/td&gt;
&lt;td&gt;A creation time, a login timestamp, a clock-in record&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Time of day&lt;/td&gt;
&lt;td&gt;What time within a day&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Time&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;TimeOnly&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Not converted&lt;/td&gt;
&lt;td&gt;The start and end of a shift, business hours, a reminder time&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;They are declared like any other field, one line per field:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;FormField&lt;/span&gt; &lt;span class="na"&gt;FieldName&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"hire_date"&lt;/span&gt;  &lt;span class="na"&gt;Caption&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Hire Date"&lt;/span&gt;  &lt;span class="na"&gt;DbType&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Date"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;FormField&lt;/span&gt; &lt;span class="na"&gt;FieldName&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"created_at"&lt;/span&gt; &lt;span class="na"&gt;Caption&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Created At"&lt;/span&gt; &lt;span class="na"&gt;DbType&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"DateTime"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;FormField&lt;/span&gt; &lt;span class="na"&gt;FieldName&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"work_start"&lt;/span&gt; &lt;span class="na"&gt;Caption&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Work Start"&lt;/span&gt; &lt;span class="na"&gt;DbType&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Time"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;DbType&lt;/code&gt; also decides the default control on screen, which is where &lt;a href="https://dev.to/jeff377/day-7-formlayout-and-screen-generation-for-multiple-front-ends-1edf"&gt;FormLayout and Screen Generation for Multiple Front Ends&lt;/a&gt; mapped a field's type to a control: calendar dates and points in time get a date input control, and a time of day gets a fixed width time input control. If a field names its own control, that one is used.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. The two kinds that are both &lt;code&gt;DateTime&lt;/code&gt; in a &lt;code&gt;DataSet&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Once in a &lt;code&gt;DataSet&lt;/code&gt;, calendar dates and points in time are both &lt;code&gt;DateTime&lt;/code&gt; columns, and a &lt;code&gt;DateOnly&lt;/code&gt; value cannot be written into one. The framework handles them like this:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Calendar date &lt;code&gt;Date&lt;/code&gt;
&lt;/th&gt;
&lt;th&gt;Point in time &lt;code&gt;DateTime&lt;/code&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Column type in the &lt;code&gt;DataSet&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;DateTime&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;DateTime&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Marker on the column&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Date&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;DateTime&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;When read&lt;/td&gt;
&lt;td&gt;Read as a &lt;code&gt;DateOnly&lt;/code&gt; with &lt;code&gt;CDateOnly&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Read as a &lt;code&gt;DateTime&lt;/code&gt; with &lt;code&gt;CDateTime&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;With the same type, the two can only be told apart by the marker on the column. Code that has to decide reads the marker first and looks at the CLR type only when there is no marker, and a &lt;code&gt;DateTime&lt;/code&gt; is always taken to be a point in time.&lt;/p&gt;

&lt;p&gt;The marker governs how a value is read, not how it is written. Code that writes &lt;code&gt;row["hire_date"] = DateTime.Now&lt;/code&gt; directly leaves the hours, minutes and seconds in the &lt;code&gt;DataSet&lt;/code&gt; along with the date; if you want only the date, take &lt;code&gt;.Date&lt;/code&gt; yourself.&lt;/p&gt;

&lt;p&gt;Where the marker comes from in each situation, and whether it is kept:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Situation&lt;/th&gt;
&lt;th&gt;Marker&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;A column created from the definition&lt;/td&gt;
&lt;td&gt;Attached when the column is created&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A table read back from the database&lt;/td&gt;
&lt;td&gt;The framework adds it from the &lt;code&gt;FormSchema&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Your own SQL&lt;/td&gt;
&lt;td&gt;There is no definition to look up, so whoever writes the SQL lists the calendar date columns on the query, or marks the table afterwards; a misspelled column name throws an exception&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sent over the API as JSON or MessagePack&lt;/td&gt;
&lt;td&gt;The other end restores it&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A calendar date column without a marker is treated as a point in time, and no error is raised, because its value is a perfectly valid point in time. Where that goes wrong is for the next post.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Why a time of day does not use the database's native type
&lt;/h2&gt;

&lt;p&gt;How a time of day is held in each place:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Where&lt;/th&gt;
&lt;th&gt;What it looks like&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;In the database&lt;/td&gt;
&lt;td&gt;A fixed width string of five characters, such as &lt;code&gt;"08:30"&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;In the &lt;code&gt;DataSet&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;The same string, &lt;code&gt;"08:30"&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;When code reads it&lt;/td&gt;
&lt;td&gt;Converted to a &lt;code&gt;TimeOnly&lt;/code&gt; with &lt;code&gt;CTimeOnly&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;When it is written back to the &lt;code&gt;DataSet&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;The time of day control on a single record form pads &lt;code&gt;"8:30"&lt;/code&gt; to &lt;code&gt;"08:30"&lt;/code&gt;; other ways of writing it may not, and code that writes it has to convert with &lt;code&gt;CTimeString&lt;/code&gt; itself&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A &lt;code&gt;TimeOnly&lt;/code&gt; cannot go into a &lt;code&gt;DataSet&lt;/code&gt; directly. The time of day column is a string, so setting a &lt;code&gt;TimeOnly&lt;/code&gt; on it does not throw; the value is stored as text in the current culture (under zh-TW that is &lt;code&gt;上午8:30&lt;/code&gt;, 8:30 AM written in Chinese). Change the column type to &lt;code&gt;TimeOnly&lt;/code&gt; instead, and JSON and MessagePack do not recognize it when they send the table. So a time of day column is formatted as an &lt;code&gt;"HH:mm"&lt;/code&gt; string, and converted to a &lt;code&gt;TimeOnly&lt;/code&gt; when it is read.&lt;/p&gt;

&lt;p&gt;Code uses &lt;code&gt;TimeOnly&lt;/code&gt; because what it represents is exactly a time within a day: two of them can be compared directly, the hour and the minute can be taken out, and subtracting one from another gives a duration. The end of a shift minus its start is the hours worked, and that holds even for a night shift that crosses midnight. A &lt;code&gt;TimeSpan&lt;/code&gt; represents a length of time, which can exceed a day and can be negative; store a start time in one, and the type cannot stop a value longer than a day.&lt;/p&gt;

&lt;p&gt;The database does not use a native time type because the five databases disagree. &lt;code&gt;TIME&lt;/code&gt; in SQL Server and PostgreSQL is a time within a day, &lt;code&gt;TIME&lt;/code&gt; in MySQL is a length of time that ranges up to plus or minus 838 hours, and Oracle has no &lt;code&gt;TIME&lt;/code&gt; at all; on top of that, the drivers for the first three all read it back as a &lt;code&gt;TimeSpan&lt;/code&gt;. So all five use a string:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;Date&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;DateTime&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;Time&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;SQL Server&lt;/td&gt;
&lt;td&gt;&lt;code&gt;date&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;datetime2(7)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;nchar(5)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PostgreSQL&lt;/td&gt;
&lt;td&gt;&lt;code&gt;date&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;timestamp&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;char(5)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;MySQL&lt;/td&gt;
&lt;td&gt;&lt;code&gt;DATE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;DATETIME(6)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;CHAR(5)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SQLite&lt;/td&gt;
&lt;td&gt;&lt;code&gt;DATE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;DATETIME&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;VARCHAR(5)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Oracle&lt;/td&gt;
&lt;td&gt;&lt;code&gt;DATE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;TIMESTAMP(6)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;VARCHAR2(5)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The advantage of a string is that a plain SELECT is readable as it is, and when every row is zero padded, lexicographic order is time order, so &lt;code&gt;WHERE work_start BETWEEN '08:00' AND '17:00'&lt;/code&gt; simply works. An unpadded &lt;code&gt;"8:30"&lt;/code&gt; sorts after &lt;code&gt;"17:00"&lt;/code&gt;, and that condition does not pick it up. The cost is that the database reports this column only as a string of length 5, so when the framework compares structures it first has to treat the time of day in the definition as a string of length 5 as well; otherwise every upgrade would find a difference.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Empty values and values out of range
&lt;/h2&gt;

&lt;p&gt;The reading methods for the three kinds are written the same way: the method name is the return type, and the single parameter overload always returns a nullable (all three are in &lt;a href="https://github.com/jeff377/bee-library/blob/main/src/Bee.Base/ValueUtilities.Temporal.cs" rel="noopener noreferrer"&gt;&lt;code&gt;ValueUtilities.Temporal&lt;/code&gt;&lt;/a&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;DateOnly&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;day&lt;/span&gt;     &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ValueUtilities&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CDateOnly&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"hire_date"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;DateTime&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;instant&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ValueUtilities&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CDateTime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"created_at"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;TimeOnly&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ValueUtilities&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CTimeOnly&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"work_start"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;They return a nullable because a time of day has no value that can stand for not filled in. &lt;code&gt;default(TimeOnly)&lt;/code&gt; is &lt;code&gt;00:00&lt;/code&gt;, and &lt;code&gt;00:00&lt;/code&gt; can itself be a start time; use it to mean empty, and a field nobody filled in is taken as &lt;code&gt;00:00&lt;/code&gt;, again with no error. A date could use &lt;code&gt;0001-01-01&lt;/code&gt; to mean empty, but that value is easily printed on a report and taken as data, so the two date methods return a nullable as well. When you need a non null value, use the two parameter overload and supply the substitute yourself. Storage works the same way: a time of day that was not filled in is stored as an empty string, not &lt;code&gt;"00:00"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Reading also checks format and range: &lt;code&gt;CTimeOnly("8:30")&lt;/code&gt; is accepted and taken as &lt;code&gt;08:30&lt;/code&gt;, while &lt;code&gt;"25:00"&lt;/code&gt; and a &lt;code&gt;TimeSpan&lt;/code&gt; of 30 hours both come back null. A string column and a &lt;code&gt;TimeSpan&lt;/code&gt; can both hold a value longer than a day, so it is the reading method that keeps the range in check.&lt;/p&gt;




&lt;h2&gt;
  
  
  Back to Northwind
&lt;/h2&gt;

&lt;p&gt;The case study's &lt;code&gt;FormSchema&lt;/code&gt; definitions have only two time fields of the application's own, and both are the same kind: &lt;code&gt;order_date&lt;/code&gt; on orders and &lt;code&gt;hire_date&lt;/code&gt; on employees, each declared with &lt;code&gt;DbType="Date"&lt;/code&gt; in its definition.&lt;/p&gt;

&lt;p&gt;The case study's &lt;code&gt;TableSchema&lt;/code&gt; files do contain a batch of point in time columns, and not one of them is on the application's own tables: the write and expiry times of sessions, the expiry time of API keys, the update times of definitions and of cache notifications, the event time on each of the audit tables, and the write times on several other framework tables. All of it is data the framework records for itself.&lt;/p&gt;

&lt;p&gt;The reason is that the two kinds of data serve different purposes. An order date is used for billing, for dividing into periods and for month-end reconciliation, and what time of day the order was placed is of no use to the business. When a session expires, or in which second a login failed, means nothing without the hours, minutes and seconds. By the test in the first section, the application's tables want calendar dates and the framework's tables want points in time.&lt;/p&gt;




&lt;h2&gt;
  
  
  In closing
&lt;/h2&gt;

&lt;p&gt;Calendar dates and points in time are both &lt;code&gt;DateTime&lt;/code&gt; in a &lt;code&gt;DataSet&lt;/code&gt; and are told apart by the marker on the column; a column without one is treated as a point in time, and no error is raised. Tables the framework creates or reads back are marked, while with SQL you write yourself, you declare the calendar date columns yourself.&lt;/p&gt;

&lt;p&gt;A time of day is a fixed width string in both the database and the &lt;code&gt;DataSet&lt;/code&gt;, so it cannot be mistaken for a point in time, and code converts it to a &lt;code&gt;TimeOnly&lt;/code&gt; when reading it. The cost is that the database knows it only as a string of length 5, which the framework has to handle separately when comparing structures.&lt;/p&gt;

&lt;p&gt;The next post takes up the only one of the three that ever gets changed: time zone conversion of points in time. Calendar dates and times of day are not converted, and converting them only produces errors, such as a birthday slipping back to the day before or an eight o'clock start turning into four in the afternoon. All of it depends on first being able to tell which kind a column is.&lt;/p&gt;




&lt;p&gt;📚 Part of the thirty-post series &lt;a href="https://dev.to/jeff377/erp-architects-notes-designing-a-definition-driven-framework-series-index-4ln7"&gt;ERP Architect's Notes: Designing a Definition-Driven Framework&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📘 Original article on HackMD, in Traditional Chinese:&lt;br&gt;
👉 &lt;a href="https://hackmd.io/@jeff377/day-27-temporal-semantics" rel="noopener noreferrer"&gt;https://hackmd.io/@jeff377/day-27-temporal-semantics&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📢 You are welcome to republish this, please credit the source.&lt;br&gt;
📬 Follow for more engineering notes:&lt;br&gt;
&lt;a href="https://www.facebook.com/profile.php?id=61574839666569" rel="noopener noreferrer"&gt;Facebook&lt;/a&gt; ｜ &lt;a href="https://hackmd.io/@jeff377" rel="noopener noreferrer"&gt;HackMD&lt;/a&gt; ｜ &lt;a href="https://github.com/jeff377" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; ｜ &lt;a href="https://www.nuget.org/profiles/jeff377" rel="noopener noreferrer"&gt;NuGet&lt;/a&gt;&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>dotnet</category>
      <category>erp</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Day 26: Numeric Semantics, Multiple Currencies and Units of Measure</title>
      <dc:creator>Jeff Tsai</dc:creator>
      <pubDate>Thu, 19 Nov 2026 01:48:10 +0000</pubDate>
      <link>https://dev.to/jeff377/day-26-numeric-semantics-multiple-currencies-and-units-of-measure-2n2p</link>
      <guid>https://dev.to/jeff377/day-26-numeric-semantics-multiple-currencies-and-units-of-measure-2n2p</guid>
      <description>&lt;p&gt;The chapters so far asked whether a value gets in, whether it gets across, and whether it gets recorded. This post asks a different question: what the value that got in actually stands for.&lt;/p&gt;

&lt;p&gt;The quantity, unit price, amount, discount rate and exchange rate on a document look almost identical in the &lt;code&gt;TableSchema&lt;/code&gt; definition, each one a decimal number with a precision. The real difference lies in three places: how many decimal places to show, whether to round when the value is written, and who has the say over the number of places. Most systems hand these three to the table's DDL, the format strings on screen, and the &lt;code&gt;Math.Round&lt;/code&gt; calls in the business logic, and each of the three gets changed on its own. To be maintainable, the three have to be governed by one mechanism.&lt;/p&gt;

&lt;p&gt;This post covers:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What a numeric field is marked with is its meaning, not its number of decimal places&lt;/li&gt;
&lt;li&gt;How the decimal places of an amount depend on the currency&lt;/li&gt;
&lt;li&gt;How the decimal places of a quantity or a weight depend on the unit of measure&lt;/li&gt;
&lt;li&gt;Whether detail lines are rounded before they are added up or after, and why that order decides whether the total is right&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  1. Mark the meaning, not the decimal places
&lt;/h2&gt;

&lt;p&gt;Once a table declares &lt;code&gt;decimal(19,4)&lt;/code&gt;, you know the column holds at most four decimal places. What you do not know is how many it should show, whether a calculated value should be rounded, or, when a customer asks for unit prices to show only two places, whether to change this column or somewhere else.&lt;/p&gt;

&lt;p&gt;The framework's approach is to declare a semantic kind, &lt;code&gt;NumberKind&lt;/code&gt;, on a field in &lt;code&gt;FormSchema&lt;/code&gt; (a &lt;code&gt;FormField&lt;/code&gt;), and let it decide how many places to keep and whether to round:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;code&gt;NumberKind&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;th&gt;Rounding strategy&lt;/th&gt;
&lt;th&gt;Where the places come from&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Quantity&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Quantity&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Round&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The unit of measure&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Weight&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Weight&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Round&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The unit of measure&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Amount&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Amount&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Round&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The currency&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Percent&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Percentage&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Round&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The company&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;UnitPrice&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Unit price&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Preserve&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The company&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Cost&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Cost&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Preserve&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The company&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ExchangeRate&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Exchange rate&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Preserve&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Fixed by the system&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Numbers that come out of a calculation are rounded; numbers that go into one are not. An amount is printed on the document, added into totals and posted to the books, so it has to be rounded to the places it is meant to have. A unit price, a cost or an exchange rate is there to be multiplied, and rounding it carries the error into every calculation that follows. So the framework's rounding method, &lt;code&gt;RoundByKind&lt;/code&gt;, hands those three back unchanged. A unit price shown with four places is only for people to read, and the framework does not round it to four places because of that.&lt;/p&gt;

&lt;p&gt;A field without a &lt;code&gt;NumberKind&lt;/code&gt; gets no format and no rounding from the framework, and leaving it off raises no error.&lt;/p&gt;

&lt;p&gt;Whether the places can be worked out in advance depends on whether they change while a user is working on a document. The places for a percentage, a unit price and a cost are set by the company, and an exchange rate is fixed by the framework at five. None of these four change, so their formats are worked out when the definition is delivered, and the front end prints by them.&lt;/p&gt;

&lt;p&gt;The places for an amount, a quantity or a weight do change: an amount follows its currency, and a quantity or a weight follows its unit. Those are the next two sections.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. The currency decides an amount's decimal places
&lt;/h2&gt;

&lt;p&gt;A currency's decimal places are a property of the currency, not company policy: the Japanese yen has no subunit, the US dollar has two places, and the Bahraini dinar has three. ISO 4217 sets this, and it does not change from one company to the next. So currency places live in a master shared by the whole system, and no individual company can touch it. What a company does decide is which currency is its base currency, and whether the final amount payable is aligned once more to a cash rounding unit (more on that later).&lt;/p&gt;

&lt;p&gt;That master is a definition file, kept in the same place as the form definitions and served through the same cache (&lt;a href="https://dev.to/jeff377/day-4-the-purpose-and-classification-of-definition-files-3i6n"&gt;The Purpose and Classification of Definition Files&lt;/a&gt; showed that all thirteen definition types work this way), and it is sent to the front end along with them (excerpt from the &lt;a href="https://github.com/jeff377/bee-library/blob/main/src/Bee.Definition/Defaults/CurrencySettings.xml" rel="noopener noreferrer"&gt;&lt;code&gt;CurrencySettings.xml&lt;/code&gt;&lt;/a&gt; the framework ships):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;CurrencyItem&lt;/span&gt; &lt;span class="na"&gt;Code&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"USD"&lt;/span&gt; &lt;span class="na"&gt;Numeric&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"840"&lt;/span&gt; &lt;span class="na"&gt;Rounding&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"0.01"&lt;/span&gt; &lt;span class="na"&gt;Symbol&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"$"&lt;/span&gt; &lt;span class="na"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"US Dollar"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;CurrencyItem&lt;/span&gt; &lt;span class="na"&gt;Code&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"JPY"&lt;/span&gt; &lt;span class="na"&gt;Numeric&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"392"&lt;/span&gt; &lt;span class="na"&gt;Rounding&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"1"&lt;/span&gt; &lt;span class="na"&gt;Symbol&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"¥"&lt;/span&gt; &lt;span class="na"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Japanese Yen"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;CurrencyItem&lt;/span&gt; &lt;span class="na"&gt;Code&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"BHD"&lt;/span&gt; &lt;span class="na"&gt;Numeric&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"048"&lt;/span&gt; &lt;span class="na"&gt;Rounding&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"0.001"&lt;/span&gt; &lt;span class="na"&gt;Symbol&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"BD"&lt;/span&gt; &lt;span class="na"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Bahraini Dinar"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What it stores is not the number of places but the smallest natural unit, and the places are derived from that (&lt;code&gt;0.01&lt;/code&gt; is two places, &lt;code&gt;1&lt;/code&gt; is zero). The smallest unit is what a currency actually exists in, and cash rounding, which comes later, uses it as well.&lt;/p&gt;

&lt;p&gt;An amount's places cannot be worked out in advance, because a user may switch the currency on screen from US dollars to yen. So the definition does not write down an amount's places, only which column holds its currency. If none is named, the document's currency column is used, and the places are worked out once that column has a value.&lt;/p&gt;

&lt;p&gt;That means rows in the same column can each have different places. Adding US dollars to yen means nothing, so the column footer shows a total only when the whole column is in a single currency. The column converted into the company's base currency has only one currency, so it always adds up.&lt;/p&gt;

&lt;p&gt;When the currency column is empty, the company's base currency is used. The base currency is a required company setting, and if it is not set, the framework throws an exception when it works out an amount rather than falling back to a default number of places.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. The unit decides the decimal places of a quantity or a weight
&lt;/h2&gt;

&lt;p&gt;Quantities follow the same reasoning: entered in pieces, a quantity should have no decimals, and entered in kilograms it can be 0.375. Whether it can have decimals depends on which unit that row was entered in, not on the field.&lt;/p&gt;

&lt;p&gt;The unit master is also a definition file shared by the whole system, and each unit's places are a property of that unit, not the company's to decide (excerpt from the &lt;a href="https://github.com/jeff377/bee-library/blob/main/src/Bee.Definition/Defaults/UnitSettings.xml" rel="noopener noreferrer"&gt;&lt;code&gt;UnitSettings.xml&lt;/code&gt;&lt;/a&gt; the framework ships):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;UnitItem&lt;/span&gt; &lt;span class="na"&gt;Code&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"PCS"&lt;/span&gt; &lt;span class="na"&gt;Decimals&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"0"&lt;/span&gt; &lt;span class="na"&gt;Dimension&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"count"&lt;/span&gt; &lt;span class="na"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Pieces"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;UnitItem&lt;/span&gt; &lt;span class="na"&gt;Code&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"KG"&lt;/span&gt; &lt;span class="na"&gt;Decimals&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"3"&lt;/span&gt; &lt;span class="na"&gt;Dimension&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"weight"&lt;/span&gt; &lt;span class="na"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Kilogram"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The two masters differ in how they store it. A unit of measure stores its places directly, while a currency stores its smallest unit and the places are derived from it, because cash rounding needs that smallest unit.&lt;/p&gt;

&lt;p&gt;The approach matches the one for amounts: a quantity field only states which column holds its unit, and its places are looked up in the unit master by that column's value. The difference is what happens when the value is missing:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Where the code comes from&lt;/th&gt;
&lt;th&gt;When that column is empty&lt;/th&gt;
&lt;th&gt;Default at the company level&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Amount&lt;/td&gt;
&lt;td&gt;The currency column that the amount field names, or the document's currency column if it names none&lt;/td&gt;
&lt;td&gt;The company's base currency is used&lt;/td&gt;
&lt;td&gt;The base currency, which is required&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Quantity / weight&lt;/td&gt;
&lt;td&gt;The unit column that the quantity field names&lt;/td&gt;
&lt;td&gt;There is nothing to stand in for it&lt;/td&gt;
&lt;td&gt;No default unit&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A company has a base currency, yet it will not have a default unit, because the detail lines of a single document can perfectly well be one in kilograms and the next in cases.&lt;/p&gt;

&lt;p&gt;The principle is this: a field marked &lt;code&gt;Quantity&lt;/code&gt; or &lt;code&gt;Weight&lt;/code&gt; must name a unit column, and a number that needs no unit is an ordinary numeric field with no &lt;code&gt;NumberKind&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Round then add, or add then round
&lt;/h2&gt;

&lt;p&gt;Three detail lines, each one piece at a unit price of 10.3333. Intuitively, adding at full precision and rounding once at the end is more accurate. Running it through the expressions in the case study's &lt;code&gt;FormSchema&lt;/code&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Approach&lt;/th&gt;
&lt;th&gt;Each row shows&lt;/th&gt;
&lt;th&gt;Total&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Round first, then add&lt;/td&gt;
&lt;td&gt;10.33&lt;/td&gt;
&lt;td&gt;30.99&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Add first, then round&lt;/td&gt;
&lt;td&gt;10.33&lt;/td&gt;
&lt;td&gt;31.00&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A cent apart, and the wrong one is the one that looks more accurate. The document shows three lines of 10.33, which a user adds up to 30.99, while the master total says 31.00, so the document does not agree with itself. An ERP system requires the master total to equal the sum of the detail lines without being a single cent off, because documents get printed, reconciled and audited.&lt;/p&gt;

&lt;p&gt;The framework's approach is to round each row as soon as it has been calculated, so the total only ever receives rounded values and the two agree as a matter of course. This rule is called round-then-sum. It is the only place the framework rounds automatically, and numbers a user types in are not rounded.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rounding comes in two layers
&lt;/h3&gt;

&lt;p&gt;Each detail row is rounded to the natural decimal places of its currency, and that layer is the same across the whole system. The final amount payable on a document can take one more layer: alignment to a cash rounding unit set by the company. The Swiss franc, for example, has no coin for a single cent, so a company can set 0.05, and then 12.34 is collected as 12.35 and 12.32 as 12.30.&lt;/p&gt;

&lt;p&gt;The two layers are different in nature. The first removes precision that does not exist. The second deliberately creates a difference, and the extra cent collected or the two cents short both have to be posted to a rounding account. So at this layer the framework returns only the amount payable, and the caller works out and records the difference itself.&lt;/p&gt;

&lt;h3&gt;
  
  
  The database column is capacity, not decimal places
&lt;/h3&gt;

&lt;p&gt;Table columns have a precision too, but that is a capacity limit: it only says how many places fit, and has nothing to do with how many places the value should show or be rounded to. The same amount type is created as &lt;code&gt;decimal(19,4)&lt;/code&gt;, &lt;code&gt;numeric(19,4)&lt;/code&gt;, &lt;code&gt;DECIMAL(19,4)&lt;/code&gt;, &lt;code&gt;NUMBER(19,4)&lt;/code&gt; and &lt;code&gt;NUMERIC(19,4)&lt;/code&gt; in the five databases, written differently with the same numbers.&lt;/p&gt;

&lt;p&gt;The limit is deliberately set well above the business places. Otherwise every additional company and every additional currency would mean another &lt;code&gt;ALTER TABLE&lt;/code&gt;, and that could not be run in operation.&lt;/p&gt;

&lt;p&gt;Places beyond the capacity are not currently handled by the framework. When a value is sent to the database, it carries only the value, the type, the length and whether NULL is allowed, not a precision, and before the write it is not rounded to the column's capacity either.&lt;/p&gt;

&lt;p&gt;So what happens to the excess is up to each database. Write &lt;code&gt;1.234567&lt;/code&gt; into a column with four decimal places and SQLite keeps it as it is (measured), while the other databases trim it by their own rules, so the same data stored in different databases ends up with different precision.&lt;/p&gt;

&lt;p&gt;A database can limit the places with a &lt;code&gt;CHECK&lt;/code&gt; constraint, but that only rejects data that does not fit, and it does not round. Storing a value at the right number of places has to be handled by the framework before the write, and that step has not been built yet.&lt;/p&gt;




&lt;h2&gt;
  
  
  Back to Northwind
&lt;/h2&gt;

&lt;p&gt;Only two fields in the case study's &lt;code&gt;FormSchema&lt;/code&gt; are marked with a meaning, and they happen to fall one on each side (excerpt from &lt;a href="https://github.com/jeff377/bee-northwind-avalonia/blob/main/Define/FormSchema/Order.FormSchema.xml" rel="noopener noreferrer"&gt;&lt;code&gt;Order.FormSchema.xml&lt;/code&gt;&lt;/a&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;FormField&lt;/span&gt; &lt;span class="na"&gt;FieldName&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"unit_price"&lt;/span&gt; &lt;span class="na"&gt;DbType&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Currency"&lt;/span&gt; &lt;span class="na"&gt;NumberKind&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"UnitPrice"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;FormField&lt;/span&gt; &lt;span class="na"&gt;FieldName&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"amount"&lt;/span&gt; &lt;span class="na"&gt;DbType&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Currency"&lt;/span&gt; &lt;span class="na"&gt;NumberKind&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Amount"&lt;/span&gt; &lt;span class="na"&gt;ReadOnly&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt;
           &lt;span class="na"&gt;ValueExpression&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"quantity * unit_price * (1 - discount)"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first is the unit price, whose places are set by the company. When the definition is delivered, &lt;code&gt;unit_price&lt;/code&gt; receives a format with four places, but the column in the case study's hand-written layout carries no marker, so the screen still shows the raw value. Enter &lt;code&gt;10.33335&lt;/code&gt;, and after a round of evaluation it is still &lt;code&gt;10.33335&lt;/code&gt;, not rounded.&lt;/p&gt;

&lt;p&gt;The second is the computed field from &lt;a href="https://dev.to/jeff377/day-8-declarative-business-rules-and-computed-fields-4jj9"&gt;Declarative Business Rules and Computed Fields&lt;/a&gt;, and it belongs to the currency group. The case study has no currency column, so the company's base currency, USD, is used, two places are looked up in the currency master, and the framework rounds to two places once it has evaluated the value. When the definition is delivered, the format for &lt;code&gt;amount&lt;/code&gt; is left empty and it names no currency column either; the two places on screen come from the marker on that layout column, worked out from the base currency. Neither multiple currencies nor units of measure can be demonstrated, and &lt;code&gt;ExchangeRate&lt;/code&gt; goes unused.&lt;/p&gt;

&lt;p&gt;The master total is not in the definition. &lt;code&gt;OrderBO&lt;/code&gt; adds up the &lt;code&gt;amount&lt;/code&gt; of the detail lines before saving, and those &lt;code&gt;amount&lt;/code&gt; values have already been rounded row by row by the framework before they are added, so this satisfies round-then-sum: the framework rounds, and the application adds. That post on business rules said a sum across rows is something expressions cannot currently write. The rounding half is already done by the framework, and all that remains is the adding.&lt;/p&gt;




&lt;h2&gt;
  
  
  In closing
&lt;/h2&gt;

&lt;p&gt;From the semantic marker on a field to the capacity of a table column, this post has been answering one question: who decides how many decimal places a number has.&lt;/p&gt;

&lt;p&gt;The answer is spread across different places, and deliberately so. Unit prices, costs, percentages and exchange rates are set by the company or the framework, because that is policy. Amounts are decided by the currency, and quantities and weights by the unit, because those are properties of the currency and the unit themselves. Whether the amount payable is aligned once more is decided by the company, because that is collection policy. How many places a column can hold is decided by the table, because that is only capacity.&lt;/p&gt;

&lt;p&gt;Mixing these up is where things go wrong. Tie the places to the table, and a new currency means changing the structure. Tie them to the display, and unit prices and exchange rates lose precision to rounding. Round after adding, and the master total no longer matches the detail lines.&lt;/p&gt;

&lt;p&gt;Beyond the places, there is the value itself. The framework automatically rounds only values it has calculated itself, and the unit prices, discounts and amounts a user types in are left untouched. Rounding is a rule for calculated results, not a correction of what the user entered; the moment the framework changes a number on a user's behalf, the books gain a difference that nobody can say who made.&lt;/p&gt;

&lt;p&gt;The next post, &lt;a href="https://dev.to/jeff377/day-27-the-semantic-boundaries-between-date-datetime-and-time-24i8"&gt;The Semantic Boundaries Between Date, DateTime and Time&lt;/a&gt;, turns to another kind of data. Numbers at least all look alike, but time does not: a single CLR type holds several kinds of value with different meanings, and the type system cannot tell them apart.&lt;/p&gt;




&lt;p&gt;📚 Part of the thirty-post series &lt;a href="https://dev.to/jeff377/erp-architects-notes-designing-a-definition-driven-framework-series-index-4ln7"&gt;ERP Architect's Notes: Designing a Definition-Driven Framework&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📘 Original article on HackMD, in Traditional Chinese:&lt;br&gt;
👉 &lt;a href="https://hackmd.io/@jeff377/day-26-numeric-semantics" rel="noopener noreferrer"&gt;https://hackmd.io/@jeff377/day-26-numeric-semantics&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📢 You are welcome to republish this, please credit the source.&lt;br&gt;
📬 Follow for more engineering notes:&lt;br&gt;
&lt;a href="https://www.facebook.com/profile.php?id=61574839666569" rel="noopener noreferrer"&gt;Facebook&lt;/a&gt; ｜ &lt;a href="https://hackmd.io/@jeff377" rel="noopener noreferrer"&gt;HackMD&lt;/a&gt; ｜ &lt;a href="https://github.com/jeff377" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; ｜ &lt;a href="https://www.nuget.org/profiles/jeff377" rel="noopener noreferrer"&gt;NuGet&lt;/a&gt;&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>dotnet</category>
      <category>erp</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Day 25: The Audit Trail and the Before and After Values of a Change</title>
      <dc:creator>Jeff Tsai</dc:creator>
      <pubDate>Wed, 18 Nov 2026 01:00:09 +0000</pubDate>
      <link>https://dev.to/jeff377/day-25-the-audit-trail-and-the-before-and-after-values-of-a-change-1j5k</link>
      <guid>https://dev.to/jeff377/day-25-the-audit-trail-and-the-before-and-after-values-of-a-change-1j5k</guid>
      <description>&lt;p&gt;By the time someone asks who changed the amount on this record last month, it is usually months later. Whoever is asking has neither the screen as it looked then nor the build that was running, only the few rows left behind at the time. Who logged in, who changed which field from what to what, who looked at which record: every decision in the auditing layer has to assume that scene first, including the ones that look like nothing more than saving space.&lt;/p&gt;

&lt;p&gt;This post covers:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Why the name is copied in as well, rather than storing an id and joining back for it&lt;/li&gt;
&lt;li&gt;How one column encodes the whole outcome of a login, and why the failure reason is kept apart from the line shown to the user&lt;/li&gt;
&lt;li&gt;Why the access record covers only single record reads, and why it is the one of the three that is off by default&lt;/li&gt;
&lt;li&gt;Why a change is one row per save, why that write sits outside the transaction, and what happens when it fails&lt;/li&gt;
&lt;li&gt;How what changed is stored as a DiffGram, and why there is only one place it can be captured&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  1. Three tables and the columns they share
&lt;/h2&gt;

&lt;p&gt;Recording every call is not workable. Reads far outnumber writes, and the volume would go straight through the roof. The questions auditing has to answer come down to three, one question one table:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;The question to answer&lt;/th&gt;
&lt;th&gt;Where it lands&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Who logged in and what came of it&lt;/td&gt;
&lt;td&gt;&lt;code&gt;st_log_login&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Who looked at which record&lt;/td&gt;
&lt;td&gt;&lt;code&gt;st_log_access&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Who did what to which record, and which fields went from what to what&lt;/td&gt;
&lt;td&gt;&lt;code&gt;st_log_change&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The three tables live in the &lt;code&gt;log&lt;/code&gt; database category. The whole thing is off by default, and each of the three has a sub switch of its own. Whichever table it is, every row starts with the same set of shared columns.&lt;/p&gt;

&lt;p&gt;To record who did something, the intuitive approach is to store a row identifier for the user and join back for the name when it needs displaying.&lt;/p&gt;

&lt;p&gt;That does not work here, and not for performance reasons. &lt;code&gt;log&lt;/code&gt; is a database category of its own (&lt;a href="https://dev.to/jeff377/day-11-connections-transactions-and-query-conditions-in-the-data-access-layer-2d1h"&gt;Connections, Transactions and Query Conditions in the Data Access Layer&lt;/a&gt; covered how logs can occupy a database to themselves), physically separate from &lt;code&gt;common&lt;/code&gt; and &lt;code&gt;company&lt;/code&gt;, and it can be moved to another machine, or split by year into several databases. A join across databases is not slower, it is absent. A column that only means something once joined is, here, a column that means nothing.&lt;/p&gt;

&lt;p&gt;So every piece of identity is stored as two columns, an id in one and a display name in the other (excerpt from &lt;a href="https://github.com/jeff377/bee-library/blob/main/src/Bee.Definition/Logging/AuditEntry.cs" rel="noopener noreferrer"&gt;&lt;code&gt;AuditEntry.cs&lt;/code&gt;&lt;/a&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;columns&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AuditColumn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"user_id"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;UserId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;columns&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AuditColumn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"user_name"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;UserName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;columns&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AuditColumn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"company_id"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;CompanyId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;columns&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AuditColumn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"company_name"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;CompanyName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// access_token / trace_id / client_ip / source ...&lt;/span&gt;
&lt;span class="n"&gt;columns&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AuditColumn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"api_key_id"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ApiKeyId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;columns&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AuditColumn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"api_key_name"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ApiKeyName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This data is not normalized, and deliberately not. A user changes their name, a company changes its letterhead, and the old rows keep the name as it was then, which no longer lines up with &lt;code&gt;st_user&lt;/code&gt; as it stands today. That is exactly what auditing wants: a record is meant to state how things were at that moment, not how they are today. The same trade-off would be a defect elsewhere; here it is the specification.&lt;/p&gt;

&lt;p&gt;Within the key columns, the id is not a secret, so it can go into an audit row; the secret itself the framework does not hold in plain text once it has been issued, so it cannot. This group of columns is most useful on the login table: a run of failed logins concentrated in one application, and the same run spread across several applications, read as two different things.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. The login record
&lt;/h2&gt;

&lt;p&gt;The simplest of the three, and it comes on with the master switch. What it adds to the shared columns is an event value, plus a line giving the reason for a failure.&lt;/p&gt;

&lt;p&gt;The event value carries the outcome itself, rather than there being a separate column for whether it succeeded. Login succeeded, login failed, account locked, logout, and a session opened for a user by a trusted local call: five values, each of them an outcome. Without that extra column, the case where the event value and the outcome column disagree cannot arise.&lt;/p&gt;

&lt;p&gt;The failure reason column and the line shown on the login screen are not doing the same job. The on-screen message is always terse and does not tell the user whether the account does not exist or the password was wrong, which is the usual practice: the moment the two can be told apart, the login endpoint becomes a tool for testing whether an account exists. The audit column has no such concern. Its reader is someone going back over the records afterwards, and the more specific it is, the more use it is.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. The access record
&lt;/h2&gt;

&lt;p&gt;Pulling up a record to look at it is itself something to be recorded. Who pulled up the full details of which customer, and when, matters as much in a compliance question as who changed it. The category table in that post on the data access layer described &lt;code&gt;log&lt;/code&gt; as the record of operations, logins and changes, and operations is this axis.&lt;/p&gt;

&lt;p&gt;What it adds to the shared columns is which form, plus the key of that record. Pulling up the detail of a single record loads the whole record anyway, so who plus which record is the unit of one row.&lt;/p&gt;

&lt;p&gt;It is the only one of the three that is off by default, because its volume tracks the number of queries. The deployment level switch can turn it on across the board, and it can also be turned on one form at a time: the customer master is worth recording, a code table is not.&lt;/p&gt;

&lt;p&gt;The row is written by the single record read; list queries and lookup windows leave no record.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. The change record
&lt;/h2&gt;

&lt;p&gt;One save on a form, one row in the audit. That row says who, when, which record on which form, and whether it was an insert, an update or a delete, and it carries a sensitive flag.&lt;/p&gt;

&lt;p&gt;The same row also holds the change detail: which fields changed, and what each one went from and to, all of it in a single column. The next section covers where that column comes from.&lt;/p&gt;

&lt;p&gt;Insert, update or delete is decided by the state of the master row. Changing only the detail without touching a character of the master still counts as an update, because that whole record did change.&lt;/p&gt;

&lt;p&gt;With the master switch off, none of it is recorded. Once it is on, the change axis is on by default just as login is, and a per form rule can override that default: change and access each have a three state value, follow the deployment level default, force recording, or force no recording.&lt;/p&gt;

&lt;h3&gt;
  
  
  Writing sits outside the transaction
&lt;/h3&gt;

&lt;p&gt;The audit row is written outside the transaction. A save that succeeded has succeeded, and a failure to write the audit row does not drag the save down with it: the framework logs an error and the business operation finishes as normal.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/jeff377/day-14-extension-points-on-a-business-object-and-when-to-override-them-43lm"&gt;Extension Points on a Business Object and When to Override Them&lt;/a&gt; put this decision next to its opposite: master and detail must share a transaction, while change auditing accepts not being atomic. The two decisions go opposite ways because the broken states look different. A missing detail leaves an incomplete document and raises nothing; a missing audit row means one record fewer, with the data itself correct.&lt;/p&gt;

&lt;p&gt;Not blocking the business does not mean rows may be dropped at will. Every way the write can fail has a route out:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Situation&lt;/th&gt;
&lt;th&gt;Behavior&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Normal&lt;/td&gt;
&lt;td&gt;Goes into a bounded in-memory queue, and a background batch writes it to the log database&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The queue is full&lt;/td&gt;
&lt;td&gt;Switches to a synchronous write on the spot rather than being dropped&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The log database cannot be written to&lt;/td&gt;
&lt;td&gt;Lands as a file if a fallback file is configured, and if not, all that is left is that one error&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;For a queue that must not drop anything, the right behavior at saturation is not to wait, it is to take another route.&lt;/p&gt;

&lt;p&gt;One more thing turns up only at this layer: an audit write is itself a database access, and the framework records slow database accesses separately, which is another audit write. The fix is not to run that detection when the log database is the one being hit, and the loop breaks there.&lt;/p&gt;

&lt;p&gt;With a mechanism that cuts across every layer, the easiest thing to miss is not who it fails to cover, it is that it covers itself.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. The change detail
&lt;/h2&gt;

&lt;p&gt;The usual way to record what changed is a field by field comparison, one row per field. Its strength is queries at field level: how many times a given field was changed over the past year, and by whom, is one SQL statement away. The cost is that the row count is the number of changes multiplied by the number of fields, the comparison logic has to be written from scratch, and that query strength goes unused on most of the questions people actually ask, which are what changed on this document.&lt;/p&gt;

&lt;p&gt;The framework uses what &lt;code&gt;DataSet&lt;/code&gt; already has: &lt;code&gt;GetChanges()&lt;/code&gt; returns the change set, and &lt;code&gt;WriteXml&lt;/code&gt; in DiffGram mode writes it as one piece of XML carrying the new and the old values together, with the field structure as it stood at the time attached in front, all wrapped in the same outer element. One save writes one row, the old and new values of the master and of every detail are inside that one column, and there is no custom comparison code at all.&lt;/p&gt;

&lt;p&gt;Change the status and the freight on an order, and what that column stores has this shape:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;AuditChanges&amp;gt;
  &amp;lt;xs:schema id="form" ...&amp;gt;
    ...  the types of sys_id / status / freight  ...
  &amp;lt;/xs:schema&amp;gt;
  &amp;lt;diffgr:diffgram ...&amp;gt;
    &amp;lt;form&amp;gt;
      &amp;lt;Order diffgr:id="Order1" diffgr:hasChanges="modified"&amp;gt;
        &amp;lt;status&amp;gt;Confirmed&amp;lt;/status&amp;gt;
        &amp;lt;freight&amp;gt;41.90&amp;lt;/freight&amp;gt;
      &amp;lt;/Order&amp;gt;
    &amp;lt;/form&amp;gt;
    &amp;lt;diffgr:before&amp;gt;
      &amp;lt;Order diffgr:id="Order1"&amp;gt;
        &amp;lt;status&amp;gt;Draft&amp;lt;/status&amp;gt;
        &amp;lt;freight&amp;gt;32.38&amp;lt;/freight&amp;gt;
      &amp;lt;/Order&amp;gt;
    &amp;lt;/diffgr:before&amp;gt;
  &amp;lt;/diffgr:diffgram&amp;gt;
&amp;lt;/AuditChanges&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Capture and write fall on either side of the save: the change set is taken before the save, and the trail is written after it. That position is pinned from both sides by &lt;code&gt;DoBeforeSave&lt;/code&gt; and &lt;code&gt;DoSave&lt;/code&gt;, and the post on business object extension points explained why. What follows is what it looks like in code (excerpt from &lt;a href="https://github.com/jeff377/bee-library/blob/main/src/Bee.Business/Form/FormBusinessObject.Write.cs" rel="noopener noreferrer"&gt;&lt;code&gt;FormBusinessObject.Write.cs&lt;/code&gt;&lt;/a&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;auditChange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ChangeAuditEnabled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;changes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;auditChange&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DataSet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetChanges&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rowKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;changeKind&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;auditChange&lt;/span&gt;
    &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nc"&gt;ExtractMasterChange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DataSet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;masterTableName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ChangeKind&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;DoSave&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;auditChange&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;changes&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nc"&gt;WriteChangeAudit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;changeKind&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rowKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;AuditDiffGram&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Serialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;changes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;masterTableName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That boolean is also cost control: with it off, &lt;code&gt;GetChanges&lt;/code&gt; is never called at all.&lt;/p&gt;

&lt;p&gt;Getting the position wrong raises nothing, it just leaves things out. Move it after &lt;code&gt;DoSave&lt;/code&gt; and, once the database write succeeds, the row states and the original values are cleared together, &lt;code&gt;GetChanges()&lt;/code&gt; comes back null, and that save does not exist in the trail at all. Move it before &lt;code&gt;DoBeforeSave&lt;/code&gt; and what gets recorded are the values from before the computed fields and the rules were filled in. Delete is the case where no change set is available; the approach is to load a snapshot from before the delete, mark every row as deleted, and run the same &lt;code&gt;GetChanges&lt;/code&gt;, which yields the full contents of that record as it stood before it was removed.&lt;/p&gt;

&lt;p&gt;Reading it back rests on that structure: read the structure out first, then read the DiffGram into it, and the result is a real &lt;code&gt;DataSet&lt;/code&gt;, with which fields changed derived by comparing the original and current values. The structure is carried by the row itself rather than taken from today's definition, so three years later, when that table has two more columns, one fewer, or a changed type, a trail row from three years ago still reads back as it was then. The ones written earlier do not carry this structure, and the framework tells old from new by that outermost element and takes a different route for each, without a single old row having to be migrated.&lt;/p&gt;




&lt;h2&gt;
  
  
  Back to Northwind
&lt;/h2&gt;

&lt;p&gt;The case study turns auditing on, which brings login and change with it while access stays off, and switches the background write to synchronous so that it can be demonstrated at all. The rule table holds three rows: orders and customers force access on, and categories force change off.&lt;/p&gt;

&lt;p&gt;Change the contact and the city on one customer, and &lt;code&gt;st_log_change&lt;/code&gt; gains a row:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user_id      = demo          company_id   = NORTHWIND
user_name    = Demo User     company_name = Northwind Traders
source       = Customer.Save prog_id      = Customer
change_kind  = 2             is_sensitive = 1
row_key      = sys_rowid of the ALFKI row
api_key_id   = (empty)       api_key_name = (empty)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the column that holds the change itself, &lt;code&gt;contact_name&lt;/code&gt; and &lt;code&gt;city&lt;/code&gt; each have a before and an after value. The &lt;code&gt;1&lt;/code&gt; in &lt;code&gt;is_sensitive&lt;/code&gt; comes from the rule row for customers.&lt;/p&gt;

&lt;p&gt;Force off and force on were each checked once: change a category and the data goes in while the trail gains nothing; the single record read axis goes the other way, with the deployment level off, and one read each on an order and a customer leaves one row each. Same deployment, same code, and the whole difference between recorded and not recorded sits in those three rows.&lt;/p&gt;

&lt;p&gt;The two key columns are empty, and empty for a good reason: the case study has not issued a single key, so the validator never went looking for one and there is no id to record. A mechanism that has not taken effect leaves a trace in the audit that is not zero rows, it is two missing columns on every row.&lt;/p&gt;

&lt;p&gt;The trail is there, and the case study cannot read it. The audit query endpoints declare &lt;code&gt;Encrypted&lt;/code&gt;, and a plain text request of the kind &lt;a href="https://dev.to/jeff377/day-23-the-api-payload-security-pipeline-order-protection-levels-and-keys-ona"&gt;The API Payload Security Pipeline: Order, Protection Levels and Keys&lt;/a&gt; described comes back with &lt;code&gt;This API requires encoded or encrypted transmission.&lt;/code&gt; Go again by the route that performs a key exchange, get past that gate, and what comes back is &lt;code&gt;Not authorized to read the audit log.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The reason is that the case study has no permission model at all. The check on audit reads does not hang off &lt;code&gt;FormSchema&lt;/code&gt;; it takes a reserved ProgId straight to the authorization service, so the opening described in &lt;a href="https://dev.to/jeff377/day-24-the-three-axes-of-permissions-action-row-and-column-15fo"&gt;The Three Axes of Permissions: Action, Row and Column&lt;/a&gt;, where a form that declares nothing skips the whole thing, does not exist here. Writing hangs off a boolean switch and reading hangs off a permission model, and a deployment with only the write side turned on is exactly the one that most needs telling to connect the read side up.&lt;/p&gt;




&lt;h2&gt;
  
  
  In closing
&lt;/h2&gt;

&lt;p&gt;An audit trail has no readers at all on the day it is written, and it only ever grows, until it has to be moved to another machine and split by year into several databases. That is exactly why the identity columns copy the names in as well: by then, a join is not a question of being slow, there is simply no such route.&lt;/p&gt;

&lt;p&gt;So not one of the decisions in this post is accepted on today's date. What gets recorded decides what can be asked on that day; each row being complete in itself decides whether it can be understood on that day; that column carrying its own structure decides whether it can still be read back after the table has been revised; and that write outside the transaction decides whether it was ever written down at all.&lt;/p&gt;

&lt;p&gt;The four posts in this chapter have looked at the same call from four angles: as bytes to be serialized, as bytes to be protected, as a request to be judged for permissions, and as an event to be recorded. The bottom line across all four is the same: everything the caller sends up is a claim, not a fact.&lt;/p&gt;

&lt;p&gt;Nothing that can determine the outcome is the caller's to decide; and whatever is to be recorded has to be recorded as what the caller actually sent. The two read as opposites, and are in fact two sides of the same line.&lt;/p&gt;

&lt;p&gt;The next post, &lt;a href="https://dev.to/jeff377/day-26-numeric-semantics-multiple-currencies-and-units-of-measure-2n2p"&gt;Numeric Semantics, Multiple Currencies and Units of Measure&lt;/a&gt;, changes level, to the values that have been stored and recorded themselves: what a number actually stands for inside the system.&lt;/p&gt;




&lt;p&gt;📚 Part of the thirty-post series &lt;a href="https://dev.to/jeff377/erp-architects-notes-designing-a-definition-driven-framework-series-index-4ln7"&gt;ERP Architect's Notes: Designing a Definition-Driven Framework&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📘 Original article on HackMD, in Traditional Chinese:&lt;br&gt;
👉 &lt;a href="https://hackmd.io/@jeff377/day-25-audit-trail" rel="noopener noreferrer"&gt;https://hackmd.io/@jeff377/day-25-audit-trail&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📢 You are welcome to republish this, please credit the source.&lt;br&gt;
📬 Follow for more engineering notes:&lt;br&gt;
&lt;a href="https://www.facebook.com/profile.php?id=61574839666569" rel="noopener noreferrer"&gt;Facebook&lt;/a&gt; ｜ &lt;a href="https://hackmd.io/@jeff377" rel="noopener noreferrer"&gt;HackMD&lt;/a&gt; ｜ &lt;a href="https://github.com/jeff377" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; ｜ &lt;a href="https://www.nuget.org/profiles/jeff377" rel="noopener noreferrer"&gt;NuGet&lt;/a&gt;&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>dotnet</category>
      <category>erp</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Day 24: The Three Axes of Permissions: Action, Row and Column</title>
      <dc:creator>Jeff Tsai</dc:creator>
      <pubDate>Tue, 17 Nov 2026 00:47:10 +0000</pubDate>
      <link>https://dev.to/jeff377/day-24-the-three-axes-of-permissions-action-row-and-column-15fo</link>
      <guid>https://dev.to/jeff377/day-24-the-three-axes-of-permissions-action-row-and-column-15fo</guid>
      <description>&lt;p&gt;&lt;a href="https://dev.to/jeff377/day-23-the-api-payload-security-pipeline-order-protection-levels-and-keys-ona"&gt;The API Payload Security Pipeline: Order, Protection Levels and Keys&lt;/a&gt; closed by changing axis. A protection level governs the form of one call, and at no point does it ask which data this person may see.&lt;/p&gt;

&lt;p&gt;Permissions are something an ERP system cannot avoid, and they have never been one question. They are three: whether a buyer may delete a purchase order, which orders may be deleted, and whether the cost column on that order can be seen at all, or changed. The three answers come from different sources, and they are enforced in different places.&lt;/p&gt;

&lt;p&gt;This post covers:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What &lt;code&gt;X-Api-Key&lt;/code&gt; and &lt;code&gt;Authorization&lt;/code&gt; each answer, and why neither of them is a permission&lt;/li&gt;
&lt;li&gt;Where the action axis is granted, and why a form that declares nothing skips the whole thing&lt;/li&gt;
&lt;li&gt;Who declares the scope on the row axis, how it becomes one SQL statement, and why assembling it never queries the database&lt;/li&gt;
&lt;li&gt;Why the column axis exists only on the front end, and why it is not bound to the same permission model&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  1. First, separating out the check that is not a permission
&lt;/h2&gt;

&lt;p&gt;A remote caller carries two headers, and neither of them answers what this person may do:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Header&lt;/th&gt;
&lt;th&gt;What it answers&lt;/th&gt;
&lt;th&gt;Where the answer comes from&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;X-Api-Key&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Which application is calling&lt;/td&gt;
&lt;td&gt;The keys already issued in &lt;code&gt;st_api_key&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Authorization: Bearer&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Who is calling&lt;/td&gt;
&lt;td&gt;A session that has not expired&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The key itself grants no data permission at all. What it identifies is the calling application, and authenticating the user is the access token's job. &lt;a href="https://dev.to/jeff377/day-16-json-rpc-20-protocol-choice-and-the-request-pipeline-47gf"&gt;JSON-RPC 2.0: Protocol Choice and the Request Pipeline&lt;/a&gt; mentioned a list of methods that need no header, and there are in fact two such lists, one per header.&lt;/p&gt;

&lt;p&gt;On the token side there is one more check, declared on the method. &lt;code&gt;[ApiAccessControl]&lt;/code&gt; takes two parameters. The protection level from the previous post is one of them, and the other has only two values, &lt;code&gt;Anonymous&lt;/code&gt; and &lt;code&gt;Authenticated&lt;/code&gt;. What it asks is whether this method needs a valid token, not what this person may do.&lt;/p&gt;

&lt;p&gt;Once those are answered, who this is and which application they came from are both settled. Permissions start after that, and they split into three axes:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Axis&lt;/th&gt;
&lt;th&gt;What it asks&lt;/th&gt;
&lt;th&gt;Server&lt;/th&gt;
&lt;th&gt;Front end&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Action&lt;/td&gt;
&lt;td&gt;Whether this can be done at all&lt;/td&gt;
&lt;td&gt;The authoritative decision&lt;/td&gt;
&lt;td&gt;The state of toolbar commands&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Row&lt;/td&gt;
&lt;td&gt;Which records it can be done to&lt;/td&gt;
&lt;td&gt;The query condition, and reading back on write&lt;/td&gt;
&lt;td&gt;Takes no part&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Column&lt;/td&gt;
&lt;td&gt;What can be seen and what can be changed&lt;/td&gt;
&lt;td&gt;Masks nothing&lt;/td&gt;
&lt;td&gt;Hides, masks or locks read only&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The last two of those are the ones to look at. The three axes are not three parallel mechanisms, because where they run is not the same: action runs on both ends, row only on the server, and column only on the front end.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Action: whether this can be done at all
&lt;/h2&gt;

&lt;p&gt;The action axis is bound to a business entity, not to a form. Which permissions there are to talk about is registered in &lt;code&gt;PermissionModels&lt;/code&gt;, and a &lt;code&gt;FormSchema&lt;/code&gt; declares a &lt;code&gt;PermissionModelId&lt;/code&gt; pointing at one of them, with the grant sitting on that model. Both excerpts in this post come from &lt;a href="https://github.com/jeff377/bee-library/blob/main/src/Bee.Business/Form/FormBusinessObject.Permission.cs" rel="noopener noreferrer"&gt;&lt;code&gt;FormBusinessObject.Permission.cs&lt;/code&gt;&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;modelId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;DefineAccess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetFormSchema&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ProgId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PermissionModelId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IsNullOrEmpty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;modelId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;authorization&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Can&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;AccessToken&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;modelId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ForbiddenException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;$&lt;span class="s2"&gt;"Permission denied: '{action}' on model '{modelId}'."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Binding to a business entity buys something very practical in an ERP system: one model can be consumed by several ProgIds. Creating an order, querying orders and the report all reference the same &lt;code&gt;PurchaseOrder&lt;/code&gt;, so one grant takes effect on all three. Bind to the form instead and the same purchasing permission has to be granted three times, once per form, and the one that gets missed shows no symptom at all. It is simply looser than the others.&lt;/p&gt;

&lt;p&gt;There are six action flags: insert, query, update and delete, plus printing and exporting, the two that take data out. The latter two are listed separately because printing a purchase order or exporting it to a spreadsheet takes the data out of the system just the same, and being able to read is not the same thing as being able to carry the whole lot away once auditing is involved. State transitions such as submitting for approval, posting and confirming are deliberately kept off this axis, since those belong to workflow permissions.&lt;/p&gt;

&lt;p&gt;There is one opening here. A form that declares no &lt;code&gt;PermissionModelId&lt;/code&gt; skips the whole thing, with neither action nor row applied, so that existing forms can be rolled out one at a time.&lt;/p&gt;

&lt;h3&gt;
  
  
  The front end narrows too, but only follows
&lt;/h3&gt;

&lt;p&gt;Action is the only one of the three axes that runs on both ends. The front end's share is only about not putting actions this user has no permission for in front of them to click: it receives a list of what this person may do, and hides or disables those commands accordingly.&lt;/p&gt;

&lt;p&gt;But it stops nobody. The server's check is the boundary, and it has to be, because the standard screen is only one caller among several: another front end, a script, an integration hitting the API directly, none of them pass through that layer. If the front end fails to narrow, the server refuses all the same.&lt;/p&gt;

&lt;p&gt;A command can carry more than one flag, since saving is both an insert and an update, and a command like that shows as long as one of them is held, with whether it is actually allowed decided on the server. Guessing wide on the front end is not a security problem, while guessing narrow leaves the user thinking the feature is broken.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Row: which records it can be done to
&lt;/h2&gt;

&lt;p&gt;The declaration on the row axis is split across two places, and deliberately does not overlap. A role's grant states a strategy, and the &lt;code&gt;FormSchema&lt;/code&gt; marks which column:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;code&gt;ScopeStrategy&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;Condition added when reading&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;All&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;No restriction&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Own&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The owner column &lt;code&gt;IN&lt;/code&gt; the user row id and the employee row id&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Dept&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The department column matches the department the person belongs to, &lt;code&gt;OR&lt;/code&gt;ed with &lt;code&gt;Own&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;DeptAndSub&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The department column &lt;code&gt;IN&lt;/code&gt; that department and all its descendants, &lt;code&gt;OR&lt;/code&gt;ed with &lt;code&gt;Own&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A strategy never names a column, and the column is marked &lt;code&gt;Owner&lt;/code&gt; or &lt;code&gt;Dept&lt;/code&gt; by &lt;code&gt;FormField.ScopeRole&lt;/code&gt;. What splitting them buys is that the same set of strategies holds for every form: moving to another form changes only which column carries the mark, with no new strategy to write. &lt;code&gt;Own&lt;/code&gt; matches two identities, because the same column can hold whoever entered the record on one form and the employee the record is about on another. One role can see a whole department while only being able to change its own records, since a grant is one row per action and the scope travels with that row.&lt;/p&gt;

&lt;p&gt;The same role marker can sit on more than one column of a master table. A staff transfer document has a department transferred out of and a department transferred into, and the managers on both sides should see it, so both columns are marked &lt;code&gt;Dept&lt;/code&gt; and the condition that comes out is the union of two branches.&lt;/p&gt;

&lt;p&gt;The role, role grant and user role binding tables land in the company database rather than the shared one, so the same person can hold two completely different roles at two companies. This is one of the examples behind &lt;a href="https://dev.to/jeff377/day-11-connections-transactions-and-query-conditions-in-the-data-access-layer-2d1h"&gt;Connections, Transactions and Query Conditions in the Data Access Layer&lt;/a&gt; saying the prefix answers who owns a table and the database category answers where it lives: all three carry the framework's own prefix, yet sit on the company side.&lt;/p&gt;

&lt;h3&gt;
  
  
  Add a condition when reading, and read back when writing
&lt;/h3&gt;

&lt;p&gt;What the strategy and the markers work out to is a &lt;code&gt;FilterNode&lt;/code&gt;. That same post established that a query condition is a tree living in the definition layer, with not one conversion between the condition the user fills in and each of five databases assembling its own statement. Permission filtering produces a tree of the same type, attached straight alongside the user's, and the two together are one &lt;code&gt;FilterGroup.All&lt;/code&gt;. Rows outside the scope are filtered out of a list, and a single record query returns &lt;code&gt;null&lt;/code&gt;, indistinguishable from no such row, so a caller cannot use it to probe whether an invisible row exists.&lt;/p&gt;

&lt;p&gt;The write side cannot use the same trick. The &lt;code&gt;DataSet&lt;/code&gt; that arrives is the caller's, and the department column in it can say whatever they like, so comparing the value in the payload against the scope amounts to letting the person being blocked fill in the answer. What the framework does is read back once:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ExistsInScope&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rowId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;scopeFilter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ForbiddenException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;$&lt;span class="s2"&gt;"Record out of scope for '{action}' on model '{schema.PermissionModelId}'."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;ExistsInScope&lt;/code&gt; issues &lt;code&gt;WHERE sys_rowid = this row AND the scope condition&lt;/code&gt;, asking whether that row in the database is inside the scope, which a forged payload cannot move. Delete takes the same path, and out of scope it deletes zero rows with no cascade. Insert applies no scope, since a row that does not exist yet has no existing scope to violate, and whether it may be inserted is the action axis's business.&lt;/p&gt;

&lt;h3&gt;
  
  
  Multiple roles are always unioned
&lt;/h3&gt;

&lt;p&gt;One person carrying several roles is normal in an ERP system, and the rule is the same on the action and row axes: union. Action flags are &lt;code&gt;OR&lt;/code&gt;ed together and scope conditions are &lt;code&gt;OR&lt;/code&gt;ed together, and if any one role gives &lt;code&gt;All&lt;/code&gt; on this action, the whole scope is unrestricted.&lt;/p&gt;

&lt;p&gt;Put the other way, this model has no deny grants. Give him the purchasing manager role but block delete cannot be done here, since adding another role only adds permissions and never removes any, and the only way to get that effect is to split off a role without delete and give him that instead. What it buys is traceability: if someone can delete, some one role gave it to them.&lt;/p&gt;

&lt;h3&gt;
  
  
  How those conditions become one SQL statement
&lt;/h3&gt;

&lt;p&gt;Turning that tree into SQL takes only two rules, and they are recursive: a group collapses into a pair of parentheses with &lt;code&gt;AND&lt;/code&gt; or &lt;code&gt;OR&lt;/code&gt; between the parts, and a single condition collapses into one comparison with the value always replaced by a parameter. With a role holding &lt;code&gt;DeptAndSub&lt;/code&gt;, a master table marking a department column and an employee column, and the user filling in the start of a document number on the list, what comes out is this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WHERE ((dept_rowid IN (@p0, @p1) OR emp_rowid IN (@p2, @p3)) AND sys_no LIKE @p4)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The outer &lt;code&gt;AND&lt;/code&gt; is the seam between the permission and the user's condition, and the inner &lt;code&gt;OR&lt;/code&gt; is the department subtree or one's own. The user only ever sees the half they filled in, with the other half attached by the server, and there is no way for them to work back from the returned data to what that half looks like.&lt;/p&gt;

&lt;p&gt;Two details remain. First, &lt;code&gt;@p0&lt;/code&gt; through &lt;code&gt;@p3&lt;/code&gt; are four department and identity ids, all going through parameters, while column names take the other route, looked up against the definition and then given identifier quoting. The earlier post's line about parameterization stopping values but not column names is about exactly that fork, and permission conditions are no exception. Second, when the scope resolves to no usable grant, what comes out is not an empty condition but an empty &lt;code&gt;IN&lt;/code&gt;, which lands in SQL as &lt;code&gt;1 = 0&lt;/code&gt;. That is deliberate, because an empty condition on this path means unrestricted, and the direction of failure has to be closed.&lt;/p&gt;

&lt;p&gt;There is one more thing that statement does not say: assembling it never queried the database. Roles and grants and the department organization tree are all per company caches that read the source once when there is no hit, the same shape as in &lt;a href="https://dev.to/jeff377/day-12-cross-node-invalidation-for-definition-and-data-caches-17gf"&gt;Cross-Node Invalidation for Definition and Data Caches&lt;/a&gt; with the source swapped for the database, and the three row ids come from the session snapshot that &lt;a href="https://dev.to/jeff377/day-13-session-management-and-lifecycle-248p"&gt;Session Management and Lifecycle&lt;/a&gt; resolves once on entering a company.&lt;/p&gt;

&lt;p&gt;What the department part saves is more than one query. &lt;code&gt;DeptAndSub&lt;/code&gt; wants this department and all its descendants, and with the organization tree already in memory the subtree flattens in place into a list of ids, landing in SQL as nothing more than a flat &lt;code&gt;IN&lt;/code&gt;. Without that tree every query would have to expand the subtree recursively in the database, and recursive queries are written differently by each of five databases, which amounts to growing five more dialects along the permission path.&lt;/p&gt;

&lt;p&gt;Deciding runs entirely in memory, and only applying touches data. The read side costs nothing, with the condition merged into the statement that was going to run anyway, and only the write side adds one &lt;code&gt;SELECT sys_rowid&lt;/code&gt; per changed master row, while a save that only inserts queries nothing at all.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Column: what can be seen and what can be changed
&lt;/h2&gt;

&lt;p&gt;A purchase order has a cost column. The buyer can see it, a sales assistant should not, and the cost column does not appear only on purchase orders. It is scattered across quotations, goods receipts and stock queries.&lt;/p&gt;

&lt;p&gt;The column axis is declared on &lt;code&gt;FormField.SensitiveCategory&lt;/code&gt;, whose value comes from a closed list: amount, cost, personal data, plus the default of no category. Picking a category rather than letting each form invent an identifier of its own is because a category is a cross form data question, not that form's question. A column marked cost is governed by the same grant wherever it appears, so whether this person may see cost is granted once and takes effect everywhere.&lt;/p&gt;

&lt;p&gt;So the permission model it binds to is not the same one the other two axes use. Action and row go through the &lt;code&gt;PermissionModelId&lt;/code&gt; the form declares for itself, and column goes through the one the category name maps to. That means whether a purchase order may be changed and whether cost may be seen are two independent grants, and one person can be given every action on purchase orders while still not being shown cost.&lt;/p&gt;

&lt;p&gt;A column's permission has two levels, viewing and modifying, and modifying only comes into question once viewing is allowed. The front end handles them two ways: what may not be viewed is hidden or masked, and what may not be modified is locked read only.&lt;/p&gt;

&lt;p&gt;Last is this axis's boundary: the column axis exists only on the front end. The server applies no column permission when fetching data, &lt;code&gt;GetList&lt;/code&gt; and &lt;code&gt;GetData&lt;/code&gt; return the values of sensitive columns just the same, and callers that do not pass through a front end get the complete row. This is not an oversight. Form fields often take part in computation, and the framework runs one and the same expression on both ends, so with one of those columns missing those calculations come out on neither end. The framework therefore marks this axis as a presentation layer matter, and data that genuinely must not leave the server has to be placed on one of the other two boundaries, action or row.&lt;/p&gt;




&lt;h2&gt;
  
  
  In closing
&lt;/h2&gt;

&lt;p&gt;Permissions in this framework are three questions, not one, and what separates the three axes is not only what they ask but who enforces them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Action, whether this can be done at all. Granted on a business entity rather than on a form, so one model can be shared by several forms; done on both the server and the front end, with the server end being the boundary&lt;/li&gt;
&lt;li&gt;Row, which records it can be done to. The role gives the strategy and the &lt;code&gt;FormSchema&lt;/code&gt; marks the column, deliberately without overlap, so moving to another form changes only which column carries the mark; the read side merges the condition into the statement that was going to run anyway, and the write side does not trust what arrives, always reading back the authoritative copy&lt;/li&gt;
&lt;li&gt;Column, what can be seen and what can be changed. Bound to the other model the category name maps to, so independent of the first two axes; and it exists only on the front end&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Multiple roles are always unioned, adding and never subtracting, and what that buys is that every permission can be traced to where it came from.&lt;/p&gt;

&lt;p&gt;What lets the first two axes finish deciding without touching the database is holding the grants and the organization tree in memory in full. The tree matters most, since it turns a recursive query into a flat &lt;code&gt;IN&lt;/code&gt;, one way of writing it shared by all five databases.&lt;/p&gt;

&lt;p&gt;Resolving to no grant produces an always false condition rather than an empty one, and an out of scope single record query comes back as no such row rather than not authorized, both of them falling to the closed side. The one thing that falls to the open side is a form that declares nothing skipping the whole thing, a door left open so that an existing system can be rolled out one form at a time.&lt;/p&gt;

&lt;p&gt;The next post, &lt;a href="https://dev.to/jeff377/day-25-the-audit-trail-and-the-before-and-after-values-of-a-change-1j5k"&gt;The Audit Trail and the Before and After Values of a Change&lt;/a&gt;, closes this chapter, on what these decisions leave behind: who, at what time, on which record, did what, and from which value to which value.&lt;/p&gt;




&lt;p&gt;📚 Part of the thirty-post series &lt;a href="https://dev.to/jeff377/erp-architects-notes-designing-a-definition-driven-framework-series-index-4ln7"&gt;ERP Architect's Notes: Designing a Definition-Driven Framework&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📘 Original article on HackMD, in Traditional Chinese:&lt;br&gt;
👉 &lt;a href="https://hackmd.io/@jeff377/day-24-permission-axes" rel="noopener noreferrer"&gt;https://hackmd.io/@jeff377/day-24-permission-axes&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📢 You are welcome to republish this, please credit the source.&lt;br&gt;
📬 Follow for more engineering notes:&lt;br&gt;
&lt;a href="https://www.facebook.com/profile.php?id=61574839666569" rel="noopener noreferrer"&gt;Facebook&lt;/a&gt; ｜ &lt;a href="https://hackmd.io/@jeff377" rel="noopener noreferrer"&gt;HackMD&lt;/a&gt; ｜ &lt;a href="https://github.com/jeff377" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; ｜ &lt;a href="https://www.nuget.org/profiles/jeff377" rel="noopener noreferrer"&gt;NuGet&lt;/a&gt;&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>dotnet</category>
      <category>erp</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Day 23: The API Payload Security Pipeline: Order, Protection Levels and Keys</title>
      <dc:creator>Jeff Tsai</dc:creator>
      <pubDate>Mon, 16 Nov 2026 00:38:46 +0000</pubDate>
      <link>https://dev.to/jeff377/day-23-the-api-payload-security-pipeline-order-protection-levels-and-keys-ona</link>
      <guid>https://dev.to/jeff377/day-23-the-api-payload-security-pipeline-order-protection-levels-and-keys-ona</guid>
      <description>&lt;p&gt;The previous post, &lt;a href="https://dev.to/jeff377/day-22-the-division-of-labor-between-xml-json-and-messagepack-51h4"&gt;The Division of Labor Between XML, JSON and MessagePack&lt;/a&gt;, closed by saying the other half of &lt;code&gt;value&lt;/code&gt; comes here. &lt;code&gt;Encoded&lt;/code&gt; and &lt;code&gt;Encrypted&lt;/code&gt; differ by one number on the API payload, and underneath that number are three stages that run in a fixed order, while on the way back the order is more than the same run in reverse.&lt;/p&gt;

&lt;p&gt;An ERP system's API has to answer two questions that are independent of each other: what a call's content should be processed into, and who is entitled to decide it may be processed less. The first is a fixed pipeline, the second is one declaration written onto the method. The two are kept apart, and are compared against each other at one stop only, which is access control.&lt;/p&gt;

&lt;p&gt;This post covers:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The order of serialization, compression and encryption, and what breaks when it is swapped&lt;/li&gt;
&lt;li&gt;That &lt;code&gt;ApiProtectionLevel&lt;/code&gt; and &lt;code&gt;PayloadFormat&lt;/code&gt; are two different things, and how access control compares them&lt;/li&gt;
&lt;li&gt;What actually sits under the name &lt;code&gt;aes-cbc-hmac&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Where keys come from, and why cryptographic primitives and security policy live in separate layers&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  1. The order of the three stages
&lt;/h2&gt;

&lt;p&gt;An &lt;code&gt;Encoded&lt;/code&gt; &lt;code&gt;value&lt;/code&gt; goes out as a Base64 string. Getting from an object to that string takes three stages, each carried by its own interface and each replaceable as a whole:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Stage&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;th&gt;Current implementation&lt;/th&gt;
&lt;th&gt;Can be replaced with&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Serialization&lt;/td&gt;
&lt;td&gt;Turns the object into bytes&lt;/td&gt;
&lt;td&gt;&lt;code&gt;messagepack&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;json&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Compression&lt;/td&gt;
&lt;td&gt;Makes those bytes shorter&lt;/td&gt;
&lt;td&gt;&lt;code&gt;gzip&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;none&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Encryption&lt;/td&gt;
&lt;td&gt;Turns bytes into bytes carrying an authentication code&lt;/td&gt;
&lt;td&gt;&lt;code&gt;aes-cbc-hmac&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;none&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The latter two names are in the deployment's &lt;code&gt;SystemSettings&lt;/code&gt;, while the first is declared by the caller on the API payload of every individual call, with no declaration meaning &lt;code&gt;messagepack&lt;/code&gt;. That split is what the previous post's three components and two ways of deciding was about. What can be replaced is each stage's implementation, not the order they run in. The encoding side, &lt;a href="https://github.com/jeff377/bee-library/blob/main/src/Bee.Api.Core/Transformers/ApiPayloadTransformer.cs" rel="noopener noreferrer"&gt;&lt;code&gt;ApiPayloadTransformer&lt;/code&gt;&lt;/a&gt;, is two lines, and the source puts the order at the end of each:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;serializer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Serialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;              &lt;span class="c1"&gt;// Serialize&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;ApiServiceOptions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PayloadCompressor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Compress&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// Compress&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Encryption is not in those two lines. It is one further step the layer outside decides on according to the format: &lt;code&gt;Encoded&lt;/code&gt; stops here, and &lt;code&gt;Encrypted&lt;/code&gt; goes one stage further.&lt;/p&gt;

&lt;p&gt;Compression comes before encryption because compression does nothing at all to ciphertext. The output of good encryption is statistically indistinguishable from random data, and repetition and skew are exactly what a compression algorithm looks for. Running the case study's order &lt;code&gt;FormSchema&lt;/code&gt; through both orders shows it:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Order&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Serialize, compress, then encrypt&lt;/td&gt;
&lt;td&gt;Leaves a little over a quarter after compression, with encryption adding a few dozen bytes on top&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Serialize, encrypt, then compress&lt;/td&gt;
&lt;td&gt;Comes out larger after compression than it went in, and the final size is more than three times the first&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Swapping the order makes no call fail, and no check anywhere says a word. It just means the compression stage stops doing anything from then on.&lt;/p&gt;

&lt;p&gt;The way back is those three steps reversed: decrypt, decompress, deserialize. But what matters about the reverse direction is not that it runs backwards, it is that each stage only ever sees bytes that got past the one before it. The authentication code is verified before decryption, and decryption comes before decompression, so the gzip decompressor never at any point holds a stretch of data that has not been verified.&lt;/p&gt;

&lt;p&gt;A decompressor is a parser that accepts arbitrary bytes. Feed it ciphertext with one bit changed and it will start parsing all the same, then throw because it cannot read a structure out of it. This order means it never gets that far.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. The level declared, and the format actually used
&lt;/h2&gt;

&lt;p&gt;The three values of &lt;code&gt;PayloadFormat&lt;/code&gt; were covered in that same earlier post. What access control looks at is not that one on its own, it is the result of comparing it with another enum, and that one has four values.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;The question it answers&lt;/th&gt;
&lt;th&gt;Values&lt;/th&gt;
&lt;th&gt;Who decides&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;PayloadFormat&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;What this call actually did&lt;/td&gt;
&lt;td&gt;Three&lt;/td&gt;
&lt;td&gt;The caller, writing it onto the API payload&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ApiProtectionLevel&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The least this method will accept&lt;/td&gt;
&lt;td&gt;Four&lt;/td&gt;
&lt;td&gt;Whoever writes the method, attaching it to the method or to the type&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Two of the values share a name across the two enums, being &lt;code&gt;Encoded&lt;/code&gt; and &lt;code&gt;Encrypted&lt;/code&gt;, which makes it easy to read them as two ways of saying the same thing. They are not: one records a fact, the other declares a requirement.&lt;/p&gt;

&lt;p&gt;What the four levels ask for, and the kind of method each suits:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Level&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;th&gt;Where it suits&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Public&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;HTTPS is enough&lt;/td&gt;
&lt;td&gt;Calls where the server seeing the content does no harm, such as the connectivity probe &lt;code&gt;Ping&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Encoded&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;At least encoded&lt;/td&gt;
&lt;td&gt;Content that should not be readable straight out of a packet capture, but that is not worth paying for encryption&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Encrypted&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Encrypted&lt;/td&gt;
&lt;td&gt;Content that should not be plain text inside the server either, such as managing API keys&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;LocalOnly&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Remote calls are not accepted&lt;/td&gt;
&lt;td&gt;Things only the host process itself should start, such as writing a definition&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;How the framework's own methods are distributed across those four is itself a decision: the trust boundary is drawn at HTTPS, and only the handful whose authors judged explicitly that the content must not be visible once inside the server are marked &lt;code&gt;Encrypted&lt;/code&gt;. Nothing declares the middle level at present, which is where the format side lands rather than something the level side asks for.&lt;/p&gt;

&lt;p&gt;Drawing the boundary at HTTPS does not mean application layer encryption is pointless. HTTPS protects only the stretch where data travels over the network, and it is unwrapped the moment it reaches the server, so every stop that handles it after that sees plain text. A node gets compromised, logging and monitoring keep the content, something is intercepted and replayed across network segments or over a VPN: all of these are real in actual ERP deployments.&lt;/p&gt;

&lt;p&gt;Drawing the line at &lt;code&gt;Public&lt;/code&gt; buys one thing: a front end with no .NET runtime does not have to implement the whole encryption pipeline before it can reach its first method. The cost is that for calls at that level, &lt;code&gt;value&lt;/code&gt; is visible once it is inside the server, and a method needing that layer has to be marked for it.&lt;/p&gt;

&lt;p&gt;The test is that the actual format has to be greater than or equal to the declared level: doing more is fine, doing less is refused. What follows is all about remote calls, and a remote call gets past &lt;code&gt;LocalOnly&lt;/code&gt; at no format level at all:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Actual format \ Declared level&lt;/th&gt;
&lt;th&gt;&lt;code&gt;Public&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;Encoded&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;Encrypted&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;LocalOnly&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Plain&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Passes&lt;/td&gt;
&lt;td&gt;Refused&lt;/td&gt;
&lt;td&gt;Refused&lt;/td&gt;
&lt;td&gt;Refused&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Encoded&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Passes&lt;/td&gt;
&lt;td&gt;Passes&lt;/td&gt;
&lt;td&gt;Refused&lt;/td&gt;
&lt;td&gt;Refused&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Encrypted&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Passes&lt;/td&gt;
&lt;td&gt;Passes&lt;/td&gt;
&lt;td&gt;Passes&lt;/td&gt;
&lt;td&gt;Refused&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Firing at the case study's endpoint, the three refusals each carry their own message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Plain    calling System.ListApiKeys → This API requires encoded or encrypted transmission.
Encoded  calling System.ListApiKeys → This API requires encrypted transmission.
Plain    calling System.SaveDefine  → This API is restricted to local calls only.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The other half, that doing more is fine, has been measured too: send an &lt;code&gt;Encoded&lt;/code&gt; API payload to the &lt;code&gt;Public&lt;/code&gt; list query and it passes, and what comes back is &lt;code&gt;Encoded&lt;/code&gt; as well. The server does not touch the format, it wraps the response back up using the value the caller sent, so both directions of one round trip are always the same format.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;LocalOnly&lt;/code&gt; is not stricter encryption. It asks something else: this method does not arrive over the network at all. Not being on the same axis, it is not compared with the format for size, it is judged on its own. &lt;a href="https://dev.to/jeff377/day-17-the-connector-and-the-convergence-of-client-responsibilities-5d9k"&gt;The Connector and the Convergence of Client Responsibilities&lt;/a&gt; mentioned that a few server side methods accept in-process calls only, and this is the slot it meant. They can still be called, just not over the network. A local call is let through whole the moment it reaches this check, without the format being looked at, so the table above does not govern them.&lt;/p&gt;

&lt;p&gt;This check can sit ahead of decryption because both values it compares are available from the start: the format is a number on the API payload, and the level is attached to the type and fixed once compiled. Neither side requires opening &lt;code&gt;value&lt;/code&gt; and looking inside. &lt;a href="https://dev.to/jeff377/day-16-json-rpc-20-protocol-choice-and-the-request-pipeline-47gf"&gt;JSON-RPC 2.0: Protocol Choice and the Request Pipeline&lt;/a&gt; said access control sits ahead of restoration and does no decryption work for a request that has not passed, and this is the condition that makes it hold.&lt;/p&gt;

&lt;p&gt;A method with no declaration is not unrestricted, it throws. The search order is the method itself, then the one it overrides, then the type declaring it. The cost is that a missing declaration only surfaces when somebody calls it for the first time, so there is a separate build time rule that points such methods out.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. What sits under the name &lt;code&gt;aes-cbc-hmac&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The name puts three parts together: a symmetric encryption algorithm, a mode of operation, and a message authentication code. In the implementation it comes to four decisions, and the body of the encrypting and decrypting is &lt;a href="https://github.com/jeff377/bee-library/blob/main/src/Bee.Base/Security/AesCbcHmacCryptor.cs" rel="noopener noreferrer"&gt;&lt;code&gt;AesCbcHmacCryptor&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What goes in is one 64 byte key, cut in two and used as two: the first 32 as the AES key, the last 32 as the HMAC key. One key does not hold both jobs&lt;/li&gt;
&lt;li&gt;Every encryption generates a new IV on the spot and writes it into the output in the clear, because decryption needs it. Encrypting the same data with the same key twice in a row was measured to produce different bytes&lt;/li&gt;
&lt;li&gt;Encrypt first, then stamp an authentication code over the encrypted result, not the other way round. What that buys is a decrypting end that turns data away before decrypting it, whereas the version done the other way round has to decrypt before it can verify, which amounts to letting the decryptor eat a stretch of data of unknown origin first. That line from the first section, each stage only sees bytes that got past the one before it, starts here&lt;/li&gt;
&lt;li&gt;Verifying the authentication code uses a fixed time comparison, not &lt;code&gt;==&lt;/code&gt;. An ordinary loop returns at the first byte that differs, which turns how many bytes were guessed right into something measurable from outside, and lets an attacker assemble the authentication code one byte at a time&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That fixed time comparison is three lines in the decryption stage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;computedHmac&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hmac&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ComputeHash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dataToVerify&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nc"&gt;CryptographicOperations&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FixedTimeEquals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hmacBytes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;computedHmac&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CryptographicException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"HMAC validation failed."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The message itself hides a choice too. Three different ways of breaking it were measured: one bit changed in the ciphertext, one bit changed in the authentication code, and an entirely unrelated key. All three come back with the same &lt;code&gt;HMAC validation failed.&lt;/code&gt;. Being unable to tell them apart is correct. Being able to distinguish a wrong authentication code from a wrong key is itself a signal that can be probed.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Where the keys come from
&lt;/h2&gt;

&lt;p&gt;Getting all four of the previous section's decisions right still leaves one thing unanswered: who supplies that 64 byte key. The framework keeps this and the previous section in two separate layers:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Layer&lt;/th&gt;
&lt;th&gt;Location&lt;/th&gt;
&lt;th&gt;What it holds&lt;/th&gt;
&lt;th&gt;Examples&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Cryptographic primitives&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Bee.Base&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Computing an output from an input, with no business meaning&lt;/td&gt;
&lt;td&gt;AES-CBC-HMAC, RSA, password hashing, the key generator, file hashing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Security policy&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Bee.Definition&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Where a key comes from, who may reach it and how it is verified&lt;/td&gt;
&lt;td&gt;The master key source, protecting keys, access token validation, the protection level from the second section&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The dividing line takes one question: does this code know what it is computing? The encryption code in the previous section recognizes only bytes and a key, and does not know it is encrypting an API call, so it lives in the primitive layer. That is also why this post's payload and the key protection coming up use the same code.&lt;/p&gt;

&lt;h3&gt;
  
  
  The master key is the first item in the chain
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://dev.to/jeff377/day-4-the-purpose-and-classification-of-definition-files-3i6n"&gt;The Purpose and Classification of Definition Files&lt;/a&gt; reached a conclusion about the startup dependency chain that carries straight over here: the master key is the first item in the chain, and it cannot be encrypted itself, so &lt;code&gt;SystemSettings&lt;/code&gt; records only where it is. The framework's default copy, &lt;a href="https://github.com/jeff377/bee-library/blob/main/src/Bee.Definition/Defaults/SystemSettings.xml" rel="noopener noreferrer"&gt;&lt;code&gt;SystemSettings.xml&lt;/code&gt;&lt;/a&gt;, records the name of an environment variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;MasterKeySource&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;Type&amp;gt;&lt;/span&gt;Environment&lt;span class="nt"&gt;&amp;lt;/Type&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;Value&amp;gt;&lt;/span&gt;BEE_MASTER_KEY&lt;span class="nt"&gt;&amp;lt;/Value&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/MasterKeySource&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An environment variable is the present default and a file is the other option. Each route has its own exposure surface, and the environment variable is not chosen for being safer. It is chosen because containers and secret management tools inject through environment variables as a matter of course, while going through a file means either mounting a volume or baking the file into the image. The master key does not encrypt any call directly. Its use is unlocking the other keys in the settings, which are stored as ciphertext, and what wraps them is &lt;code&gt;AesCbcHmacCryptor&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  The transport key is one per session
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://dev.to/jeff377/day-13-session-management-and-lifecycle-248p"&gt;Session Management and Lifecycle&lt;/a&gt; listed what a session carries, one of them being this connection's symmetric key, and it named this post at the time. That key comes from an interface, and the framework ships three implementations:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Provider&lt;/th&gt;
&lt;th&gt;Where its key comes from&lt;/th&gt;
&lt;th&gt;Survives a session rebuild?&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Shared&lt;/td&gt;
&lt;td&gt;The whole deployment shares a single key&lt;/td&gt;
&lt;td&gt;Survives&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Derived (the default)&lt;/td&gt;
&lt;td&gt;Derived from a root key and the access token&lt;/td&gt;
&lt;td&gt;Survives&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Random&lt;/td&gt;
&lt;td&gt;Generated at login, living only inside the session&lt;/td&gt;
&lt;td&gt;Does not survive&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;What the third column decides is whether rebuilding a session from a database seed, which that same earlier post described, can hold at all. The interface carries a property that answers exactly this: if the key cannot be recovered, a rebuilt session leaves the user looking as though they are still signed in while every encrypted call fails.&lt;/p&gt;

&lt;p&gt;The derived one's root key comes from the master key by default, and a value given in the settings takes precedence. The cost of making it the default is that the same access token always derives the same key, rather than a random one at each login. What that buys is a key that never has to be stored and that every node can compute for itself, while the token itself is a GUID nobody can guess and the root key never leaves the server. The order here is counterintuitive as a result: the token has to be generated first, and only then can the key be derived.&lt;/p&gt;

&lt;h3&gt;
  
  
  How that key reaches the caller
&lt;/h3&gt;

&lt;p&gt;At login the caller generates an RSA key pair and writes the public key into the login request. Once the server has built the session it wraps the symmetric key with that public key and returns it alongside the access token. Every call asking for encryption after that uses it.&lt;/p&gt;

&lt;p&gt;That same post on the Connector wrote up the exception on the browser side: an RSA key pair cannot be generated there, so the public key sent is an empty string and the server returns an empty string too. The degrading happens on the calling end rather than on the server, and a call asking for &lt;code&gt;Encrypted&lt;/code&gt; falls back to &lt;code&gt;Encoded&lt;/code&gt; by itself when it has no key in hand. Set that against the second section and it is clear how far this fallback route goes: the &lt;code&gt;Encoded&lt;/code&gt; format gets past &lt;code&gt;Public&lt;/code&gt; and &lt;code&gt;Encoded&lt;/code&gt;, and does not get past the handful marked &lt;code&gt;Encrypted&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Back to Northwind
&lt;/h2&gt;

&lt;p&gt;The application code in the case study relating to this post is a few lines at startup: put a demo master key in if the environment variable is not set, build the payload options from the definition file into components, then allow automatic creation when the framework starts. The rest is all in the definition files: &lt;code&gt;gzip&lt;/code&gt; for compression, &lt;code&gt;aes-cbc-hmac&lt;/code&gt; for encryption, and the master key from &lt;code&gt;BEE_MASTER_KEY&lt;/code&gt;. For the &lt;code&gt;value&lt;/code&gt; slot the case study declares nothing, so it takes &lt;code&gt;messagepack&lt;/code&gt;, which is what applies when nothing is declared.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;[ApiAccessControl]&lt;/code&gt; appears zero times in the whole case study. The order business object overrides &lt;code&gt;GetNewData&lt;/code&gt;, and the overriding method carries no declaration, relying on exactly the second section's rule that the search moves on to the method it overrides.&lt;/p&gt;

&lt;p&gt;The implementation that does not encrypt is only allowed to be constructed in debug mode, and the case study has debug mode on. &lt;a href="https://dev.to/jeff377/day-18-the-error-contract-and-user-visible-messages-1d0n"&gt;The Error Contract and User Visible Messages&lt;/a&gt; used the same flag, where what it decided was whether error messages get masked. So that check may as well not exist here, and the case study picked real encryption anyway. The check has a gap of its own as well: it only stops the route that builds an implementation by name, and the route that hands a component in directly goes around it.&lt;/p&gt;

&lt;p&gt;What actually makes the case study's encryption not count is not that check, it is the demo master key written into the source. The comment beside it says outright that production has to inject a real one through a real deployment mechanism before startup. The framework cannot tell which kind of environment it is running in, and blocking startup outright would take down test environments wanting to run the sample data, so this is recorded in a comment and nowhere else.&lt;/p&gt;




&lt;h2&gt;
  
  
  In closing
&lt;/h2&gt;

&lt;p&gt;A call's &lt;code&gt;value&lt;/code&gt; carries three stages of processing and one declaration:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The three run in the order serialize, compress, encrypt, and the way back runs them in reverse, where the point of reversing is that each stage only sees bytes that got past the check before it&lt;/li&gt;
&lt;li&gt;A protection level records the least this method accepts and a format records what this call actually did, and what is compared is the former being less than or equal to the latter. Three of the four levels sit on one axis, and the fourth only borrows that axis to stand in line&lt;/li&gt;
&lt;li&gt;The four decisions in the encryption stage can each be verified one at a time: two keys, a new IV every time, encrypt then stamp, and a fixed time comparison&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All three are things anybody can go and check. A pipeline with the order swapped shows it in the compression ratio on the spot. Whether a level holds is answered by firing once at the endpoint for each. Whether the authentication code is stamped in the right place is answered by changing one bit and trying.&lt;/p&gt;

&lt;p&gt;One thing is not. Whether that master key really is known only to whoever deployed it cannot be seen from inside the framework, and cannot be measured either. Once the three above are all done right, the strength of the whole thing equals whatever is in that environment variable, and that happens outside the code: the real line of defense is the deployment checklist.&lt;/p&gt;

&lt;p&gt;In a security pipeline, the part about whether it is built right can be verified, and the part about whether it deserves trust cannot. The first is a design question, closed once it is written into the mechanism. The second has to be answered again at every deployment.&lt;/p&gt;

&lt;p&gt;The next post, &lt;a href="https://dev.to/jeff377/day-24-the-three-axes-of-permissions-action-row-and-column-15fo"&gt;The Three Axes of Permissions: Action, Row and Column&lt;/a&gt;, changes axis. A protection level governs the form of one call, and at no point does it ask which data this person may see.&lt;/p&gt;




&lt;p&gt;📚 Part of the thirty-post series &lt;a href="https://dev.to/jeff377/erp-architects-notes-designing-a-definition-driven-framework-series-index-4ln7"&gt;ERP Architect's Notes: Designing a Definition-Driven Framework&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📘 Original article on HackMD, in Traditional Chinese:&lt;br&gt;
👉 &lt;a href="https://hackmd.io/@jeff377/day-23-payload-pipeline" rel="noopener noreferrer"&gt;https://hackmd.io/@jeff377/day-23-payload-pipeline&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📢 You are welcome to republish this, please credit the source.&lt;br&gt;
📬 Follow for more engineering notes:&lt;br&gt;
&lt;a href="https://www.facebook.com/profile.php?id=61574839666569" rel="noopener noreferrer"&gt;Facebook&lt;/a&gt; ｜ &lt;a href="https://hackmd.io/@jeff377" rel="noopener noreferrer"&gt;HackMD&lt;/a&gt; ｜ &lt;a href="https://github.com/jeff377" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; ｜ &lt;a href="https://www.nuget.org/profiles/jeff377" rel="noopener noreferrer"&gt;NuGet&lt;/a&gt;&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>dotnet</category>
      <category>erp</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Day 22: The Division of Labor Between XML, JSON and MessagePack</title>
      <dc:creator>Jeff Tsai</dc:creator>
      <pubDate>Sun, 15 Nov 2026 01:25:10 +0000</pubDate>
      <link>https://dev.to/jeff377/day-22-the-division-of-labor-between-xml-json-and-messagepack-51h4</link>
      <guid>https://dev.to/jeff377/day-22-the-division-of-labor-between-xml-json-and-messagepack-51h4</guid>
      <description>&lt;p&gt;The previous post, &lt;a href="https://dev.to/jeff377/day-21-business-logic-customization-and-the-four-plugin-stages-3iha"&gt;Business Logic Customization and the Four Plugin Stages&lt;/a&gt;, closed by saying the level changes here. From this point the subject is no longer what a definition declares, it is how those definitions and that data travel once they leave memory.&lt;/p&gt;

&lt;p&gt;Serialization is two separate jobs. One is persistence: an object has to be stored and read back unchanged next time, which is how definitions are stored and how system data such as &lt;code&gt;SessionUser&lt;/code&gt; is stored. The other is transport: an object has to reach the other end, and the other end is not one thing. Desktop, browser, iOS and Android all run in different environments.&lt;/p&gt;

&lt;p&gt;This post covers:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Which route persistence takes, why transport splits into two, and what the dividing line is drawn on&lt;/li&gt;
&lt;li&gt;How &lt;code&gt;PayloadFormat&lt;/code&gt; decides what a call's &lt;code&gt;value&lt;/code&gt; looks like on the way out&lt;/li&gt;
&lt;li&gt;Why definition style things go out as an XML string rather than a JSON tree&lt;/li&gt;
&lt;li&gt;Why not one transport type can be left unregistered&lt;/li&gt;
&lt;li&gt;On the deserialization end, who decides the type, and why only one route needs an allowlist&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  1. One route for persistence, two for transport
&lt;/h2&gt;

&lt;p&gt;Persistence wants something a human can read, that can go into version control and be edited by hand, and that job goes through XML from end to end.&lt;/p&gt;

&lt;p&gt;Transport splits into two, divided on whether the caller is .NET:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Both ends .NET: &lt;code&gt;value&lt;/code&gt; goes through MessagePack&lt;/li&gt;
&lt;li&gt;The front end is not .NET, a pure JavaScript front end or a third party integration: &lt;code&gt;value&lt;/code&gt; goes through JSON&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The outer layer is the same on both routes: one call sends a JSON-RPC payload, and the protocol says that is JSON, which cannot be swapped out. What there is a choice about is the &lt;code&gt;value&lt;/code&gt; slot inside it. Without separate encoding it is simply part of that JSON, and only once it is encoded do the two routes above come into play.&lt;/p&gt;

&lt;p&gt;One route for persistence plus two for transport gives three formats, each doing what it is good at:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Format&lt;/th&gt;
&lt;th&gt;What it handles in the framework&lt;/th&gt;
&lt;th&gt;Needs an outside package&lt;/th&gt;
&lt;th&gt;Used by&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;XML&lt;/td&gt;
&lt;td&gt;Persistence for definitions and system data, and the body of definition style API results&lt;/td&gt;
&lt;td&gt;No, built into the BCL&lt;/td&gt;
&lt;td&gt;The definition layer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;JSON&lt;/td&gt;
&lt;td&gt;The outer JSON-RPC payload, and one of the two ways an encoded &lt;code&gt;value&lt;/code&gt; can be written&lt;/td&gt;
&lt;td&gt;No, built into the BCL&lt;/td&gt;
&lt;td&gt;The API layer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;MessagePack&lt;/td&gt;
&lt;td&gt;The other way an encoded &lt;code&gt;value&lt;/code&gt; can be written, and the one that applies when nothing else has been declared&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;The API layer&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Whether a format needs an outside package is not a footnote on that table, it is the reason for which layer uses it. A format that needs one cannot sit in the definition layer. The definition layer is at the bottom of the dependency graph, with the database, the repository, the business logic, every front end and the definition file tooling all downstream of it, so any package added there spreads along the dependency chain to every consumer, and not one of them has a use for it.&lt;/p&gt;

&lt;p&gt;What that buys is a definition layer that changes not at all when the transport format changes later. The cost is that the transport binding has to be written separately in the API layer, and there is no compiler holding that copy and the definition types together.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. &lt;code&gt;PayloadFormat&lt;/code&gt; decides what &lt;code&gt;value&lt;/code&gt; looks like on the way out
&lt;/h2&gt;

&lt;p&gt;When &lt;a href="https://dev.to/jeff377/day-16-json-rpc-20-protocol-choice-and-the-request-pipeline-47gf"&gt;JSON-RPC 2.0: Protocol Choice and the Request Pipeline&lt;/a&gt; went through the &lt;code&gt;params&lt;/code&gt; slot it mentioned &lt;code&gt;format&lt;/code&gt; and &lt;code&gt;type&lt;/code&gt;, noting only that they sit on the API payload rather than inside &lt;code&gt;value&lt;/code&gt;. What is in those two slots is this section.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;format&lt;/code&gt; has three values, all deciding the same thing: what form this call's &lt;code&gt;value&lt;/code&gt; takes on the way out.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;th&gt;What &lt;code&gt;value&lt;/code&gt; looks like on the way out&lt;/th&gt;
&lt;th&gt;Which serializer&lt;/th&gt;
&lt;th&gt;Used by&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Plain&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A JSON object tree a human can read&lt;/td&gt;
&lt;td&gt;The same System.Text.Json that produced the JSON-RPC payload&lt;/td&gt;
&lt;td&gt;The handful of calls made before login&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Encoded&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A Base64 string&lt;/td&gt;
&lt;td&gt;Whichever serializer the caller declared, serialized and then compressed&lt;/td&gt;
&lt;td&gt;Login itself, and the place a degraded call lands&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Encrypted&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A Base64 string&lt;/td&gt;
&lt;td&gt;Whichever serializer the caller declared, compressed and then encrypted&lt;/td&gt;
&lt;td&gt;Everything else, and the caller's default&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The compression and encryption pipeline itself is the next post's subject.&lt;/p&gt;

&lt;p&gt;The same method works under all three values, which is exactly the point of putting the format on the API payload rather than in the method name or the path. Send a &lt;code&gt;Plain&lt;/code&gt; API payload to the case study's login and &lt;code&gt;value&lt;/code&gt; is readable JSON:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"jsonrpc"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"2.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nl"&gt;"method"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"System.Login"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="nl"&gt;"params"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"format"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nl"&gt;"value"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"userId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"demo"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nl"&gt;"password"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"demo"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"1"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What comes back is readable too:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"jsonrpc"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"2.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nl"&gt;"method"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"System.Login"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="nl"&gt;"result"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"format"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nl"&gt;"value"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"accessToken"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"033bedcf-..."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nl"&gt;"userName"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"Demo User"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="nl"&gt;"timeZone"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"Asia/Taipei"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"1"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Change &lt;code&gt;format&lt;/code&gt; to &lt;code&gt;1&lt;/code&gt; on the same method, with nothing else touched, and what comes back is an error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"jsonrpc"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"2.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nl"&gt;"method"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"System.Login"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="nl"&gt;"error"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"code"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;-32099&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nl"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"TypeName is missing for deserialization."&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"1"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;type&lt;/code&gt; slot is used only by the latter two values, and the reason explains precisely where the difference between the three lies. A &lt;code&gt;Plain&lt;/code&gt; &lt;code&gt;value&lt;/code&gt; never leaves JSON at any point, so what the server receives is a &lt;code&gt;JsonElement&lt;/code&gt; with no type yet, and it is deserialized into the parameter type that method declares only after reflection has found it. The type comes from the method signature, not from the API payload. The other two are different: they pack the object into a string of bytes on the calling end, and bytes carry no type information of their own, so the type name has to be written onto the API payload and sent along. That slot is therefore a field the caller decides, and the server has to check it against a type allowlist before it reaches &lt;code&gt;Type.GetType&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Plain&lt;/code&gt; is not a debugging side road, it is the startup path. Probing connectivity, fetching shared settings and establishing a connection all happen before a transport key exists, and at that point there is nothing to encrypt with.&lt;/p&gt;

&lt;h3&gt;
  
  
  Three components, two ways of deciding
&lt;/h3&gt;

&lt;p&gt;The three components on &lt;code&gt;value&lt;/code&gt; are each an interface plus a factory that resolves an implementation by name, and they differ in where the name comes from:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Component&lt;/th&gt;
&lt;th&gt;Where the name is written&lt;/th&gt;
&lt;th&gt;Implementations available&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;serializer&lt;/td&gt;
&lt;td&gt;On the API payload of every individual call&lt;/td&gt;
&lt;td&gt;MessagePack, JSON&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;compressor&lt;/td&gt;
&lt;td&gt;In the deployment's &lt;code&gt;SystemSettings&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Gzip, no compression&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;encryptor&lt;/td&gt;
&lt;td&gt;In the deployment's &lt;code&gt;SystemSettings&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;AES-CBC-HMAC, no encryption&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A name with no matching implementation throws, with no quiet fall back to a default.&lt;/p&gt;

&lt;p&gt;They split into two because they are not answering the same kind of question. Compression and encryption answer whether this deployment protects &lt;code&gt;value&lt;/code&gt; and with what, which is the deploying party's decision. The serializer answers which kind of &lt;code&gt;value&lt;/code&gt; this caller is able to write, which is a capability of the caller. Folding the latter into deployment settings too would require every caller on one server to speak the same language, and the two routes in the first section do not speak the same language to begin with.&lt;/p&gt;

&lt;p&gt;A call that declares nothing is treated as MessagePack, because that is what callers predating this slot send.&lt;/p&gt;

&lt;p&gt;All three components are shut inside two places: the Connector on the calling end, and the API layer on the server. The method that fetches one page of a list has only query fields, conditions, sorting and paging in its signature, with no parameter relating to format at all, and what the business object receives on the far end is a restored object. So swapping in a better suited transport format later changes the serializer slot, and no application code on either end has to change.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Why definition style things go out as an XML string
&lt;/h2&gt;

&lt;p&gt;The previous section said &lt;code&gt;value&lt;/code&gt; can be a JSON tree. So fetching a &lt;code&gt;FormSchema&lt;/code&gt; should give back one too. It does not. Ask the case study for an order's &lt;code&gt;FormSchema&lt;/code&gt; with the same &lt;code&gt;Plain&lt;/code&gt; API payload, and &lt;code&gt;value&lt;/code&gt; has exactly one field in it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"result"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"format"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nl"&gt;"value"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"xml"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"&amp;lt;?xml version=\"1.0\" encoding=\"utf-8\"?&amp;gt;
&amp;lt;FormSchema ProgId=\"Order\" CategoryId=\"company\" ...&amp;gt;
  &amp;lt;Tables&amp;gt;&amp;lt;FormTable TableName=\"Order\" DbTableName=\"ft_order\" ..."&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An XML string, packed inside the JSON tree that is the API payload. Everything definition style takes this route. The API layer only delivers the string, and restoring it into an object is done by the Connector on the calling end, so &lt;code&gt;XmlSerializer&lt;/code&gt; has to run on every front end as well.&lt;/p&gt;

&lt;p&gt;The reason is that the persistence copy is already there and usable: definitions land on disk as XML to begin with, so sending them out reuses the same serialization at no extra cost.&lt;/p&gt;

&lt;p&gt;Letting definition types go through JSON or MessagePack too would cost something continuously. Every additional serialization adds its own set of things to keep track of: whether a round trip loses data, and whether deserialization can be stopped from constructing a type that should not be constructed. Definitions are a nested structure that evolves with each version, so every added property would have to be confirmed again in every format, and swapping in a better suited transport format later would mean revalidating the whole set of definition types once more.&lt;/p&gt;

&lt;p&gt;Their present shape is genuinely unsuited to it as well: collection properties are read only, each holding a reference to its own owner. The three serializers happen to split into two camps over that kind of property:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;How it reads back&lt;/th&gt;
&lt;th&gt;Meeting a read only collection property&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;XmlSerializer&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Takes the existing instance and fills the values in one at a time&lt;/td&gt;
&lt;td&gt;Restored normally&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;System.Text.Json&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Builds a new collection and assigns it back&lt;/td&gt;
&lt;td&gt;Nothing to assign to, so the whole thing is skipped&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;MessagePack&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Builds a new collection and assigns it back&lt;/td&gt;
&lt;td&gt;Nothing to assign to, so the whole thing is skipped&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The difference between the two camps is not capability, it is direction. And on the side that builds a new collection, whether it can be assigned to is the same question as whether that member exists at all.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Not one transport type can be left unregistered
&lt;/h2&gt;

&lt;p&gt;The rules on the MessagePack side are a separate set. Given that it does not even look at read only properties, the least effort route is to let it find the members itself by reflection, with nothing declared. On a desktop that does run.&lt;/p&gt;

&lt;p&gt;The problem is that this route relies on generating code at run time, and not every execution environment can supply that. Where it cannot, an unregistered type is not slower, it throws. So every transport type has to have a formatter registered explicitly, with the members listed out one at a time. The following is from the form family's &lt;a href="https://github.com/jeff377/bee-library/blob/main/src/Bee.Api.Core/MessagePack/WireContracts.Form.cs" rel="noopener noreferrer"&gt;&lt;code&gt;WireContracts.Form.cs&lt;/code&gt;&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;WireContract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;For&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;GetListRequest&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Member&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;nameof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;GetListRequest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SelectFields&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SelectFields&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SelectFields&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Member&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;nameof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;GetListRequest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Filter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Filter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Filter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;// remaining members listed one at a time&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That &lt;code&gt;nameof&lt;/code&gt; slot is not only about readability. What the formatter writes out is a map keyed by property name, an unrecognized key on the way back in is skipped, and a key that never arrived leaves the property at the default declared on it. The one method one parameter object rule from the post on the request pipeline therefore buys one more thing: adding a property to an &lt;code&gt;Args&lt;/code&gt; type does not break callers that have not upgraded yet. That is a property this present shape happens to give, not a promise anybody is holding, and no test verifies it.&lt;/p&gt;

&lt;p&gt;The verbosity is unavoidable: switching to one generic formatter that reads property types puts you straight back on the route that generates code at run time. So this list can only be written by hand, and there is no generator to rerun.&lt;/p&gt;

&lt;p&gt;Collections carry several further traps of their own on this route, some of which behave perfectly normally on a development machine, so testing fine on a desktop cannot be taken as evidence.&lt;/p&gt;

&lt;p&gt;Nothing in the compiler holds this list and the types together, and what keeps it from drifting is a set of tests. They start from the same set of message types, walk everything reachable, and compare in both directions: everything reachable must be registered, and anything registered that nothing can reach is reported too. Those tests can also be run on a development machine simulating that kind of environment, rather than waiting for a deployment to find out.&lt;/p&gt;

&lt;p&gt;The whole package is kept in one place: the package reference, all the formatters and this list all sit in one folder under &lt;code&gt;Bee.Api.Core&lt;/code&gt;, and no other assembly has to know MessagePack exists. That is exactly what the outside package question in the first section was asking.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. On the deserialization end, who decides the type
&lt;/h2&gt;

&lt;p&gt;Deserialization turns bytes sent from outside into an object inside the process. How dangerous that is depends on who holds the decision about which type it becomes, and there are three different answers here.&lt;/p&gt;

&lt;p&gt;The XML route is the most closed. When the server stores a definition back, the type is not read out of the XML content, it is looked up in a fixed table using the &lt;code&gt;DefineType&lt;/code&gt; on the request, and a value that is not in the table throws.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Plain&lt;/code&gt; route is decided by the method signature, which is the &lt;code&gt;JsonElement&lt;/code&gt; from the second section: the caller does not even have a field it could put a type in.&lt;/p&gt;

&lt;p&gt;The encoded route is the only one the caller decides, through the &lt;code&gt;type&lt;/code&gt; field from the second section, and it works the same way whichever serializer is used. Bytes carry no type, so the type name can only be written onto the API payload and sent along, which is why there has to be an allowlist before &lt;code&gt;Type.GetType&lt;/code&gt;. It filters the whole name string, letting through neither generic arguments nor array element types, and refuses anything it cannot parse. The entire check happens before the object is constructed, not after.&lt;/p&gt;

&lt;p&gt;Without it, the caller would in effect be specifying which type the server constructs. The danger is not in the data that arrives, it is that some types do something as soon as they are constructed, and constructing objects is exactly what deserialization does.&lt;/p&gt;

&lt;p&gt;The allowlist itself has two layers: a fixed set of basic types, plus namespaces declared when the application starts, with the framework's own as the default. The wider the second layer is opened, the less this check stops, and that is the application's own choice.&lt;/p&gt;

&lt;p&gt;A correct type does not mean the content cannot misbehave. The XML side additionally forbids DTDs. &lt;code&gt;XmlSerializer&lt;/code&gt; reading a string directly already blocks the fetch an outside resource variety, but abbreviations a document declares for itself still get expanded, and that is enough for a file of a few kilobytes to consume all available memory once expanded. The framework routes all XML in through the same tightened reader, so no individual caller has to remember this.&lt;/p&gt;




&lt;h2&gt;
  
  
  Back to Northwind
&lt;/h2&gt;

&lt;p&gt;On the case study's server, the code relating to serialization is zero lines. The one &lt;code&gt;JsonSerializer&lt;/code&gt; in it is in the table seeding program, reading its own seed data, unrelated to transport.&lt;/p&gt;

&lt;p&gt;The front end is the same. Four front ends share one UI project, and that project has not one line handling formats. Even restoring the XML for definitions is done by the Connector. All three serializations are shut inside that layer, and the code above it does not have to know which one is used, how it is compressed, or how it is encrypted.&lt;/p&gt;

&lt;p&gt;Zero code does not mean zero cost. Of the four, all but the desktop one carry an entry in their build files granting an exception. The browser one runs on wasm and has to turn reflection based serialization on explicitly, or the very first call cannot be sent, because the framework's message types do not go through source generation and every call passes through that JSON-RPC payload layer. What is caught there is not &lt;code&gt;value&lt;/code&gt;, it is the outer layer. What the application pays lands in the build configuration, not in the source code.&lt;/p&gt;




&lt;h2&gt;
  
  
  In closing
&lt;/h2&gt;

&lt;p&gt;Persistence goes through XML from end to end: definitions have to land on disk anyway, so sending them out reuses the same copy at no extra cost.&lt;/p&gt;

&lt;p&gt;Transport splits into two layers. The outer JSON-RPC payload is fixed by the protocol, and the choice is in the &lt;code&gt;value&lt;/code&gt; slot inside it: both ends .NET goes through MessagePack, which is also what applies when nothing is declared, while a pure JavaScript front end and third party integrations go through JSON.&lt;/p&gt;

&lt;p&gt;And all three components on &lt;code&gt;value&lt;/code&gt; are shut inside two places, the Connector on the calling end and the API layer on the server. Replacing any one of them later needs not a single line of application code changed.&lt;/p&gt;

&lt;p&gt;The next post, &lt;a href="https://dev.to/jeff377/day-23-the-api-payload-security-pipeline-order-protection-levels-and-keys-ona"&gt;The API Payload Security Pipeline: Order, Protection Levels and Keys&lt;/a&gt;, carries on with the other half of &lt;code&gt;value&lt;/code&gt;: the compression and encryption that follow serialization, and why that order cannot be swapped.&lt;/p&gt;




&lt;p&gt;📚 Part of the thirty-post series &lt;a href="https://dev.to/jeff377/erp-architects-notes-designing-a-definition-driven-framework-series-index-4ln7"&gt;ERP Architect's Notes: Designing a Definition-Driven Framework&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📘 Original article on HackMD, in Traditional Chinese:&lt;br&gt;
👉 &lt;a href="https://hackmd.io/@jeff377/day-22-three-formats" rel="noopener noreferrer"&gt;https://hackmd.io/@jeff377/day-22-three-formats&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📢 You are welcome to republish this, please credit the source.&lt;br&gt;
📬 Follow for more engineering notes:&lt;br&gt;
&lt;a href="https://www.facebook.com/profile.php?id=61574839666569" rel="noopener noreferrer"&gt;Facebook&lt;/a&gt; ｜ &lt;a href="https://hackmd.io/@jeff377" rel="noopener noreferrer"&gt;HackMD&lt;/a&gt; ｜ &lt;a href="https://github.com/jeff377" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; ｜ &lt;a href="https://www.nuget.org/profiles/jeff377" rel="noopener noreferrer"&gt;NuGet&lt;/a&gt;&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>dotnet</category>
      <category>erp</category>
      <category>softwareengineering</category>
    </item>
  </channel>
</rss>
