Search code examples
model-view-controllerspringformattingdatamodel

Spring Formatters inside data model, is this a violation of MVC? Is there a better alternative?


Spring provides formatters (and converters) with the use of annotations. This means that a request parameter can be annotated on a controller to format user input and also a property of a data model class can be annotated to format data for a view.

The latter seems to me as a clear violation of MVC's main purpose, that is the separation between model and view. Annotating a data model class with formatting specifics binds the model to the view. If the model hast to be used for some other view or for anything else, it can't, since its fields are formatted for a specific view.

If I am wrong let me know. If not, is there a way to format fields from and to view format without violating MVC?


Solution

  • Using annotations is a good declarative way to specify formatting - I would not give it up. An alternative would be a procedural way of defining conversions.

    As I see it, the problem is that pure model objects and form-backing objects get mixed together. If you want to "purify" your architecture, introduce form-backing objects that will be between user's input and your model. If you think that it is too complex for the scale of your project, then you don't need it. In this case, just be aware that the model object has a double meaning.