Modernization ·Case
Same business, updated stack: move the rules first, then the screens
A manufacturing execution system's Windows Forms client moved to WPF
A desktop client used all day on plant floors had its business rules inside its Windows Forms screens, so changing one screen put another at risk, and no rule could be tested on its own. The plants could not stop for a rewrite; some could stop for about an hour. We kept the old shell, replaced the screens one at a time with WPF components hosted inside it, and moved each screen's rules into the product's API as the screen moved. The shell itself moved last. Every screen became WPF, one per update, with no single switch-over, and the rules ended up in one place, behind the API.
- Problem
- a desktop client used all day on the plant floor had its business rules inside its Windows Forms screens, so a change to one screen risked another, and the rules could not be tested or shared.
- Deciding constraint
- the plants could not stop for a rewrite, and some could stop for only about an hour for a release.
- Choice
- keep the Windows Forms shell, replace the screens one at a time with WPF components inside it, move each screen's business rules into a .NET API as it moves, and leave the screens only visual logic (MVVM); move the shell last.
- Result
- every screen became WPF and then the shell did too, one screen per release, with no single switch-over; the rules ended in one place behind the API instead of in each screen.
If this is your situation
If a desktop application runs daily work and every change to a screen breaks another, start by listing which rules live inside which screens, before choosing any new technology.
Modernization → discuss a legacy product
For your technical team How it was diagnosed, the options rejected and why, what went wrong, and the decision record.
Context and scope
A manufacturing execution system covers production, quality and traceability on the plant floor: picking, stock, scheduling, production, quality control, labelling and shipping. This one was a product sold to about 20 manufacturing clients, each running it in its own plants, with 10 to 30 users per plant. The desktop client was built in Windows Forms over SQL Server, and much of what the business needed, validations, calculations, the order of steps, lived in the screens' code.
The work was to move it to WPF, and to take the business rules out of the screens in the process. The product already had a .NET API, used for integration with other systems; it was extended to carry the business rules, and the new screens call it. The same business, the same database, an updated stack.
In scope: how the client and its rules were moved while plants kept working. Out of scope: the product's features, and the database, which stayed.
Drivers
Constraints
- The client runs production: plants work all day, and some could stop for only about an hour for a release.
- Material traceability had to stay intact through every release.
- The rules were in the screens' code, written down nowhere else.
Scenarios
- When a screen moves, its users do the same work the same way, on the same data.
- When a rule moves into the API, it gives the same result it gave inside the old screen.
- When a release goes out, it fits the plant's stop window and traceability holds.
- When the move is done, no screen holds a business rule: it shows data and sends the user's actions to the API.
Options against those drivers
| Option | Rules in one place | Plants keep working | Size of each release | Main drawback |
|---|---|---|---|---|
| Rewrite, switch at once | Yes, if found | No | Everything | A critical day at every plant |
| Port screens, rules stay | No | Yes | One screen | The same problems in new screens |
| Shell kept, screen by screen, rules to the API | Yes | Yes | One screen and its rules | Two toolkits in one client for a period |
The decision and how it works
The shell stays until last. The Windows Forms shell, the window, menus and navigation users
know, kept running, and moved screens were WPF components hosted inside it with ElementHost, the
Windows Forms control made for that. Users met one new screen at a time in a client they already
knew, not a new application.
A screen moves with its rules. When a screen was rewritten in WPF, the business rules in its old code went into the .NET API, and the new screen kept only visual logic: what to show, what to enable, what to send. That is the MVVM split: the view and its view model handle presentation, and the decision is the API's. A screen moved without its rules would have been a new toolkit over the old problem.
The API grows instead of starting over. Extending the integration API meant one place for the rules, whether a screen or another system was calling, and no second service to deploy.
The shell moves when nothing is left inside it. Once every screen was WPF, the shell itself was moved to WPF, which completed the move. At that point the client was presentation only, and the rules sat behind one API.
Stored procedures keep what must be one transaction. Changes that had to succeed or fail together stayed in stored procedures, which the API calls. The rest of the business logic in them was extracted into C# classes behind the API wherever that was possible, so it could be tested and versioned with the code. Moving a procedure's logic into application code can turn one statement into one query per row, so time it and read its plan both ways before deciding ([The system is slow. Where, exactly?]).
How the move is made, and how it is undone
One screen at a time, each one a release sized to fit a plant's stop window, with the automated tests and the pre-release scripts run before it. Old and new screens use the same database, so nothing has to be synchronised, unlike a move between two databases ([Replacing a live system one role at a time]). Keep a replaced screen's old form reachable until its replacement has been through a full production cycle; then remove it, so no rule lives in two places.
How the decision is kept true
The product had automated tests across it, written by the whole team, and scripts that ran them and built the packages before each release. With rules behind an API, those rules can be tested without a screen. Nothing was measured before and after, which is the part I would change: today I would add a characterisation test for each rule as it leaves a screen, built from the old screen's outputs on real inputs, and count the rules still inside screens as the measure of progress.
Where it went wrong, and the risks that remain
The boundary between the two toolkits leaked. ElementHost puts a WPF screen inside a Windows
Forms window, but the two toolkits handle the keyboard differently. Focus was lost when a user's
work was interrupted, and the tab key did not move cleanly between Windows Forms and WPF controls.
For users on a plant floor who rely on the keyboard, that is not cosmetic: a user who tabs through a form all
day notices at once.
Focus was restored by setting it only once WPF had finished handling the interruption, queued at the dispatcher's idle priority instead of called at once:
Dispatcher.BeginInvoke(DispatcherPriority.ContextIdle, new Action(() =>
{
myTextBox.Focus();
}));
Tab order needed two fixes. Overriding OnNoMoreTabStops in the hosted form or user control lets
the tab key leave it and continue in the other toolkit. A Windows Forms form opened from WPF also
needs WindowsFormsHost.EnableWindowsFormsInterop(), called in the WPF constructor (it needs a
reference to WindowsFormsIntegration.dll); that is a partial fix, and only a form shown with
ShowDialog() has full keyboard support. Test focus and tab order at the boundary on every moved
screen, with the users' real sequences, and keep the time the shell runs both toolkits short.
| Risk | Likelihood | Impact | Mitigation | Owner |
|---|---|---|---|---|
| New screens, old rules | High | The same defects in a new toolkit | Move each screen's rules into the API with it | Tech lead |
| One rule live in two places during the move | High | Results that disagree | Remove the old form once its replacement holds | Developer |
| Screens that share state cannot move alone | Medium | A screen that cannot be released on its own | Move the shared state into the API first | Developer |
| Focus and tab order break at the toolkit boundary | High while the shell is mixed | Keyboard users slowed or lost mid-task | Focus queued at idle priority; OnNoMoreTabStops; interop enabled for forms opened from WPF; tested on every hosted screen |
Developer |
| A release larger than the stop window | Medium | A plant stopped longer than agreed | One screen per release; scripted tests and packaging | Team lead |
Operations, cost and scale boundary
- To run: the API, the client and the database.
- To learn: the rules hidden in the old screens, which is the larger job, and MVVM.
- Cost: a client with two toolkits for the length of the move.
- Scale boundary: none claimed; the case is about where rules live, not throughput.
- Where the choice flips: with a small client, no local devices and a plant that can stop for a day, a direct rewrite is cheaper than moving screen by screen.
On a different stack
Keeping the shell, moving screen by screen and taking the rules out in the process do not depend on the technology: VB6, Delphi and Access move the same way, as long as the old shell can host the new screens. Today the API would be .NET with EF Core; it could be any language the team runs.
Modernization → discuss a legacy product