In my web application that uses an MVC framework which has different modules for models, views and controllers I talk to several databases and APIs. Those are implemented as individual models.
A lot of data gets entered by the user accross several different screens. That data goes into the session. Some of that data is meta information that validates the success of the process, and some of it is stuff that I want to keep. That data is supposed to be written to a file in the file system, and the path of that file with some meta-information is stored in a database. After that, a confirmation page will be displayed to the user.
I am now struggling with where to put the writing of the subset of the data that has accumulated in the session to the file in my specific format.
There are several thoughts I have on the matter. I am not sure which one is the most right.
It should be a View because it takes data that is already there in the user's session and presents it in a specific way – the file format (which is XML, but that is not relevant). Formatting and writing the file is implemented there.
Reasoning: The default HTML view that renders data as a website to the user's browser has the webserver's interface set as its STDOUT channel. Likewise would a JSON view that presents stuff in case an API call is made. If we write to a file, the STDOUT of the view that formats the file is set to a local file handle and we write there.
There should be a View to bring the data into my XML format, but a Model to write the data into the file.
Reasoning: Because after writing the file, another website will be displayed. Only one view can be at the end of the chain of things that do stuff in the lifetime of one request handling. But because data is being formatted, a view is appropriate. It just returns the formatted data rather than writing it to a sink (STDOUT).
It should be a Model, because it deals with data.
Reasoning: Models are data sources and data sinks. Although the source-part is absent here there is still a data sink. The fact that it needs to be formatted also is neglegible because if we'd talk to e.g. a RESTful API of some sort we would also first format the data to be either part of a GET request (which is very simple) or maybe a JSON representation as the body of a POST/PUT request.
The code that formats the data into XML is already built as a stand-alone class that is not tied to the webapp yet.
My question is this: Where in the application should I use that class and write the file to disk so I do not break the MVC pattern?
Example cases where this specific process would happen include:
- a user feedback/survey form with multiple pages, like the Stack Overflow anual user survey,
- an e-commerce order funnel,
- back-office data entry that talks to a third party through a file-based API where the "sending" happens asynchronously at a later, unrelated time