Reputation: 25
I want to use Dtos to transfer data to service.I tried putting it in the business layer, but there is a problem.
My architecture similarly looks like this:
Core Layer (Dependencies:-)
-Entity Models
-Interfaces of Repositories
-Interfaces of Services
Data Layer (Dependencies:Core Layer)
-DbContext
-DbMappings
-Repositories
Business Layer (Dependencies: Core Layer, Data Layer)
-Services
-DTOs ???
WebApp Layer Business Layer (Dependencies: Core Layer, Data Layer, Business Layer)
-Controllers
-Views
-View Models, etc
I read the data from the form in the controller, convert it to SaveProductDto and send it to the service. Service takes Dto as a parameter, but I cannot give dto as a parameter to the interface of the method in the service when I defined Dtos in the Business Layer. SaveProductDto reference cannot be found.
public class ProductService : IProductService
{
private readonly IUnitOfWork _unitOfWork;
private readonly IMapper _mapper;
public ProductService(IUnitOfWork unitOfWork, IMapper mapper)
{
_unitOfWork = unitOfWork;
_mapper= mapper;
}
public async Task<Product> AddProduct(SaveProductDto saveProductResource)
{
var newProduct = _mapper.Map<SaveProductDto, Product>(saveProductResource);
await _unitOfWork.Products.AddAsync(newProduct);
await _unitOfWork.CommitAsync();
return newProduct;
}
}
I can't write SaveProductDto here:
public interface IProductService
{
Task<Product> AddProduct(SaveProductDto saveProductResource);
}
ProductService is in the Business Layer, IProductService is in the Core Layer as I explain before. How can I give reference to SaveProductDto in the Core Layer?
Update: Title was changed
Upvotes: 1
Views: 899
Reputation: 4022
In your case, controller should work with DTO, service with domain models. All Interfaces should not handle DTO but in your Business Layer. The DTO to Model mapper should be executed from controller.
Other solutions is putting dto's into Core. You could find more details from this answer.
Upvotes: 1
Reputation: 13266
Hope the below helps. It is referenced from https://aspnetboilerplate.com/Pages/Documents/NLayer-Architecture
This also provides guidance on how to build n-tier architecture https://aspnetboilerplate.com/Pages/Documents/Introduction
Upvotes: 0