DDD and MVC: the difference between a "model" and an "entity"

I am seriously confused about the concept of Models in MVC. Most of the currently existing frameworks place the model between the controller and the database, and the model almost acts as a database abstraction layer. The concept of "Fat Model Skinny Controller" is lost as the controller starts more and more logic.

In DDD, there is also the concept of Domain Entity, which has a unique identifier for it. As far as I understand, a user is a good example of Entity (for example, a unique user identifier). An entity has a life cycle — values ​​can change throughout an action, and then are stored or discarded.

The entity described above is what I thought the model should be in MVC? How am I outside the base?

To clutter things up more, you drop other templates, such as a repository template (perhaps by placing a Service there). It's pretty clear how the Repository will interact with Entity - how does this happen with the model?

Controllers can have several models, which makes it look like a model that is smaller than a “database table” than a unique entity.

UPDATE: In this post, the Model is described as something with knowledge, and it can be singular or collection of objects. So this is more like an entity and a model, more or less the same. A model is an all-encompassing term where an entity is more specific. The Value object will also be a model. At least from the point of view of MVC. May be???

So, in very rude terms, which is better?

No "model" really ...

class MyController { public function index() { $repo = new PostRepository(); $posts = $repo->findAllByDateRange('within 30 days'); foreach($posts as $post) { echo $post->Author; } } } 

Or is it that has a model like DAO?

 class MyController { public function index() { $model = new PostModel(); // maybe this returns a PostRepository? $posts = $model->findAllByDateRange('within 30 days'); while($posts->getNext()) { echo $posts->Post->Author; } } } 

Both of these examples did not even do what I described above. I am clearly lost. Any input?

+44
php model-view-controller entity domain-driven-design model
Jun 12 '10 at 20:12
source share
5 answers

Entity

Entity means an object that is one element that business logic works with, or rather, those that have an identity of some type.
Thus, many people refer to ORM-related objects as objects.

Some call an “entity” a class, an instance of which is a single row in the database.

Some other people prefer to call only those of these classes as an “entity,” which also contain business rules, validation, and general behavior, while others call “data objects.”

Model

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

Basically, everything can be a model that matches the above.

MVC

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

<strong> Examples

  • The Customer class is an entity (usually), and you also use it as part of the data access in your application. In this case, it is both an object and a model.
  • The 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 the business logic layer but do not expose it to the rest of the application, it may be an object, but it is clearly not a model from the point of view of the MVC application.

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 abstraction of application data, and not a class that has a name, a suffix with "Model". Many people consider the latest viruses.

You can pretty much consider your repository class as part of your model, even if its name is not a suffix with “Model”.

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

+45
Jun 12 '10 at 20:45
source share

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

The model in DDD is very similar to the model in the real world: Simplification and abstraction of something. No less and no more. It has nothing to do with data, objects, or anything else. This is just the concept of a domain part. And also in each complex domain there is always more than one model, for example. Trade, billing, logistics.

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

A repository is not only a level 1 cache, but also part of a domain. This gives the illusion of objects in memory and is responsible for selecting Aggregates (not entities!) From anywhere and saving them i.e. maintaining the life cycle of objects.

If you are talking about DDD concepts, first improve your knowledge by reading the basics. Like ThinkDDD here.

+9
Feb 08 2018-11-11T00:
source share

The “model” in your application is the bit that stores your data. The “essence” in domain design, if I remember correctly, is a model with an identifier. In other words, an entity is a model that usually corresponds directly to a “physical” element in a database or file. I believe that DDD defines two types of models: one is an entity, the other is a value, which is just a model without and identity.

A repository template is just a type of indexed set of models / entities. So, for example, if your code wants to order number 13, he will first ask him for a repository, and if he cannot get it, he will go and deliver it from anywhere. This is basically a level 1 cache, if you like. There is no difference in how it works with the model and how it works with the entity, but since the idea of ​​the repository is to be able to retrieve models using their identifiers, from the point of view of DDD, in the repository.

+6
Jun 12 '10 at 20:57
source share

A simple solution using service and collection:

 <?php class MyController { public function index() { $postService = ServiceContainer::get('Post'); $postCollection = $postService->findAllByDateRange('within 30 days'); while($postCollection->getNext()) { echo $postCollection->current()->getAuthor(); } } } 

EDIT: A model (class) is a simple representation of an entity schema. A model (object) is a single entity. The service works with models and provides specific data to the s controller. No controller has a model. Models are autonomous.
Maps, on the other hand, map models to sustainability levels (e.g. databases, third-party backends, etc.).

+1
Jun 12 '10 at 20:20
source share

while this is especially true for Ruby on Rails, the same principles and information still apply as the discussion takes place around MVC and DDD.

http://blog.scottbellware.com/2010/06/no-domain-driven-design-in-rails.html

+1
Jun 12 '10 at 20:48
source share



All Articles