question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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
    }


}

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:10
  • Comments:7

github_iconTop GitHub Comments

3reactions
HughIngramcommented, Jun 7, 2018

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?

3reactions
AnirudhaAgashecommented, Oct 11, 2016

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use Store Procedure and Complex types using ...
How can we use store procedures, and complex types while dealing with Repository pattern in Entity Framework?Could anybody please give a simple example....
Read more >
Repository Pattern with Jetpack Compose - RayWenderlich.com
Read and display remote data. Persist and restore local data with Room. Use pagination with LazyColumn. Manage and update UI States with Compose ......
Read more >
4 Common Mistakes with the Repository Pattern
Your repositories should return domain objects and the client of the repository can decide if it needs to do the mapping. By mapping...
Read more >
Flutter App Architecture: The Repository Pattern
What is the repository design pattern? · isolate domain models (or entities) from the implementation details of the data sources in the data ......
Read more >
Implementing the Repository and Unit of Work Patterns in an ...
The repository implements IDisposable and disposes the database context as you saw earlier in the controller, and its CRUD methods make calls to ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found