I am using repository pattern in a .NET MVC project where I need to fetch data using the same function but in a two different structures(conditionally).
To be more specific, there are cases where I need to fetch the full version of the model having a bunch of properties but there are also cases where I need to fetch a frugal version of the model (mostly for security reasons).
The code so far:
public async Task<IEnumerable<AnswerMinimalDto>> GetQuestionsForUser(int userId)
{
IEnumerable<Answer> foundAnswers = await this.repository.getAnswersByUser(userId);
return Mapper.Map<IEnumerable<AnswerMinimalDto>>(foundAnswers);
}
So the mapping should happen conditionally:
....return Mapper.Map<IEnumerable<AnswerMinimalDto>>(foundAnswers);
or
....return Mapper.Map<IEnumerable<AnswerFullDto>>(foundAnswers);
Here we have two object oriented principles conflicted. First thought is to have two different methods to satisfy the Single Responsibility principle. On the other hand, by having two different methods doing the same job is a duplication.
I am going with the approach of a single method. What I've tried so far is to make an approach using Tuples to return both models and handle the required result from the controller (out can not apply since my method is async). But I am not really happy with the approach.
To the point, I wonder if there is any elegant/preferable way to return the data in different structure chosen conditionally.
Any help is welcome.