【问题标题】:Can't Give Dto Reference to Service Interface无法为服务接口提供 Dto 引用
【发布时间】: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


【解决方案1】:

希望以下内容有所帮助。引用自https://aspnetboilerplate.com/Pages/Documents/NLayer-Architecture

这也提供了如何构建 n 层架构的指导https://aspnetboilerplate.com/Pages/Documents/Introduction

【讨论】:

    【解决方案2】:

    在您的情况下,控制器应该与 DTO 一起使用,服务与域模型一起使用。所有接口都不应该处理 DTO,而是在您的业务层中。 DTO 到模型映射器应该从控制器执行。

    其他解决方案是将 dto 放入 Core。您可以从answer 找到更多详细信息。

    【讨论】:

      猜你喜欢
      • 2023-03-28
      • 1970-01-01
      • 2012-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-21
      • 1970-01-01
      相关资源
      最近更新 更多