[question] Repository Pattern empty methods in read only remote data source
See original GitHub issueHow can we handle cases where one of the resources lets assume student is read only from server end. In such cases we have saveStudents method in the interface which is being implemented only by the local data source and the remote data source has empty method.
There are multiple other such scenario where implementation is needed only in one data source and other implementations are empty. Although the interaction is provided by repository and we can get it to work leaving empty method implementation doesn’t feel right. Is there any way to take care of this in elegant fashion ?
For example: Students services is a read only operation from server end and no new insertion can take place from client. But we need to cache the data locally. In such cases the implmentation looks something like this
public interface StudentsDataSource {
Observable<List<Student>> getStudent();
void saveStudent(@NonNull Student student);
}
public StudentsLocalDataSource{
Observable<List<Student>> getStudent(){
// return list of students from db
}
void saveStudent(@NonNull Student student){
// save student to db
}
}
public StudentsLocalDataSource{
Observable<List<Student>> getStudent(){
// return list of students after calling api
}
void saveStudent(@NonNull Student student){
// What to do here ??
// leaving this method empty is one option but this feels wrong
// plus in a project there are cases when some operation is only possible
// on server end, or vice versa
// in all such cases there will be lot of empty methods
}
}
Issue Analytics
- State:
- Created 7 years ago
- Reactions:10
- Comments:7
Top GitHub Comments
I have found that when implementing this pattern, I end up with a large number of UnsupportedOperationExctions being thrown, and in many cases there is only one class which implements an operation (e.g. when the Repository supports local caching of a read only Object, the saveObject() method is implemented only by the LocalDataSource. To me this seems like a major code smell; if only one implementation of the DataSource Interface has a certain method, then perhaps it should not be a part of this interface?
I have edited my question so as to make it more clear. I have got no problem to getting it work. I have concerns regarding the methods that are left empty since only one implementation for them is required. I am looking for a elegant solution to handle such scenario. Thanks for the help