codedude
codedude

Reputation: 6519

What is the "model" in the MVC pattern?

So I did some Google searching on the MVC pattern and I'm still not exactly sure what the "Model" part is. What exactly does it deal with? I'm rather new to programming so all the explanations I can find go right over my head. I'd really appreciate it if you could give me an explanation in simple terms.

Thanks

Upvotes: 2

Views: 476

Answers (5)

Maess
Maess

Reputation: 4146

The model is used to represent the data you are working with. The controller controls the data flow and actions that can be taken with the data. The view(s) visualize the data and the actions that can be requested of the controller.

Simple example:

A car is a model, it has properties that represent a car (wheels, engine, etc).

The controller defines the actions that can be taken on the car: view, edit, create, or even actions like buy and sell.

The controller passes the data to a view which both displays the data and sometimes lets the user take action on that data. However, the requested action is actually handled by the controller.

Upvotes: 0

JohnnyK
JohnnyK

Reputation: 1102

The simplest way I can describe it is to call it the "Data" part. If it has to deal with getting or saving data, it's in the model. If you have a web application, the model is usually where you interact with a database or a filesystem.

Upvotes: 8

David Houde
David Houde

Reputation: 4778

The model is respomnsible for managing the data in the application. This might include stuff like database queries and file IO.

the view is obviously the template, with controller being the business logic.

Upvotes: 0

Didaxis
Didaxis

Reputation: 8746

The Model can represent your "Domain Model" in smaller projects. The Domain Model consists of classes which represent the real-world entities of the problem you're dealing with.

In larger projects, the Domain Model should be separated out of the actual MVC application, and given it's own project/assembly. In these larger projects, reserve the "Model" (i.e. the "Models folder in the MVC project") for UI presentation objects (DTOs - Data Transfer Objects)

Upvotes: 0

Boris Pavlović
Boris Pavlović

Reputation: 64632

Model in MVC is a place where data presented by the UI is located. Therefore, it shouldn't be confused with Domain Model which serves as a skeleton that holds the business logic.

Certainly, for a small application that serves as a service for CRUD operations backed by a database these two models can be the same. In a real world application they should be cleanly separated.

Controller is the one who talks to the application services and the Domain Model. It receives updates back from application services updating the Model which is then rendered by the View.

View renders the state hold by the Model, interprets User's input and it redirects it to the Controller. Controller then decides if the Model is going to be updated immediately or first the information is forwarded to application services.

Upvotes: 0

Related Questions