Lets say we want to model the object we retrieved from our backend. We want to modify some data, concatenate other and we want to add some functions so we can call them on the fly at presentation layer. Lets assume our e-commerce site’s backend just retrieved a saved shopping cart (a la amazon). It contains a saved shopping cart holding product with a title, description, quantity, price, taxes, fees, etc.
Step by Step
1- Our Controller will be just a pass through. It will only call our service and make sure that the model we obtain we attach it to our scope so the presentation layer has access to it.
Notes: For debugging purposes we made an additional call to our svc so we can compare the unmodified model we got from our backend towards the one we constructed for our ui.
2- Our Service offers a small set of public methods - our API. Public function ‘retrieveMyModel’ calls by reference ‘fetchModelFromBackendAndConstructIdealObject’.
A little bit hairy method name but it makes a point about what its responsibility is. We are faking the call to a backend service and we always return the same fakeModel. No massaging so far.
3- Now we add a class with a constructor. This complex object ‘UiExpectedModel’ will serve as a blueprint to create a more suitable object with only the necessary data for the ui.
It will also contain some convenience methods that fall under the same domain and that will turn our Model smarter.