简体   繁体   English

洋葱架构中的Dtos

[英]Dtos in Onion architecture

Something is not right with my structure. 我的结构不正确。 I have a webapi(project with all controllers) And there is a reference to my IService layer in core project (IServices classes in core) here is also the core entities(domain entities). 我有一个webapi(具有所有控制器的项目),并且在核心项目(核心中的IServices类)中有我的IService层的引用,这里也是核心实体(域实体)。

From the wepapi project I call my Iservices and I get my response with objects from domain entities. 从wepapi项目中,我调用了Iservices,并且得到了来自域实体的对象的响应。

And now my question Should the services return and accept Domain entities(plain customer) or DTO:s(customerForUpdate,customerForCreate). 现在,我的问题是服务应该返回并接受域实体(普通客户)还是DTO:s(customerForUpdate,customerForCreate)。

If I return Domain entities I get many lookups against several services to create my DTO. 如果返回域实体,则会针对多个服务进行多次查找以创建我的DTO。 example customer with clothdetails(Customer is one service and Cloth is another). 例如,有衣服细节的客户(客户是一项服务,布料是另一项服务)。 This makes Ef core to flip beacuse each service method calls a save in unitOfWork. 这使得Ef核心可以翻转,因为每个服务方法都调用unitOfWork中的save。 Example I call clothservice to get on value to use for customer and the update clothservice again. 示例我致电clothservice以获取可用于客户的价值,并再次更新clothservice。

If I return Dto:s from my IServices I need to add a reference from the service project to the DTO(in my infra project). 如果我从IServices返回Dto:s,则需要从服务项目向DTO(在我的基础项目中)添加引用。 But in this case I can call other services with in this project and create my dto. 但是在这种情况下,我可以在该项目中调用其他服务并创建我的dto。 and just call save once when I am finished. 并在完成操作后致电一次save。

Or is there another way to just call save once per webapi call or I have missunderstand the dtos this kind of dtos should be places in the core instead because they are not real dto:s? 还是有另一种方法,每个webapi调用仅调用一次保存,或者我误解了dto,因此这类dto应该位于核心位置,因为它们不是真正的dto:s?

according to the onion architecture, all dependencies should be on the domain layer, and each layer with its own higher layer can communicate. 根据洋葱体系结构,所有依赖项都应在域层上,并且具有自己更高层的每一层都可以通信。 Now, the point is that the domain layer should not have access to the domain layer and other layers like infrastructure at all, and only in the web service layer or UI should only see the application layer, now your problem is to fix it. 现在,关键是域层完全不能访问域层和其他层(如基础结构),并且仅在Web服务层或UI中应该只能看到应用程序层,现在您的问题是要对其进行修复。 Make a layer separate from the application layer in which the application layer interfaces are named (Application.Contracts) In this layer, in addition to the interfaces you define for the application layer, you define the DTOs on the same layer. 将一个层与命名为(Application.Contracts)的应用程序层的应用程序层分开。在此层中,除了为该应用程序层定义的接口之外,还要在同一层上定义DTO。 Then, with this, you define the interface you want to define for the application layer, it only works with those (DTOs) so that the web service layer or the UI layer does not have a domain Layer. 然后,以此定义要为应用程序层定义的接口,该接口仅与那些接口(DTO)配合使用,以便Web服务层或UI层没有域层。

-Application
    StudentService
    TeacherService

-Application.Contracts
          --Interfaces
              IStudentService
              ITeacherService
          --DTO
              StudentListDto
              TeacherDto
-UI Or Web service layer
 //Only can work with Application.Contracts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM