1

I want to use Dtos to transfer data to service.I tried putting it in the business layer, but there is a problem.

My architecture similarly looks like this:

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

I read the data from the form in the controller, convert it to SaveProductDto and send it to the service. Service takes Dto as a parameter, but I cannot give dto as a parameter to the interface of the method in the service when I defined Dtos in the Business Layer. SaveProductDto reference cannot be found.

  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;
       }       
   
   }

I can't write SaveProductDto here:

   public interface IProductService
   {
       Task<Product> AddProduct(SaveProductDto saveProductResource);
   }

ProductService is in the Business Layer, IProductService is in the Core Layer as I explain before. How can I give reference to SaveProductDto in the Core Layer?

Update: Title was changed

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
alihangir
  • 25
  • 1
  • 7
  • Good reference point for DDD / n-tier-architecture will be https://aspnetboilerplate.com/ – Ramesh Jan 08 '21 at 03:22
  • Bro, the article I wrote may seem subjective, but I'm just trying to add dto to the interface and I'm getting an error. I added my architecture looks like what to give information maybe it was wrong. – alihangir Jan 08 '21 at 04:00
  • @00110001 Title was changed – alihangir Jan 08 '21 at 04:07
  • It's actually not clear what your question is. DTOs (Data Transferable Objects) are not Business Layer types. What caused the issue - "[you tried] putting it in the business layer". Why did you do that? ..It seems like an arbitrary thing to do. What are you trying to achieve? – Brett Caswell Jan 08 '21 at 05:47
  • @BrettCaswell First time I use dtos int the project, so I don't really familiar with it. Should I put it them in the Core instead? – alihangir Jan 08 '21 at 06:26

2 Answers2

1

In your case, controller should work with DTO, service with domain models. All Interfaces should not handle DTO but in your Business Layer. The DTO to Model mapper should be executed from controller.

Other solutions is putting dto's into Core. You could find more details from this answer.

Michael Wang
  • 3,782
  • 1
  • 5
  • 15
0

Hope the below helps. It is referenced from https://aspnetboilerplate.com/Pages/Documents/NLayer-Architecture

This also provides guidance on how to build n-tier architecture https://aspnetboilerplate.com/Pages/Documents/Introduction

enter image description here

Ramesh
  • 13,043
  • 3
  • 52
  • 88