views:

322

answers:

5
+2  Q: 

The Model in MVC

Hi,

I am just starting on ASP.NET MVC trying to understand the philosophy first. I think I am pretty clear on the roles played by the controller and the view but I am a little confused on the model part. Some sources say its the domain model, some say its the data model, some say its the objects that are bound to the view.

IMHO these are very different things. So please can someone clear this up once and for all?

+9  A: 

The model is "the domain-specific representation of the information on which the application operates". It's not just the data model, as that's a lower level than the MVC pattern thinks about, but (for example) it's the classes that encapsulate the data, and let you perform processing on them.

Scott Guthrie from MS uses this definition in his announcement:

"Models" in a MVC based application are the components of the application that are responsible for maintaining state. Often this state is persisted inside a database (for example: we might have a Product class that is used to represent order data from the Products table inside SQL).

Further reading:

crb
beat me to it on Wikipedia.
Can Berk Güder
Who/What manipulates and operates on the state if the model exists only to maintain state. I hope you don't say controller or else I'll be confused about them as well :|
Ali Kazmi
"Controller: Processes and responds to events (typically user actions) and may indirectly invoke changes on the model." The user invokes an action and that will change the model, causing an update to the view.
crb
+1  A: 

You should have a look at this it a step by step tutorial.

From one of the chapters: page 26

In a model-view-controller framework the term “model” refers to the objects that represent the data of the application, as well as the corresponding domain logic that integrates validation and business rules with it. The model is in many ways the “heart” of an MVC-based application, and as we’ll see later fundamentally drives the behavior of it.

Hope its useful.

MrHus
+6  A: 

I like to actually add an additional layer to make things clearer. Basically, the "Model" is the thing that is domain specific, and knows how to persist itself (assuming persistence is part of the domain).

IMO, the other layer I referred to I call the ViewModel ... sometimes, the "model" that gets passed to the view really has nothing to do with the domain ... it will have things like validation information, user display info, lookup list values for displaying in the view.

I think that's the disconnect you're having :-)

Joel Martinez
A: 

For example, if you're building a web site to, say, manage operations of a nuclear plant, than the model is the model of the plant, complete with properties for current operating parameters (temperature etc.), methods to start/stop power generation, etc. Mmmm... in this case the model is a actually a projection of a real plant vs. an isolated mode but you got the idea.

zvolkov
+4  A: 

Your sources of advice are correct when they say it is the domain model. In many instances, it will be quite closely aligned your data model as well.

Where the domain and data models differ is that the data model is relatively static in form (not content) whereas your domain model adds the specific constraints and rules of your domain. For example, in my data model (database) I represent blood pressure as smallints (systolic and diastolic). In my domain model, I have a "blood pressure reading" object that holds values for each of the two readings and that also imposes additional restrictions on the range of acceptable values (e.g. the range for systolic is much smaller than that for smallints). It also adds qualitative judgments on these values (a BP of 150/90 is "high").

The addition of these aspects of the problem domain is what makes the domain model more than just the data model. In some domains (e.g. those that would be better rendered with a fully object-oriented data model and that map poorly on the relational model) you'll find that the two diverge quite significantly. However, all the systems I've created feature a very high degree of overlap. Indeed, I often push a fair number of domain constraints into the data model itself via stored procedures, user-defined types, etc.

Mark Brittingham