2

Usually, I use an Interface for my repositories:

interface UserRepository {
    fun add()
    fun get(userId): User
    .....
}

I have three types of Repositories (InMemory, Database, and Network):

They all implement UserRepository: UserInMemoryRepository - UserDatabaseRepository - UserNetworkRepository

But my network doesn't have any add user API

What is the best practice here?

  1. Remove my UserRepository interface
  2. Leave an empty add user method
  3. ...... (Any better approach)
3
  • You should have only one RepositoryImpl, inside it has logic when getting data from memory or database or network Commented May 11, 2020 at 8:42
  • @CôngHải That's not how Google Samples implement this Commented May 11, 2020 at 8:56
  • Please take a look here developer.android.com/jetpack/docs/guide. Inside UserRepository it manage both memory cached and remote network. Commented May 11, 2020 at 9:23

1 Answer 1

2

Leaving add() empty doesn't goes in pair with SOLID principles. If more than one class should override some method, then you shouldn't remove interface either. What I would do is create interface:

interface Repository {
    fun get(userId): User
}
...
interface UserRepository : Repository {
     fun add()
}

Then your "Network" can implement Repository, and the rest of classes implement UserRepository. Of course naming of interfaces is up to you, but according to 'I' in SOLID it is better to have more dedicated interfaces rather than one multifunctional

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

Comments

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.