3

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.

Sandro J
  • 508
  • 3
  • 11
  • Hi. What do you mean by _"classes that are mapped to entity tables in databases"_ and by _"Classes of objects that are [...] mapped to database entities"_? More precisely, what are those _"entity tables"_ and _"database entities"_ that you are referring to? A bit of code would be helpful too. – PajuranCodes Nov 28 '21 at 22:26
  • @dakis Hey. The things you've mentioned mean the same thing. One example could be `Customer`, as an entity. If you have a website that keeps a list of customers and information about them, Customer would be a model class in Java, the would have some fields like `name`, `age`,`location` and so on. In hibernate I would declare it as an entity, in database I would create a table for it and it would have an id as a primary key. – Sandro J Nov 28 '21 at 22:40
  • I can provide an example as a code if it's still unclear what I refer those entity classes to. – Sandro J Nov 29 '21 at 07:27
  • Hi again. Yes, it would be clearer, if you could provide some code. – PajuranCodes Nov 29 '21 at 10:23
  • 1
    your class `Customer` in customer.java, if it is only having properties and is representation of your underlying persistence layer object/table, then it is usually called as Data Transfer Object(DTO)s. Model classes hold the logic of storing and retrieving data , and that data is shaped/projected as DTOs. Then your confusion on having model on both sides would not arise. – Anand Sowmithiran Nov 29 '21 at 13:38
  • @AnandSowmithiran Makes sense. But the thing is, people call DTOs models. In their apps models are only just those DTOs. I'm confused why 90% of people do this stupid thing? Why don't they call it a DTO or entity or similar? As the model is much more than just DTOs. – Sandro J Nov 29 '21 at 13:44
  • 1
    Spring (ab)uses famous pattern names for marketing. This is an effective marketing strategy, but is an egregious disservice to the development community as it pollutes our terminology. Combine that with all of the different flavors that extend MVC and partially overlap it, and you have a morass of words that are only used consistently within isolated contexts, but commonly conflated across different contexts. – jaco0646 Nov 30 '21 at 18:59

1 Answers1

1

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:

  • Data mappers: transfer data between entities and the chosen persistence space (database, session, remote repository accessible through SOAP, etc).
  • Repositories: transfer data between entities and the chosen persistence space, too, though by using data mappers from the data mapers layer. They build a layer additional to the data mappers layer, in order to hide the type of persistence space from their users. Each repository object is coded as a collection of entities of a certain type, accessible in a collection-like manner. So, for example, a collection of 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".
  • Entities, or domain objects: to hold the data and the behavior requested by the specific business. These components contain the business logic which is independent of the application in which they are used. In other words, they can be reused by multiple applications.
  • Value objects.
  • Services, as part of the service layer. These objects contain business logic as well, but, in comparison to entities, their business rules are dependent of the application in which they are used. Because of this fact, it is debatable if the service classes should be defined as part of the domain model, or of the delivery mechanism (see the first two resources below).

So, in MVC-based applications:

  • the model is not a class, but a layer of objects with different responsibilities;
  • the model contains business logic in both entities and services;
  • the domain objects contain both data and behavior.

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:

  • Injecting other components of the model layer beside services into controllers results in wrongly having business logic in them. The sole purpose of the controllers should be to delegate the processing of the user request to the service layer.
  • The advantages and disadvantages of the object-relational mapping systems (ORMs) are debatable.

Resources:

PajuranCodes
  • 303
  • 3
  • 12
  • 43
  • Thanks @dakis. But I think this is a general answer. I was interested in what happens in usual applications, that people mess up those things. – Sandro J Dec 20 '21 at 15:51