'Convert a string to a class type/model in C#
It seems that the default Jason Taylor's clean architecture .net template doesn't allow custom order or filtering.
Indeed, the default GetTodosQuery.cs only allow hardcoded order by. I need to give my users the ability to dynamically get a class type from a generic string and apply custom OrderBy, ie.
.OrderBy(t => type) // instead of .OrderBy(t.Title)
So that the Handle method shown here would look like this:
public async Task<TodosVm> Handle(GetTodosQuery request, CancellationToken cancellationToken)
{
var type = StringToType(request.classNameAsString);
return new TodosVm
{
Lists = await _context.TodoLists
.AsNoTracking()
.ProjectTo<TodoListDto>(_mapper.ConfigurationProvider)
.OrderBy(t => type) // instead of t.Title)
.ToListAsync(cancellationToken)
};
}
When doing a query.
I tried several suggestions, but I couldn't come up with something satisfactory (at least performance-wise)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|