DDD ( Domain Driven Design ), It is a set of basic and practical patterns that help us to solve and understand the business problems (Domain) in the design of object-oriented systems. DDD is a very broad topic.
I have been using the N - Layers architecture from Windows Forms, but in ASP.NET MVC I have a doubt since I am a beginner working on web systems.
Let's focus on the DDD architecture.
We go straight to the problem that I have. Starting from the entities of the domain are objects that have an identity and are important within the business logic of our application, but not they have to be known directly by other layers of the application.
This is the structure of my project.
I am using DTOs or Data Transfer Objects are classes whose main purpose is to simplify the objects that are to be exchanged between processes.
In Application Layer , in which I have two projects: Application Services and Adapters .
- Adapters: This is where I have implemented my DTOs.
- Application Services: It is in this layer that I implement the application's methods, since my Presentation Layer is ignorant of the Domain Layer and vise. Returning to the topic is in this layer where I make the magic that the Domain Entities become DTOs through AutoMapper.
Domain Entities.
public class Proveedor
{
public int ProveedorId { get; set; }
public string RazonSocial { get; set; }
public string Direccion { get; set; }
}
DTOs
public class ProveedorDto
{
[Key]
public int ProveedorId { get; set; }
[Display(Name = "Razón Social")]
public string RazonSocial { get; set; }
[Display(Name = "Dirección")]
public string Direccion { get; set; }
}
Applying AutoMapper in Application Services.
public IEnumerable<ProveedorDto> GetAll()
{
IEnumerable<Proveedor> _proveedor = _sdProveedor.GetAll();
config = new MapperConfiguration(cfg => cfg.CreateMap<Proveedor, ProveedorDto>());
IEnumerable<ProveedorDto> listDto = config.CreateMapper().Map<IEnumerable<ProveedorDto>>(_proveedor);
return listDto;
}
This way, my Domain Entities do not reach the Presentation Layer and the DTOs that are in the Adapter Layer are used as the Models of the MVC design pattern.
- Should I create the Models in the Presentation Layer, do I have to stick to the letter that the Models are implemented in that layer, as MVC says?
- How should I implement that part that I have explained, using good practices so that my architecture is neat?