C
C#3mo ago
greg/Ryhor

Not sure what Senior Dev expect me to do

Hello, I'm moving my post to this forum so it will be more visible. I just finished in my job simple endpoint that returns paginated result, we use clean arch with mediatr - easy. My handler looks something like (pseudo code)
class GetItemsHandler : IRequestHanlder<PaginatedResultsDto<ItemDto>>

query = _repo.SelectItems

query.AddSearch //just add where for filer over one field

totalCount = query.Count();

query.AddSort() //parameters were parsed from http query params


query.skip().take()

return totalCount, page, items etc.
class GetItemsHandler : IRequestHanlder<PaginatedResultsDto<ItemDto>>

query = _repo.SelectItems

query.AddSearch //just add where for filer over one field

totalCount = query.Count();

query.AddSort() //parameters were parsed from http query params


query.skip().take()

return totalCount, page, items etc.
... so not much logic into that, And then Senior Dev join my Pull Request, marked whole file with comment 'I think that paginated handlers with search and sort can be made in generic way' Tbh I havent see something like that ever, especially if that used clean arch with Mediatr. So I am now in seek and wonder how to do that and I question myself if its even good practice.. Could I use your wisdom with maybe some example from web?
13 Replies
Jimmacle
Jimmacle3mo ago
are there other handlers in the code that return paginated results that you can compare yours to? it's not clear if they're telling you to follow an established pattern in the code or if they're just brainstorming out loud
greg/Ryhor
greg/RyhorOP3mo ago
It is first handler that requires pagination, search and sort. Fresh app
Jimmacle
Jimmacle3mo ago
then i wouldn't bother making it generic until there is a reason to
greg/Ryhor
greg/RyhorOP3mo ago
how could I explain it to senior dev? I'm just regular dev, he is talking from his higher position and I'm not in position to just say 'no'. What would be explanation?
Jimmacle
Jimmacle3mo ago
do you expect the application to have many more paginated handlers in the future?
greg/Ryhor
greg/RyhorOP3mo ago
at least one more is still in planning
danc
danc3mo ago
If there is an expectation that more are coming, I would make it generic too following your own pseudo code example, making it generic should be very simple:
class PaginatedHandler<T> : IRequestHandler<PaginatedResultsDto<T>>

query = _repo.SelectItems

query.AddSearch

totalCount = query.Count()

query.AddSort()

query.skip().take()

return totalCount, page, items etc.
class PaginatedHandler<T> : IRequestHandler<PaginatedResultsDto<T>>

query = _repo.SelectItems

query.AddSearch

totalCount = query.Count()

query.AddSort()

query.skip().take()

return totalCount, page, items etc.
You could then create paginated handlers for any type by
var handler = new PaginatedHandler<ItemDto>(_repo, ...);
var handler = new PaginatedHandler<ItemDto>(_repo, ...);
greg/Ryhor
greg/RyhorOP3mo ago
Yes, I actually want to come up with something.. thanks for that example. Are you familiar with any articles on that with more code?
xriba
xriba3mo ago
Generic classes and methods - C#
Learn about generics. Generic types maximize code reuse, type safety, and performance, and are commonly used to create collection classes.
greg/Ryhor
greg/RyhorOP3mo ago
I know how generics work, I'd like to get some ideas behind PaginatedHanler implementations itself. Espesially that I somehow need to combine with sort and search.. hmm. But the most important thing you gave me is that it's not bad practice
Wrenpo
Wrenpo3mo ago
I am just wondering... have you talked to your senior dev yet about this directly? Share your thoughts and maybe he can expand on his. Open that communication channel! That's most of the job anyhow.
greg/Ryhor
greg/RyhorOP3mo ago
I haven't. I will do that tomorrow and doing background for that talk
xriba
xriba3mo ago
That's a good idea. It's better to clarify these things before you start your implementation. As an example you could create a generic object (SearchCriteria) that contains fields such as pagination and sorting but it really depends on the requirements and expectations. Remember YAGNI
Want results from more Discord servers?
Add your server