DDD and MVC: Difference between 'Model' and 'Entity'

Entity

Entity means an object that is a single item that the business logic works with, more specifically those which have an identity of some sort.
Thus, many people refer to ORM-mapped objects as entities.

Some refer to as "entity" to a class an instance of which represents a single row in a database.

Some other people prefer to call only those of these classes as "entity" which also contain business rules, validation, and general behaviour, and they call the others as "data transfer objects".

Model

A Model is something that is not directly related to the UI (=View) and control flow (=Controller) of an application, but rather about the way how data access and the main data abstraction of the application works.

Basically, anything can be a model that fits the above.

MVC

You can use entities as your models in MVC. They mean two different things, but the same classes can be called both.

Examples

  • A Customer class is very much an entity (usually), and you also use it as part of data access in your app. It is both an entity and a model in this case.
  • A Repository class may be part of the Model, but it is clearly not an entity.
  • If there is a class that you use in the middle of your business logic layer but don't expose to the rest of the application, it may be an entity, but it is clearly not a Model from the perspective of the MVC app.

Your example

As for your code examples, I would prefer the first one.
A Model is a class that is used as a means of data abstaction of an application, not a class which has a name suffixed with "Model". Many people consider the latter bloatware.

You can pretty much consider your Repository class as part of your model, even if its name isn't suffixed with "Model".

I would add to that the fact that it is also easier to work with the first one, and for other people who later may have to understand your code, it is easier to understand.


The "model" in your application is the bit which holds your data. The "entity" in domain-driven design is, if I remember correctly, a model with an identity. That is to say, an entity is a model which usually corresponds directly to a "physical" element in a database or file. I believe DDD defines two types of models, one being the entity, the other being the value, which is just a model without and identity.

The Repository pattern is just a type of indexed collection of models/entities. So for instance if your code wants order #13, it will first ask the repository for it, and if it can't get it from there, it will go and fetch it from wherever. It's basically a level 1 cache if you will. There is no difference in how it acts with a model, and how it acts with an entity, but since the idea of a repository is to be able to fetch models using their IDs, in terms of DDD, only entities would be allowed into the repository.


All answers are a heavy mashup of different things and simply wrong.

A model in DDD is much like a model in the real world: A simplification and abstraction of something. No less and no more. It has nothing to do with data nor objects or anything else. It's simply the concept of a domain part. And in also every complex domain there is always more than one model, e.g. Trading, Invoicing, Logistics.

An entity is not a "model with identity" but simply an object with identity.

A repository is not just a 1st level cache but a part of the domain too. It is giving an illusion of in-memory objects and responsible for fetching Aggregates (not entities!) from anywhere and saving them i.e. maintaining the life cycle of objects.