Java EE - What is the correct layer for formatting domain model objects before passing them to views?

一曲冷凌霜 提交于 2019-12-10 22:25:49

问题


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 show 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.


回答1:


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.


来源:https://stackoverflow.com/questions/6685043/java-ee-what-is-the-correct-layer-for-formatting-domain-model-objects-before-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!