First Attempt: Layered & Contract Driven Software Architecture

Situation:
For Super-CPQ I want to display Product Details in a nice LWC.

Old Attempt:
LWC Controller calls “APEX controller Class”. Controller gets Product Details from Database transforms it and returns the Objects Product and Pricebook as type sObject to LWC.
The LWC Controller does some more transformation. 

Problem:
Every time I need a change it was a game of hunting the right place to change, adding a bunch of if/else and squashing bugs.
Even my small project was no fun anymore to work on 🙁

New Attempt: Contracts & Layers

Contracts:
Starting with two specific contracts between the layers:
“Product Detail Wrapper” & “GetProductDetails_Interface”

1) Product Detail Wrapper
The wrapper does not accept sObjects but is defined as specific fields. This way every layer can trust the other layer that all the promised fields are there.
Lesson learned:
Using sObjects in the wrapper means that the SOQL query in the database controls the contract which I don’t want to have.
The other layers should not care about the “Database Layer”.

2) GetProductDetails_Interface
The simple GetProductDetails_Interface allows my Service Layer to not care about the Database layer implementation but just trust it.

Layers:
Instead of having everything in my APEX Controller and JS Controller almost randomly, I defined the layers first.

There’s the View (LWC HTML), the Controller (LWC JS), the Service Layer, determining to which “GetProductDetails_Interface implementation to go depending on the Use Case and finally the Database Layer.
The JS Controller does the transformation from the “Product Detail Wrapper” to something the UI can use.
The database layer does the heavy (and boring) transformation from SOQL to the “Product Detail Wrapper”

Advantage:
I wanted a completely new UI. Since everything was nicely decoupled and I could trust the contracts, nothing in the Service & Database Layer needed to be changed.

Disclaimer: I’m just getting started at this, every feedback is highly welcome! The code I’m showing is just examples, the real implementation is more complex.

PS: Thank you so much Matthew Gerry for opening my eyes to the wonderful world of Software Architecture