问题描述
我想用Dtos把数据传给服务,我试过放到业务层,但是有问题。
我的架构类似如下:
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
我从控制器中的表单中读取数据,将其转换为 SaveProductDto 并将其发送到服务。 Service以Dto为参数,但是我在业务层定义Dtos时无法将dto作为参数给service中方法的接口。找不到 SaveProductDto 引用。
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;
}
}
我不能在这里写 SaveProductDto:
public interface IProductService
{
Task<Product> AddProduct(SaveProductDto saveProductResource);
}
ProductService 位于业务层,IProductService 位于核心层,正如我之前解释的那样。核心层中如何引用SaveProductDto?
更新:标题已更改
解决方法
希望以下内容有所帮助。它引用自 https://aspnetboilerplate.com/Pages/Documents/NLayer-Architecture
这还提供了有关如何构建 n 层架构的指导https://aspnetboilerplate.com/Pages/Documents/Introduction
,在你的情况下,控制器应该与 DTO 一起工作,服务与域模型。所有接口都不应处理 DTO,而应在您的业务层中处理。应该从控制器执行 DTO 到模型映射器。
其他解决方案是将 dto 放入核心。您可以在此 answer 中找到更多详细信息。