That would be a 3-layer project?
Yes, you defined 3 layers: data, business and presentation.
I can consider that in an MVC project, the only difference is the exchange of BLL for a Controller?
No. It’s common to think that, but it goes a little further.
First of all, we need to see things from the point of view of a Model. A Model is a class that defines not only the data elements, but what values they can receive, how they are validated and the relations of a Model with another, which does not exist in the 3-Layer Model.
In the 3 Layers, you need to put validations, relationships and characteristics of each entity or in the data layer, or in the business layer.
An example of Model:
public class Product
{
[Key] // Aqui uso um atributo para definir que a propriedade a seguir é chave.
public int ProductId { get; set; }
public int CategoryId { get; set; }
[Required(ErrorMessage = "O Nome é obrigatório.")] // Aqui defini que o campo não pode ser vazio, juntamente com a mensagem de erro que deve ser exibida caso a validação falhe.
public String Name { get; set; }
// Aqui defini que um produto pertence a uma, e apenas uma categoria.
public virtual Category Category { get; set; }
// Aqui defini que um produto pertence a várias compras.
public virtual ICollection<Purchase> Purchases { get; set; }
}
Secondly, the responsibility of a Controller is to harmonise and arbitrate relations between Models. It is he who commands the creation, modification, deletion and selection of application data. In addition, it is he who receives the request and decides what should be returned as presentation, such as the data format (HTML, JSON, and so on).
There are approaches that seek to put an extra layer to work together with the Controller, on the grounds that it is not the responsibility of Controller to take care of business rules. This is not true if it is seen as something that he is responsible for doing, in this case, the harmonization of data between Models. In a project that uses Entity Framework, this:
var user = context.Users.Find(userId);
var profile = new Profile { User = user };
context.Profiles.Add(profile);
context.SaveChanges();
That is, the creation of an object, the assignment of information to the object and the command to pass the object to a data context is part of the harmonization between Models, but it’s business rule.
I can consider this project as 3 layers?
Roughly, yes. What you did was insert a layer of "Template" (which is not really "Template", but a layer of Domain, which is another concept). This "Model" layer can be seen by the presentation because it is only information of what the data transfer objects will be like. It is as if it were a pre-established contract between the three layers. If there were no business rules or peculiar behaviors, you would just be following the DDD standard.
In my example I define the class, in the data layer. Then in the presentation layer I use this class. This is not wrong? Because the presentation layer can’t communicate with the data layer, right?
– Felipe
I don’t understand. I’m referring to the 3 layers.
– Felipe
I see in some examples on the internet, using a layer called DTO.
– Felipe
Talk nonsense. Let me comment again.
– Leonel Sanches da Silva
So if you want to use the Student object in the presentation, you need to define a 4th layer called Domain, containing the representation of each DTO, into another DLL separately.
– Leonel Sanches da Silva
But by 3 layers, I cannot use the Student in the presentation?
– Felipe
Yes, you can. As I said, if you’re in a separate DLL from the other 3 layers.
– Leonel Sanches da Silva
Imagining that I have a select that brings all students, a List<Student>, as the presentation layer treats it, being that it does not "know" Student?
– Felipe
I added an image to the question...
– Felipe
@Felipe I will complement the answer with this your image.
– Leonel Sanches da Silva