Reputation: 538
Does the model in MVC contain both business logic (algorithms and stuff) and the classes that are mapped to entity tables in databases? Those mapped classes are called model as well, specifically, because they model some data. My confusion is this: Does model contain the business logic? or is it just entities? It turns out that it contains, from Mozilla docs: Model: Manages data and business logic.
I got confused by how Java Spring projects are structured. There are controllers, service (business logic), repository(connection to database, aka DAOs) and model classes (Classes of objects that are received by controller and often mapped to database entities). Let's map this to MVC "components":
View - Not in a spring app;
Controller - Rest controller (or just Controller, depending on how you want to structure your app);
Model - Services, Repositories, Model classes (???).
I am confused here, that we have model in both left and right sides.
Thanks.
Edit: Adding a snippet of code, and the structure of app.
This is how spring application looks usually. What most of people do.
.
├── BasicSpringAppApplication.java
├── controller
│ └── CustomerController.java
├── model
│ └── Customer.java
├── repository
│ └── CustomerRepository.java
└── service
└── CustomerService.java
This is how model/entity looks in a java file:
package com.example.basicspringapp.model;
public class Customer {
private String id;
private String name;
private String surname;
private int age;
//constructors, getters, setters
}
Look what I called (and what they call usually) a model, only a specific thing, an entity. But in MVC it's more than that! Model includes not only entities, but services and repositories as well, anything which is not a view and a controller.
Why MVC pattern is messed up in usual spring applications? It seems messed up for me, at least. Why people doesn't call those classes an entity or something like that? Since the model is more than that, for MVC.
Upvotes: 4
Views: 1542
Reputation: 481
The model in MVC-based applications:
In an MVC-based application, the domain model (or, short, the model) mirrors a certain business. It is programmed as a layer - the model layer, or the business layer - and is composed of multiple objects of various types:
Book
entities could be named BookCollection
, or Library
, or BooksRepository
and would contain methods used to handle a collection of objects of type Book
, like: "find/get/remove/update/store/exists/count/etc the book(s) in the book collection".So, in MVC-based applications:
By themselves, the entities are NOT mapped in any way to any persistence system (like a database). Their properties and methods mirror the specific business, by using a business-specific vocabulary, therefore beeing completely independent of the structure of the chosen persistence system. It is the responsibility of the data mappers - and theirs only! - to know about and map between the properties of the domain objects and the structure of the database records, for example.
In the first two resources below, at least, you'll discover that, in MVC-based applications, the persistence system (database, etc) does NOT determine the structure of the application. The persistence system is "just a detail".
Notes:
Resources:
Upvotes: 1