0

In MVC, as I know, the Model is the part related to data and database, it is basically related to data that is added, modified, displayed and so on...

To make sure of this I extract the following from Wikipedia:

A model stores data that is retrieved according to commands from the controller and displayed in the view.

And this from Chrome Developers:

Model is where the application’s data objects are stored. The model doesn’t know anything about views and controllers. When a model changes, typically it will notify its observers that a change has occurred.

So I was reading a little bit about CDI and I saw the @Model annotation (@Named + @RequestScoped) that is available under the package javax.enterprise.inject with the following description:

/**
 * <p>
 * The built-in stereotype intended for use with beans that define the model layer of an MVC web application architecture such
 * as JSF.
 * </p>
 * 
 * @see javax.enterprise.inject.Stereotype
 * @author Gavin King
 */

As the java doc states, this should be used on the model layer of an MVC web application like JSF, but I cannot have an idea about how a @Named @RequestScoped bean can represent the Model layer.

If I can ask the question in a different way, how can (and why) a @Named @RequestScoped bean be used let's say for accessing the database?

If it's not possible or a good practice or even not logical then isn't the @Model name a bad choice for a @Named and @RequestScoped bean? (I would avoid using it in order not to confuse anyone reading the code)

Ali Bassam
  • 9,691
  • 23
  • 67
  • 117
  • 1
    Did you read the [FAQ](https://github.com/cdi-spec/cdi-spec.org/blob/master/_faq/core/101-what-is-the-purpose-of-the-model-annotation.asciidoc)? – Robby Cornelissen Dec 14 '17 at 04:53
  • @RobbyCornelissen Yes. Yet I still see the name to be confusing. – Ali Bassam Dec 14 '17 at 04:56
  • 1
    Maybe your understanding of a model is a little too restrictive? A view model is also a model, and models are typically not used for *accessing the database*, as you state. If not `@Model`, what would you call it? – Robby Cornelissen Dec 14 '17 at 05:04
  • @RobbyCornelissen I didn't state I was asking actually... but too restrictive, yes I assume so. But is it a common practice to have a short-lived view model? – Ali Bassam Dec 14 '17 at 05:17

1 Answers1

0

Well this has gone a long time with no answer, but I'll try to give you a key.

if you look at JSF as mvc then V is the Facelets files, C is the JSF Servlet, then M is what gives data to the view, and those are the backing beans, a model usually is request scope in those designs, look at ASP.Net MVC every thing is request scoped.

Any way, in javaee configuration JSF as a whole acts as VIEW for me, there is a model and a controller behind it, being CDI transactional beans or EJBs and a database or Rest end point, so don't take MVC in such a restrictive understanding.

The folowing answer contains a much better and detailed explanation of JSF components form Bauke Scholtz, so check it out.

https://stackoverflow.com/a/5104604/4649524

alibttb
  • 526
  • 4
  • 19