0

After studying a couple of blog posts on the topic, I got confused as there were different implementations by different developers but they have all used single interface for repository.

Assume, we have following interface for repository

interface Repository<T> {
    fun get(): List<T>
    fun save(items: List<T>)
}

For a specific implementation of this repository with T = Users, Repository.get(), downloads some data from sever and cache it.

For another type of object, say Bundles I not only need these methods but some additional methods e.g. querying local database for specific column.

In that case, I can't understand, do I need to create a separate interface or add methods to existing single interface, in which case what should I do with the implementation of new methods in other repositories which do not need these methods? It it must to have same interface for all repositories. Personally, I think, its ok to create multiple interfaces for different repositories as long as I abstract them nicely.

mallaudin
  • 4,744
  • 3
  • 36
  • 68
  • Note that your question is *very* close to what is covered by the "primarily opinion-based" close vote explanation. I tried to answer your question from a technical standpoint. – CommonsWare May 29 '19 at 16:25
  • I have discussed pros and cons of generic repository in [this](https://stackoverflow.com/a/51781877/5779732) answer in details. – Amit Joshi May 30 '19 at 06:17

1 Answers1

1

do I need to create a separate interface or add methods to existing single interface

You can do whatever you like. Android does not know anything about repositories and imposes no restrictions.

In my work, I do not create a generic interface (like the one that you show) in the first place. I cannot think of a project that I worked on where developers created a generic repository interface, either. I am sure that some people do create such an interface, but there is no requirement by the OS for one.

Personally, I think, its ok to create multiple interfaces for different repositories as long as I abstract them nicely.

You are welcome to do that.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Gosh! May be the confusion raised from the fact that almost all blog posts created repository for only one use case. – mallaudin May 29 '19 at 16:25
  • 2
    @mallaudin: Blog posts have their limits. So do book examples. :-) – CommonsWare May 29 '19 at 16:26
  • Exactly. What I am thinking of, if we really don't want to write same methods in multiple repositories , we can still create a top level interface with generic methods and then create other interfaces for specific repositories. What do you think about this approach? – mallaudin May 29 '19 at 16:28
  • @mallaudin: If you find value in doing that, it should be fine. Personally, I am not a fan of creating interfaces for the sake of having interfaces. For a huge, multi-module project, this might be appropriate, to help maintain some amount of isolation between the modules. Or, if there is a pluggable system of repositories, where a consumer might get different implementations, then an interface is appropriate. But "all repositories need interfaces" is overkill, IMHO, for smaller projects where there will only ever be a single implementation of that interface. – CommonsWare May 29 '19 at 16:31
  • @mallaudin: One exception is if your test code (e.g., mock generator) requires interfaces to work properly. In that case, your interfaces are there to support that particular library. – CommonsWare May 29 '19 at 16:31
  • Perfect. I do agree with `overkill` statement. Kinda same thoughts on the subject. Thanks for you time @CommonsWare or whoever you are replying under the label of CommonsWare ;) – mallaudin May 29 '19 at 16:36