Java EE - What is the correct layer for formatting domain model objects before passing them to views?
I am developing a web application which has a typical layered architecture:
a DAO layer that retrieves domain model objects from a database;
this layer communicates with the service layer which does some business operations using those objects;
the web layer (Spring Controllers) use the service layer to retrieve the domain model objects (or collections for them) and pass them to the view layer;
the view layer is either simple JSPs which s开发者_开发技巧how the data using JSTL, or JSPs which retrieve some of the data through AJAX in the form of JSON objects (domain objects converted to JSON through Jackson library).
I have been researching about the following:
Very often I need to convert the db fields to a different format to show to the user. For example, a Date might be store as a Timestamp and I want it to show it as a formatted date (e.g. dd/mm/yyyy).
Also, I need to do the opposite, convert some value (usually user input) to the format of a domain model object's property.
My question is, where should I be doing this kind of conversions? Especially with JSON data, they should be already formatted on the AJAX response, I dont think I should format it with Javascript, am I right?
Thank you in advance.
Very often I need to convert the db fields to a different format to show to the user. For example, a Date might be store as a Timestamp and I want it to show it as a formatted date (e.g. dd/mm/yyyy).
Also, I need to do the opposite, convert some value (usually user input) to the format of a domain model object's property.
My question is, where should I be doing this kind of conversions?
My opinion is that all data format conversions ought to be done in the view, or in collaborative objects. The model or the service layer should not be performing this activity. It would be poor design to perform this in the model, for it would couple the interfaces exposed by the model to only one view. Also, it would make it more difficult to make changes in the view, requiring changes in your model as well.
You can refer to well-written JSF applications (since you mentioned that you are using Java EE), where specific converters are written to ensure that the view would format the contents of the domain objects appropriately.
Especially with JSON data, they should be already formatted on the AJAX response, I dont think I should format it with Javascript, am I right?
That would depend on how you view the view. If your representation of the view is that the server must provide the client with formatted data, then the server ought to do this. But, most applications take the practical approach where in client-side logic is also treated as part of the view. Depending on the context in which the data is used, you may:
- either format the data on the server, especially if the formatted data is used universally at the client side without any further modifications.
- or, transmit data to the client in a canonical form, which is then formatted on the context in which the data used and/or is displayed to the user.
精彩评论