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.

2
  • 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. Commented May 29, 2019 at 16:25
  • I have discussed pros and cons of generic repository in this answer in details. Commented May 30, 2019 at 6:17

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Gosh! May be the confusion raised from the fact that almost all blog posts created repository for only one use case.
@mallaudin: Blog posts have their limits. So do book examples. :-)
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: 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.
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.