In my experience, the Model in MVC is often not really a single layer. I would regularly have backend models and frontend models, where the backend ones would have properties that I want to hide from the UI code, and the frontend ones have derived or composite properties which are more meaningful in the context of the UI.Recently, I have started to introduce a third layer in between when database normalization created tables that did not really match the conceptual domain objects anymore. In those projects I have model classes that are equivalent with the DB structure, these become components of objects that represent the domain model, and finally these are filtered and amended for displaying information to the user.Are there patterns or guidelines for how, when and why to organize the data model across application layers?
How many tiers should my models have in a DB driven web app?
design
In node.js I generally use a only three layers.Data layer:A simple layer that talks directly to the datasource. It's only job is efficiently extracting data from the source and writing to it. The datasource can be anything, a TCP endpoint, a HTTP endpoint, a database, a filesystem, etc.Domain layer:The domain object layer. This is similar to your M in MVC. It's an object representation of a domain object. It has methods for construction, manipulation, validation and other logic.Note that a single domain object can talk to several different objects from the data layer and that several views can transform a domain object.View layer:The view takes a domain object and transforms in an easily printable way. For example, in the view layer I would convert a timestamp to a human readable string or do my i18n language conversion.It also does any other view logic. Generally you have multiple view objects for a single domain object.And of course it also converts data from the domain object into a format you want, whether it's binary, xml, json, html, some arbitary TCP protocol, plain text, csv, etc.IO LayerYou generally also need some form of IO layer that talks to the other layers. i.e. the IO layerunpacks inputcalls the correct domain object from either data or a domain object instancepasses the data/domain instance through the view layerpipes the raw information returned from the view layer (html/json/binary/...) down the outputMultiple applicationsNow a single application has these three layers. If I had a dynamic website then I would have these three layers on the server and these three layers on the client.If I had my own unique data source to talk to then that remote data source would also use these three layers, it would have it's own domain objects, it's own data layer on it's own data sources and it's own view layer which other services talk to when they use me as a data source.Pretty pictureImage Source