In the N-Layer coding example I made, I transform the DTO I received in the service layer into a Model by mapping, and then send it to the Repository layer and try to save it to the db. I'm trying to do it using inheritance in accordance with the logic of clean code, so I used a generic structure. But when I try to convert DTO to model using mapster
I am getting Error CS1503 Argument 1: cannot convert from 'Model' to 'DTO'. How can I fix this or how can I follow a path?
public class Service<Model, DTO> : IService<Model, DTO> where Model : class where DTO : class
{
protected readonly IUnitOfWork _unitOfWork;
private readonly IRepository<DTO> _repository;
public Service(IUnitOfWork unitOfWork, IRepository<DTO> repository)
{
_unitOfWork = unitOfWork;
_repository = repository;
}
public async Task<DTO> AddAsync(DTO entity)
{
Model model = entity.Adapt<Model>();
await _repository.AddAsync(model); <===== where i got error
await _unitOfWork.CommitAsync();
return entity;
}
}
public interface IService<Model,DTO> where Model : class where DTO : class
{
Task<DTO> GetByIdAsync(int id);
Task<IEnumerable<DTO>> GetAllAsync();
IQueryable<DTO> Where(Expression<Func<DTO, bool>> expression);
Task<bool> AnyAsync(Expression<Func<DTO, bool>> expression);
Task<DTO> AddAsync(DTO entity);
Task<IEnumerable<DTO>> AddRangeAsync(IEnumerable<DTO> entities);
Task UpdateAsync(DTO entity);
Task RemoveRangeAsync(IEnumerable<DTO> entities);
Task RemoveAsync(DTO entity);
}