DEV Community

Cover image for Day 29: Which Part Definitions, the Framework and Application Code Each Own
Jeff Tsai
Jeff Tsai

Posted on Edited on Originally published at hackmd.io

Day 29: Which Part Definitions, the Framework and Application Code Each Own

The previous post, Time Zone Conversion of DateTime, ended by saying that this one would go back to the case study and put those mechanisms in their places one by one.

Why ERP Needs a Definition-Driven Framework 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, The Northwind Case Study: Eight Forms and a Custom Business Object already covered. This post takes every behavior apart and looks at who is responsible for each part.

This post covers:

  1. The three levels of system behavior, and the test for each
  2. What appears at each level from a single form
  3. What runs on every call, and what the application paid for each
  4. What hand written code remains, how it converges into the framework, and where definitions are deliberately not used

1. System behavior falls into three levels

Take any behavior in the system apart, and each part lands in one of these three levels:

Level Test
Definition The behavior is decided by a definition file, and changing that file changes it
Built into the framework This part is the framework's code running, and the application wrote no code for it
Application code Someone wrote C# by hand, and switching to another ERP means writing it again

One behavior can span several levels

Take the detail amount. Taken apart, it belongs to all three levels:

This part Level
The formula for the amount Definition, written in the field's ValueExpression
How many places to round to Definition, looked up in the currency master
When to recalculate and when to round Built into the framework
Adding up each row's amount and writing it back to the master Application code

So a few rows in the next section's table read definition plus built into the framework.

The tables record the current state

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 An Overview of Definition-Driven Architecture: 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.

Not all definitions are written by the application

The TableSchema files for the common and log 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 The Purpose and Classification of Definition Files describes, and employee has two extra fields added.


2. What grows out of a single form

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:

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

Everything that adding a form touches falls in the definition level: one FormSchema, one TableSchema per physical table, one FormLayout, a row in DbCategorySettings, a row in ProgramSettings and an item in MenuSettings. 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 DbCategorySettings in turn.

The definition level can go wrong too, and nobody reports it. The detail's discount 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.

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


3. What runs on every call

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:

What happens on a call Level What the application paid
Endpoint, request parsing and method dispatch Built into the framework An empty controller subclass
Serialization and binding of transport types Built into the framework 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
Compression, encryption and key derivation Built into the framework A few lines at startup, being a master key seed, the payload options and a flag allowing automatic creation
Access control, the API key check and the three axes of permissions Built into the framework Nothing declared separately
Login, the session, and the user's time zone and language Built into the framework Zero lines of code, with the time zone and language read from the user's seeded row
The error contract and the messages users can see Built into the framework The message text is written on FormRule attributes, and the order's code puts it in the exceptions it throws
Auditing Built into the framework A block of switches in SystemSettings and a few rows in DbCategorySettings, zero lines of code
Time zone conversion Built into the framework Zero lines of code, since the application's own two time fields are both calendar dates
Numeric rounding Built into the framework Two fields are marked with NumberKind, and on top of that the currency master is deployed and the company's base currency is set to USD
Definition caching and cross-node invalidation Built into the framework A row in DbCategorySettings for the notification table

There is also a group of mechanisms that the case study uses only lightly:

  • Not used: business plugins, permission models, the unit of measure master, and the maintenance screen for plugin settings
  • 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

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.


4. What hand written code remains

The order's five things

OrderBO overrides only two of the steps the framework cuts out: getting a new record, and the step before saving. The latter looks like this:

protected override void DoBeforeSave(SaveContext context)
{
    base.DoBeforeSave(context);   // FormRule and ValueExpression finish running here first

    var dataSet = context.DataSet;
    var master = OrderDataSet.MasterRow(dataSet);

    OrderDataSet.RequireAtLeastOneDetail(dataSet);
    EnforceStatusRules(dataSet, master);
    AssignOrderNumber(master);
    OrderDataSet.ComputeTotal(dataSet);
}
Enter fullscreen mode Exit fullscreen mode

That is four things, and the fifth is in the step that gets a new record:

This one Why it is still in code
Whether a status transition is legal A genuine business judgment. Which states can be jumped to, and whether details can still change after confirmation, is set differently at every company
Generating the order number Standard behavior, which the framework should absorb and has not yet
At least one detail row 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
Summing the details back into the master The same. The framework already decides when each row is rounded, and adding up the rounded values is still outside it
The order date defaulting to today The framework already does this, taking today in the user's time zone; this line takes the server's today

Converging from the application into the framework

The logic and customization specific to a form converge toward the framework along the same path:

Step What happens In the case study
The application writes it first The framework has no matching mechanism yet, so it goes into an overridden business object step or a business plugin The detail amount, the required field checks and the five things above all started out in OrderBO
Deciding whether to absorb it The test from the overview post, whether every company does it the same way Status transitions differ at every company, so they stay in the application
The framework takes over What can be declared goes into definitions, and what needs no declaration is built into the framework The detail amount became a ValueExpression, the required field checks became FormRule 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
The application deletes the old code The part that has been taken over is removed from the business object The amount and the required field checks are already deleted, and the order date line is still there

The application shell

On the server, apart from the order, all the code describes this deployment and has nothing to do with forms:

  • 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 DatabaseSettings, 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.
  • The rest: host wiring, cross origin settings for development, the empty controller subclass, the demo account, and creating tables and loading sample data.

The front end side

The front ends have nothing to do with forms either:

  • 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.
  • 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.
  • 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 MenuSettings. Going from eight forms to eighty, this side does not change.

Where definitions are deliberately not used

  • Genuine business judgment: in the case study, only the status transition
  • 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
  • Deep screen customization: FormLayout describes how fields are arranged, not what a dashboard should look like

These are boundaries, not backlog items. The backlog is what the framework has not finished, such as the order number and cross row totals.


In closing

Seven of the eight forms have zero lines, and neither the application shell nor the front ends depend on how many forms there are.

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.

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.

The next post, Three Depths of Development and the Big Picture of the Framework, is the last one, gathering the thirty posts into a single big picture.


📚 Part of the thirty-post series ERP Architect's Notes: Designing a Definition-Driven Framework

📘 Original article on HackMD, in Traditional Chinese:
👉 https://hackmd.io/@jeff377/day-29-case-reconciliation

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

Top comments (0)