Closed AnirudhaAgashe closed 1 year 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.
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
+1
I've also been looking for an elegant solution to this problem!
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:
I hope it helps!
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?
Up!
Closing as obsolete in the context of the current architecture guide.
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