I wondered how I could return the DTO without breaking anything.
Here is the code from the service where I access the entity.
public async Task<IReadOnlyList<Todo>> GetAllTodosAsync(){ var result = await _repository.GetAllAsync(); if (result is not null) return result; else throw new Exception("List is null");}
public async Task<IReadOnlyList<Todo>> GetAllTodosAsync(){ var result = await _repository.GetAllAsync(); if (result is not null) return result; else throw new Exception("List is null");}
And here in the controller
[HttpGet]public async Task<ActionResult<IReadOnlyList<Todo>>> GetAll(){ var todos = await _todoService.GetAllTodosAsync(); return Ok(todos);}
[HttpGet]public async Task<ActionResult<IReadOnlyList<Todo>>> GetAll(){ var todos = await _todoService.GetAllTodosAsync(); return Ok(todos);}
I could create a mapper right here in the controller, but is that a good approach? If I'm doing anything, it's purely because I don't like the way it glows in the swager.
Here:
and the DTO that should replace the Todo entity itself looks virtually identical to it, but the point is that I don't think it's a good idea to expose the entity.