MVC and DAO - Data Rules

Asked

Viewed 472 times

4

In a CRUD where at the time of registration it is necessary to verify if a certain field already exists in the bank, in order not to allow duplicate registration, this rule of verification must be in the DAO class (Exception if it exists in the act of registration) or Controller (consult via DAO which returns true or false before registration)?

2 answers

3


This is always very controversial, everyone has an opinion. Some people like the anemic model then they will talk to put in the controller. But this is a conceptual error. The correct is in the model.

The controller will serve as an intermediary for the vision to talk to the model. The vision is the consumer and will initiate the validation call.

All data intelligence should be in the model, I think.

See more in In Ruby on Rails the business rule is on the model?.

  • But it turns out that all registrations are in the bank, not in Model. Model represents a single tuple..

  • Because the way I did, Controll talks to DAO and Model. and not Model talks to DAO.

  • What even generated this doubt, both with me and with other friends , if who talks with DAO is Model or Controll.. because a teacher uses one way and another way!

  • You would need to put it as it is done. The template is to be a representation of what is in DB. I don’t think it’s right (but there are people who think), so if it’s wrong, it makes little difference to me who does what. Sure you can do that and be right, but I don’t know, I haven’t seen anything like it.

  • But for that to be in Model I would have to define that the one who communicates with DAO would be Model, right? Because the way I did (I do not know if it is misuse) who sends the object to persistence via DAO is Control and not Model. I mean, Model doesn’t know that DAO exists.

  • That’s right and wrong depends on context. Just because someone said it does not mean it is one thing or the other. You would have to see the concrete case. Some people do as they say, some people are horrified by it.

  • But starting from the presupposition of his response and the response of the Gypsy, which it is up to Model to make such control, would be inevitable the relationship of the Model with the DAO, because this would not be possible via Control. Why would Model do such a control knowing that it represents only one instance? (I’m thinking about Java)

  • 1

    In my understanding yes. I think that if the controller talks to the DAO, it is not MVC.

Show 3 more comments

2

Not on DAO. On Model.

Conceptually speaking, because it is a set rule (a field cannot be repeated between entities), the ideal is to define itself in Model, though not all the frameworks have this recourse.

In ASP.NET MVC, for example, uniqueness can be defined as follows:

[Index(IsUnique = true)]
public String Nome { get; set; }

If the framework does not possess this ability, the verification of the already existence of the record with a certain condition can be made by the Controller. Laravel, for example, invokes validation in Controller:

$this->validate($request, [
    'titulo' => 'required|unique:posts|max:255',
    'conteudo' => 'required',
]);
  • But it turns out that all registrations are in the bank, not in Model. Model represents a single tuple..

  • 2

    Exactly. Speaking of theory, uniqueness is a question a little more technological than conceptual, so each framework implements this in its own way. When you say that "the registrations are in the bank", you are saying that the registrations are in the Model, because the Model is the system representation of a database. In MVC there is no separation between database and Model.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.