Miguel Ferreira
Miguel Ferreira

Reputation: 425

How to differentiate between Model and Entity?

I have already read A LOT but I still struggle to understand the question of this post. I am building (learning how to build) my own web application using .Net Core Framework, and also I'm using MVVM architecture, and this terms are crucial for me to understand what I am doing, namely, the Entity Framework. If someone could explain them to me with some examples, it would be perfect. Thank you in advance guys.

Upvotes: 4

Views: 3930

Answers (3)

Miguel Ferreira
Miguel Ferreira

Reputation: 425

Ok so through the answers given and with further reading + outside help I have reached my own conclusions:

Entity

As Stefan said, it is a Table in a database. It is a .Net object that is represented in a data base. For example, a Person class with the fields firstName and lastName will be a table in a database with the columns firstName and lastName.

Model

Things become spicy here. This is an object that contains the data that will populate a view (for example, may be a JSON that angular will use to generate an HTML, when using MVVM arquitecture). Another very important topic to mention is that the model is created with: entities (one or many entities may be used to create the final model) plus business logic applied by the application(although it is not mandatory). The application may only send "us" part of the total data which is avaiable to it. Take an example: Facebook. When I request my profile, it contains My name, a photo of myself, etc. But it does not contain, for example, my ads preferences! So my application (applying business logic) will cross information and generate the Model, which can also be viewed as a contract to be honored, containing certain data that MUST be there in order to applications in the client side function correctly (for example, some objects in a JSON file must be there) so Angular constructs the HTML (View) with the data from the model.

Upvotes: 2

Stefan
Stefan

Reputation: 17678

Within MVVM you have 3 components:

  • the model
  • the view (speaks for itself)
  • the viewmodel

In entity framework, you have:

  • entities

So how does it relates?

The Entity

In general it's "a thing" which has a right to exist. Within a EF context this is often referred to as a table.

ViewModel

It's a model, tailor made for a view. Ideally it contains a set of properties and some commands. Through binding, you can update your view by setting properties. The text of a label for example.

The Model

Now the fun starts; the Model is an object, possibly containing data and some of the business logic.

Basically, this could be an entity, but it doesn't necessarily have to be. As a matter in fact; depending on the size of your application don't (or do) want to mix your data layer with your business logic.

Wikipedia states it beautifully:

Model refers either to a domain model, which represents real state content (an object-oriented approach), or to the data access layer, which represents content (a data-centric approach)

So, the entity can be your model, but in larger applications there is quite often a layer in between to separate the "business" language from the data layer language.


Note; if you put an API on top of things, you might also want to dig into the DTO.

Upvotes: 4

TomTom
TomTom

Reputation: 62157

There is a simple thing here.

A model is a model for a view etc. - they often contain only partial data compared to entities, data from multiple entities or additional fields that are just there to be output. I.e. a model may have a collection of possible values for another property so that the view can then show a dropdown to select a value.

An Entity (in Ef terminology) represents (simply said) data in a table or view (though they can be a little different). There is no concern about presentation here.

Upvotes: 4

Related Questions