MVC and DAO - Data Rules

3

In a CRUD where in the act of registering you should check if a certain field already exists in the bank, in order not to allow duplicate registration, this verification rule must be in the DAO class (% with% if it exists in the act of registering ) or Controller (see via DAO that returns Exception or false before registration)?

    
asked by anonymous 28.09.2016 / 21:04

2 answers

3

It's always very controversial. Everyone has an opinion. Some people like the anemic model there they will talk to put in the controller. But this is a conceptual error. The correct one is in the template.

The controller will act as an intermediary for the view to speak with the model. The view is the consumer and will initiate the validation call.

All data intelligence must be in the model.

    
28.09.2016 / 21:30
2

At DAO no. No Model.

Conceptually speaking, since it is a set rule (a field can not be repeated between entities), the ideal is to define in Model , although not all frameworks have this feature.

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

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

If the framework does not have this capability, the Controller checks for the existence of the existing record with a given condition. Laravel, for example, invokes validation in Controller :

$this->validate($request, [
    'titulo' => 'required|unique:posts|max:255',
    'conteudo' => 'required',
]);
    
28.09.2016 / 21:28