【发布时间】:2021-01-08 03:14:53
【问题描述】:
我想用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作为参数给服务中方法的接口。找不到 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?
更新:标题已更改
【问题讨论】:
-
DDD / n-tier-architecture 的良好参考点将是 aspnetboilerplate.com
-
兄弟,我写的文章可能看起来很主观,但我只是想在界面中添加dto,我得到了一个错误。我添加了我的架构看起来像提供什么信息可能是错误的。
-
@00110001 标题已更改
-
其实不清楚你的问题是什么。 DTO(数据传输对象)不是业务层类型。是什么导致了这个问题——“[你试过]把它放在业务层”。你为什么这么做? ..这似乎是一件随意的事情。你想达到什么目的?
-
@BrettCaswell 我第一次在项目中使用 dtos,所以我不太熟悉。我应该把它们放在核心中吗?
标签: c# asp.net-core entity-framework-core repository-pattern n-tier-architecture