微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

不能给服务接口的 Dto 引用

如何解决不能给服务接口的 Dto 引用

我想用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

enter image description here

,

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

其他解决方案是将 dto 放入核心。您可以在此 answer 中找到更多详细信息。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?