Pseudo-Code
interface IPagingInfo
{
int CurrentPageNo { get; }
int RowsPerPage { get; }
...
}
interface ResultsRetriver
{
ResultRows GetResults(IPagingInfo pagingInfo);
}
class ANewResultsRetriver : ResultsRetriver
{
ResultRows GetResults(IPagingInfo pagingInfo)
{
// TODO: Retrieve and return all results, ignore pagingInfo.
}
}
Explanation
interface ResultsRetriver
has existed since as long as we know time. There are many implementations of ResultsRetriver
all happily working in the system. The new programmer comes in and is tasked with writing the ANewResultsRetriver
. His results retriever however is different in that it does not have a concept of paging, it must always return all results.
The programmer feels the person who wrote interface ResultsRetriver
should have provided a version of the call without the paging parameter for cases such as this, where the parameter is not required. Now he wants to add it in, a GetResults()
.
A colleague suggests that it's okay to not use the pagingInfo parameter in this special case, but what if the caller sends in valid pagingInfo expecting results to conform to the supplied pagingInfo? Then ignoring the callers request may not be prudent. So the colleague also suggests we throw a PagingNotSupportedException()
to alert the caller if a valid pagingInfo is supplied.
Suggestions:
- Add a new
GetResults
method toResultsRetriver
which does not take any parameters. Rewire all code around the interface to call the correct version based on validity of pagingInfo. - Ignore the pagingInfo parameter.
- Ignore the pagingInfo parameter but throw a
PagingNotSupportedException()
if a valid pagingInfo parameter is supplied to alert the caller to the fact that the parameter will be ignored and they may need to work around this. - Argue that what the programmer is writing is not really a
ResultsRetriver
as aResultsRetriver
must support paging by its original definition. So a new interface must be definedNonPagedResultsRetriver
with aGetResults()
method taking no parameters. The refactor to include this interface with surrounding code that currently works withResultsRetriver
to also work withNonPagedResultsRetriver
- Something else?
Question
What is the optimal solution and why?
pagingInfo
parameter, and 2. would the caller ofGetResults
be aware of the different results retrievers, or does it just work with a passed-inResultsRetriver
instance?