android / architecture-samples

A collection of samples to discuss and showcase different architectural tools and patterns for Android apps.
Apache License 2.0
44.38k stars 11.63k forks source link

[question] Repository Pattern empty methods in read only remote data source #201

Closed AnirudhaAgashe closed 1 year ago

AnirudhaAgashe commented 8 years ago

How 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
    }

}
gokhanbarisaker commented 8 years ago

You need to depend on interfaces. In your case, it is StudentDataSource. A repository pattern is a concrete implementation of DataSource that orchestrates/delegates multiple DataSource implementations for you.

Considering you are depending on interfaces. Passing instance of StudentRemoteDataSource instead of StudentRepository should be fine.

AnirudhaAgashe commented 7 years ago

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

noloman commented 7 years ago

+1

starkej2 commented 7 years ago

I've also been looking for an elegant solution to this problem!

celsogithub commented 7 years ago

Dear @AnirudhaAgashe,

There is an example in Fernando Cejas project that handles this problem throwing an UnsupportedOperationException, but it is used in local data source instead of remote data source:

https://github.com/android10/Android-CleanArchitecture/blob/ae30300e1c2b1edbb096ad42ae66fc34c4979bb1/data/src/main/java/com/fernandocejas/android10/sample/data/repository/datasource/DiskUserDataStore.java#L41

I hope it helps!

HughIngram commented 6 years ago

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?

Lampotrias commented 4 years ago

Up!

tunjid commented 1 year ago

Closing as obsolete in the context of the current architecture guide.