mellis481
mellis481

Reputation: 4148

MVC "model" terminology

In reading articles and working with MVC projects, I've found the term "model" used in a variety of ways and I'm hoping for some clarity in understanding what is meant by the terms below. Some may be synonyms. Some may have to do with Entity Framework and/or DDD.

I'm thankful for any input in defining what is (most likely) being referred to when these phrases are used.

Upvotes: 0

Views: 386

Answers (4)

CodeCaster
CodeCaster

Reputation: 151588

In ASP.NET MVC, the M stands for ViewModel. You use a Model to populate a View, and from a View your browser can post (data that resembles) a Model back to the Controller.

What logic you use to populate your ViewModel in your controllers is up to you. You can for example use Entity Framework with its own models and a tool like AutoMapper to fill your ViewModel with your database model's properties, or you could even use EF models as ViewModels.

Like said here:

"I’m building an MVC application" [means] in reality you are putting an MVC presentation layer onto your application

Upvotes: 0

Captain Kenpachi
Captain Kenpachi

Reputation: 7215

  • Model can refer to anything that serves as a conceptual model (bear with me). A *Data*Model is a conceptual model of what the database looks like.

  • View model. An entity that doesn't neatly map to an entity in your DataModel. Usually, you'll use a ViewModel when you want to add things like a list of selectable values for a dropdownlist. Or when you want to add extra information into the Model you're sending to your View. Or, less confusingly, a ViewModel is a custom item that is made up of different data entities and doesn't get saved to your database in the usual MyEntities.SaveChanges() fashion, but rather requires you to retrieve the bits you need to save.

  • Domain model. An extra layer between the DataModel and the Controller. A domain model could be most easily described as the cashier who takes your order at the drive-thru. You ask her for an item, and she decides what the details are and whether or not you are allowed to get that item.

  • Entity model. A set of classes that represent your database tables, views and stored procedure. When an MVC article talks about the Model, it is usually in reference to the Entity Model.

Upvotes: 3

Maheep
Maheep

Reputation: 5605

ViewModel: Used for defining properties/fields which are bound to some UI element like textbox, lists, labels etc.

Model: used for writing Front end processing logic like saving data, calculating fields values based on data which we get from below layers etc

Upvotes: -2

ShawnW.
ShawnW.

Reputation: 1852

Domain and Entity models 'typically' refer to data objects that are parallels to business objects; think "User" or "Car" or "Fruitcake", ie. something that you have a table in the database for. These are specific to your Domain/business, where Entities are single instances of those objects.

View models 'typically' refer to a collection of those models from above that makeup the information that you want to view/work with in your webpage/form/whatnot. So maybe you want a list of Users and what Cars they drive to the store to buy Fruitcakes. That collection of related data becomes a single "model" dealt with in the particular "view".

Model then becomes a very generic way of saying any of the above, but more nebulously it means a data object of some type that you need to deal with or that exposes the information that you want to work on.

Upvotes: 0

Related Questions