Allow abstraction of MAPPER to work with Generics
See original GitHub issueIf you have a number of tables that need to have Cursor
s mapped to Objects created by SqlDelight
, then you will end up with duplicate code in many classes. In my code base, I have 6 tables that use 7 Loader
s to retrieve data.
In the code below, this is the whole class from one of my Loader
s, and the code in the loadInBackground()
method is highly repetitive and could be moved to the superclass; however, in the current version of SqlDelight
this doesn’t seem possible.
public class WorkLoader extends ProviderLoader<Resume.Work> {
// -----------
public WorkLoader(Context context) { super(context); }
@Override
public List<Resume.Work> loadInBackground() {
List<Resume.Work> results = new ArrayList<>(5);
Cursor cursor = getProvider().queryAllWork();
// ^^^^^^^^^^^^^^
while (cursor.moveToNext()) {
results.add(Resume.Work.MAPPER.map(cursor));
// ^^^^^^^^^^^^^^^^^^
}
return results;
}
}
In the above code, WorkLoader
would not contain an overridden loadInBackground()
method, but would implement an abstract method that would return a Cursor
to the superclass and the super class would be able to determine the correct MAPPER
in its loadInBackground()
method.
Is something like this possible to implement?
Issue Analytics
- State:
- Created 7 years ago
- Comments:9 (5 by maintainers)
Top GitHub Comments
We have a
RowMapper<T>
interface all mappers implement for this.You could use it like:
and then call it like:
from subclasses.
Yeah, that was going to be my next suggestion since it not only lets you mark each type, but also perhaps partially mark them in any other ways you see fit to constrain your application-layer code.
On Fri, Jun 3, 2016 at 11:43 AM Christopher Rucinski < notifications@github.com> wrote: